tuninstantiatedgenericcalls.nim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. # Cases that used to only work due to weird workarounds in the compiler
  2. # involving not instantiating calls in generic bodies which are removed
  3. # due to breaking statics.
  4. # The issue was that these calls are compiled as regular expressions at
  5. # the generic declaration with unresolved generic parameter types,
  6. # which are special cased in some places in the compiler, but sometimes
  7. # treated like real types.
  8. block:
  9. type Base10 = object
  10. func maxLen(T: typedesc[Base10], I: type): int8 =
  11. when I is uint8:
  12. 3
  13. elif I is uint16:
  14. 5
  15. elif I is uint32:
  16. 10
  17. elif I is uint64:
  18. 20
  19. else:
  20. when sizeof(uint) == 4:
  21. 10
  22. else:
  23. 20
  24. type
  25. Base10Buf[T: SomeUnsignedInt] = object
  26. data: array[maxLen(Base10, T), byte]
  27. len: int8
  28. var x: Base10Buf[uint32]
  29. doAssert x.data.len == 10
  30. var y: Base10Buf[uint16]
  31. doAssert y.data.len == 5
  32. import typetraits
  33. block thardcases:
  34. proc typeNameLen(x: typedesc): int {.compileTime.} =
  35. result = x.name.len
  36. macro selectType(a, b: typedesc): typedesc =
  37. result = a
  38. type
  39. Foo[T] = object
  40. data1: array[T.high, int]
  41. data2: array[typeNameLen(T), float]
  42. data3: array[0..T.typeNameLen, selectType(float, int)]
  43. type MyEnum = enum A, B, C, D
  44. var f1: Foo[MyEnum]
  45. var f2: Foo[int8]
  46. doAssert high(f1.data1) == 2 # (D = 3) - 1 == 2
  47. doAssert high(f1.data2) == 5 # (MyEnum.len = 6) - 1 == 5
  48. doAssert high(f2.data1) == 126 # 127 - 1 == 126
  49. doAssert high(f2.data2) == 3 # int8.len - 1 == 3
  50. static:
  51. doAssert high(f1.data1) == ord(C)
  52. doAssert high(f1.data2) == 5 # length of MyEnum minus one, because we used T.high
  53. doAssert high(f2.data1) == 126
  54. doAssert high(f2.data2) == 3
  55. doAssert high(f1.data3) == 6 # length of MyEnum
  56. doAssert high(f2.data3) == 4 # length of int8
  57. doAssert f2.data3[0] is float
  58. import muninstantiatedgenericcalls
  59. block:
  60. var x: Leb128Buf[uint32]
  61. doAssert x.data.len == 5
  62. var y: Leb128Buf[uint16]
  63. doAssert y.data.len == 3
  64. import macros
  65. block: # issue #12415
  66. macro isSomePointerImpl(t: typedesc): bool =
  67. var impl = t.getTypeInst[1].getTypeImpl
  68. if impl.kind == nnkDistinctTy:
  69. impl = impl[0].getTypeImpl
  70. if impl.kind in {nnkPtrTy,nnkRefTy}:
  71. result = newLit(true)
  72. elif impl.kind == nnkSym and impl.eqIdent("pointer"):
  73. result = newLit(true)
  74. else:
  75. result = newLit(false)
  76. proc isSomePointer[T](t: typedesc[T]): bool {.compileTime.} =
  77. isSomePointerImpl(t)
  78. type
  79. Option[T] = object
  80. ## An optional type that stores its value and state separately in a boolean.
  81. when isSomePointer(typedesc(T)):
  82. val: T
  83. else:
  84. val: T
  85. has: bool
  86. var x: Option[ref int]
  87. doAssert not compiles(x.has)
  88. var y: Option[int]
  89. doAssert compiles(y.has)
  90. block: # issue #2002
  91. proc isNillable(T: typedesc): bool =
  92. when compiles((let v: T = nil)):
  93. return true
  94. else:
  95. return false
  96. type
  97. Foo[T] = object
  98. when isNillable(T):
  99. nillable: float
  100. else:
  101. notnillable: int
  102. var val1: Foo[ref int]
  103. doAssert compiles(val1.nillable)
  104. doAssert not compiles(val1.notnillable)
  105. var val2: Foo[int]
  106. doAssert not compiles(val2.nillable)
  107. doAssert compiles(val2.notnillable)
  108. block: # issue #1771
  109. type
  110. Foo[X, T] = object
  111. bar: array[X.low..X.high, T]
  112. proc test[X, T](f: Foo[X, T]): T =
  113. f.bar[X.low]
  114. var a: Foo[range[0..2], float]
  115. doAssert test(a) == 0.0
  116. block: # issue #23730
  117. proc test(M: static[int]): array[1 shl M, int] = discard
  118. doAssert len(test(3)) == 8
  119. doAssert len(test(5)) == 32
  120. block: # issue #19819
  121. type
  122. Example[N: static int] = distinct int
  123. What[E: Example] = Example[E.N + E.N]
  124. block: # issue #23339
  125. type
  126. A = object
  127. B = object
  128. template aToB(t: typedesc[A]): typedesc = B
  129. type
  130. Inner[I] = object
  131. innerField: I
  132. Outer[O] = object
  133. outerField: Inner[O.aToB]
  134. var x: Outer[A]
  135. doAssert typeof(x.outerField.innerField) is B
  136. block: # deref syntax
  137. type
  138. Enqueueable = concept x
  139. x is ptr
  140. Foo[T: Enqueueable] = object
  141. x: typeof(default(T)[])
  142. proc p[T](f: Foo[T]) =
  143. var bar: Foo[T]
  144. discard
  145. var foo: Foo[ptr int]
  146. p(foo)
  147. doAssert foo.x is int
  148. foo.x = 123
  149. doAssert foo.x == 123
  150. inc foo.x
  151. doAssert foo.x == 124
  152. block:
  153. type Generic[T] = object
  154. field: T
  155. macro foo(x: typed): untyped = x
  156. macro bar[T](x: typedesc[Generic[T]]): untyped = x
  157. type
  158. Foo[T] = object
  159. field: Generic[int].foo()
  160. Foo2[T] = object
  161. field: Generic[T].foo()
  162. Bar[T] = object
  163. field: Generic[int].bar()
  164. Bar2[T] = object
  165. field: Generic[T].bar()
  166. var x: Foo[int]
  167. var x2: Foo2[int]
  168. var y: Bar[int]
  169. var y2: Bar2[int]
  170. block:
  171. macro pick(x: static int): untyped =
  172. if x < 100:
  173. result = bindSym"int"
  174. else:
  175. result = bindSym"float"
  176. type Foo[T: static int] = object
  177. fixed1: pick(25)
  178. fixed2: pick(125)
  179. unknown: pick(T)
  180. var a: Foo[123]
  181. doAssert a.fixed1 is int
  182. doAssert a.fixed2 is float
  183. doAssert a.unknown is float
  184. var b: Foo[23]
  185. doAssert b.fixed1 is int
  186. doAssert b.fixed2 is float
  187. doAssert b.unknown is int
  188. import std/sequtils
  189. block: # version of #23432 with `typed`, don't delay instantiation
  190. type
  191. Future[T] = object
  192. InternalRaisesFuture[T, E] = object
  193. macro Raising[T](F: typedesc[Future[T]], E: varargs[typed]): untyped =
  194. let raises = nnkTupleConstr.newTree(E.mapIt(it))
  195. nnkBracketExpr.newTree(
  196. ident "InternalRaisesFuture",
  197. nnkDotExpr.newTree(F, ident"T"),
  198. raises
  199. )
  200. type X[E] = Future[void].Raising(E)
  201. proc f(x: X) = discard
  202. var v: Future[void].Raising([ValueError])
  203. f(v)
  204. block: # issue #22647
  205. proc c0(n: static int): int = 8
  206. proc c1(n: static int): int = n div 2
  207. proc c2(n: static int): int = n * 2
  208. proc c3(n: static int, n2: int): int = n * n2
  209. proc `**`(n: static int, n2: int): int = n * n2
  210. proc c4(n: int, n2: int): int = n * n2
  211. type
  212. a[N: static int] = object
  213. f0 : array[N, int]
  214. b[N: static int] = object
  215. f0 : a[c0(N)] # does not work
  216. f1 : a[c1(N)] # does not work
  217. f2 : a[c2(N)] # does not work
  218. f3 : a[N * 2] # does not work
  219. f4 : a[N] # works
  220. f5: a[c3(N, 2)]
  221. f6: a[N ** 2]
  222. f7: a[2 * N]
  223. f8: a[c4(N, 2)]
  224. proc p[N: static int](x : a[N]) = discard x.f0[0]
  225. template check(x, s: untyped) =
  226. p(x)
  227. doAssert x is a[s]
  228. doAssert x.N == s
  229. doAssert typeof(x).N == s
  230. doAssert x.f0 == default(array[s, int])
  231. doAssert x.f0.len == s
  232. proc p2[N: static int](y : a[N]) {.gensym.} =
  233. doAssert y is a[s]
  234. doAssert y.N == s
  235. doAssert typeof(y).N == s
  236. doAssert y.f0 == default(array[s, int])
  237. doAssert y.f0.len == s
  238. p2(x)
  239. proc p3(z: typeof(x)) {.gensym.} = discard
  240. p3(default(a[s]))
  241. proc p[N: static int](x : b[N]) =
  242. x.f0.check(8)
  243. x.f1.check(2)
  244. x.f2.check(8)
  245. x.f3.check(8)
  246. x.f4.check(4)
  247. x.f5.check(8)
  248. x.f6.check(8)
  249. x.f7.check(8)
  250. x.f8.check(8)
  251. var x: b[4]
  252. x.p()
  253. block: # issue #1969
  254. type ZeroGenerator = object
  255. proc next(g: ZeroGenerator): int = 0
  256. # This compiles.
  257. type TripleOfInts = tuple
  258. a, b, c: typeof(new(ZeroGenerator)[].next)
  259. # This raises a compiler error before it's even instantiated.
  260. # The `new` proc can't be resolved because `Generator` is not defined.
  261. type TripleLike[Generator] = tuple
  262. a, b, c: typeof(new(Generator)[].next)
  263. import std/atomics
  264. block: # issue #12720
  265. const CacheLineSize = 128
  266. type
  267. Enqueueable = concept x, type T
  268. x is ptr
  269. x.next is Atomic[pointer]
  270. MyChannel[T: Enqueueable] = object
  271. pad: array[CacheLineSize - sizeof(default(T)[]), byte]
  272. dummy: typeof(default(T)[])
  273. block: # issue #12714
  274. type
  275. Enqueueable = concept x, type T
  276. x is ptr
  277. x.next is Atomic[pointer]
  278. MyChannel[T: Enqueueable] = object
  279. dummy: type(default(T)[])
  280. block: # issue #24044
  281. type ArrayBuf[N: static int, T = byte] = object
  282. buf: array[N, T]
  283. template maxLen(T: type): int =
  284. sizeof(T) * 2
  285. type MyBuf[I] = ArrayBuf[maxLen(I)]
  286. var v: MyBuf[int]
  287. block: # issue #15959
  288. proc my[T](a: T): typeof(a[0]) = discard
  289. proc my2[T](a: T): array[sizeof(a[0]), T] = discard
  290. proc byLent2[T](a: T): lent type(a[0]) = a[0] # Error: type mismatch: got <T, int literal(0)>
  291. proc byLent3[T](a: T): lent typeof(a[0]) = a[0] # ditto
  292. proc byLent4[T](a: T): lent[type(a[0])] = a[0] # Error: no generic parameters allowed for lent
  293. var x = @[1, 2, 3]
  294. doAssert my(x) is int
  295. doAssert my2(x) is array[sizeof(int), seq[int]]
  296. doAssert byLent2(x) == 1
  297. doAssert byLent2(x) is lent int
  298. doAssert byLent3(x) == 1
  299. doAssert byLent3(x) is lent int
  300. doAssert byLent4(x) == 1
  301. doAssert byLent4(x) is lent int
  302. proc fn[U](a: U): auto = a
  303. proc my3[T](a: T, b: typeof(fn(a))) = discard
  304. my3(x, x)
  305. doAssert not compiles(my3(x, x[0]))
  306. block: # issue #14877
  307. proc fn[T](a: T, index: int): typeof(a.x) = a.x # ok
  308. proc fn2(a: seq[int], index: int): typeof(a[0]) = a[index] # ok
  309. proc fn3[T](a: T, index: int): typeof(a[0]) = a[index] # Error: type mismatch: got <T, int literal(0)>
  310. block: # issue #22342, type section version of #22607
  311. type GenAlias[isInt: static bool] = (
  312. when isInt:
  313. int
  314. else:
  315. float
  316. )
  317. doAssert GenAlias[true] is int
  318. doAssert GenAlias[false] is float
  319. proc foo(T: static bool): GenAlias[T] = discard
  320. doAssert foo(true) is int
  321. doAssert foo(false) is float
  322. proc foo[T: static bool](v: var GenAlias[T]) =
  323. v += 1
  324. var x: int
  325. foo[true](x)
  326. doAssert not compiles(foo[false](x))
  327. foo[true](x)
  328. doAssert x == 2
  329. var y: float
  330. foo[false](y)
  331. doAssert not compiles(foo[true](y))
  332. foo[false](y)
  333. doAssert y == 2
  334. block: # `when`, test no constant semchecks
  335. type Foo[T] = (
  336. when false:
  337. {.error: "bad".}
  338. elif defined(neverDefined):
  339. {.error: "bad 2".}
  340. else:
  341. T
  342. )
  343. var x: Foo[int]
  344. type Bar[T] = (
  345. when true:
  346. T
  347. elif defined(js):
  348. {.error: "bad".}
  349. else:
  350. {.error: "bad 2".}
  351. )
  352. var y: Bar[int]
  353. block: # weird regression
  354. type
  355. Foo[T] = distinct int
  356. Bar[T, U] = distinct int
  357. proc foo[T, U](x: static Foo[T], y: static Bar[T, U]): Foo[T] =
  358. # signature gives:
  359. # Error: cannot instantiate Bar
  360. # got: <typedesc[T], U>
  361. # but expected: <T, U>
  362. x
  363. doAssert foo(Foo[int](1), Bar[int, int](2)).int == 1
  364. block: # issue #24090
  365. type M[V] = object
  366. template y[V](N: type M, v: V): M[V] = default(M[V])
  367. proc d(x: int | int, f: M[int] = M.y(0)) = discard
  368. d(0, M.y(0))
  369. type Foo[T] = object
  370. x: typeof(M.y(default(T)))
  371. var a: Foo[int]
  372. doAssert a.x is M[int]
  373. var b: Foo[float]
  374. doAssert b.x is M[float]
  375. # related to #24091:
  376. doAssert not (compiles do:
  377. type Bar[T] = object
  378. # ideally fails here immediately since the inside of `typeof` does not
  379. # depend on an unresolved parameter
  380. # but if typechecking gets too lazy, then we need to instantiate to error
  381. x: typeof(M()))
  382. doAssert not (compiles do:
  383. type Bar[T] = object
  384. # again, ideally fails here immediately
  385. x: typeof(default(M)))
  386. proc foo[T: M](x: T = default(T)) = discard x
  387. foo[M[int]]()
  388. doAssert not compiles(foo())
  389. block: # above but encountered by sigmatch using replaceTypeVarsN
  390. type Opt[T] = object
  391. x: T
  392. proc none[T](x: type Opt, y: typedesc[T]): Opt[T] = discard
  393. proc foo[T](x: T, a = Opt.none(int)) = discard
  394. foo(1, a = Opt.none(int))
  395. foo(1)
  396. block: # real version of above
  397. type Opt[T] = object
  398. x: T
  399. template none(x: type Opt, T: type): Opt[T] = Opt[T]()
  400. proc foo[T](x: T, a = Opt.none(int)) = discard
  401. foo(1, a = Opt.none(int))
  402. foo(1)
  403. block: # issue #20880
  404. type
  405. Child[n: static int] = object
  406. data: array[n, int]
  407. Parent[n: static int] = object
  408. child: Child[3*n]
  409. const n = 3
  410. doAssert $(typeof Parent[n*3]()) == "Parent[9]"
  411. doAssert $(typeof Parent[1]().child) == "Child[3]"
  412. doAssert Parent[1]().child.data.len == 3
  413. {.experimental: "dynamicBindSym".}
  414. block: # issue #16774
  415. type SecretWord = distinct uint64
  416. const WordBitWidth = 8 * sizeof(uint64)
  417. func wordsRequired(bits: int): int {.compileTime.} =
  418. ## Compute the number of limbs required
  419. # from the **announced** bit length
  420. (bits + WordBitWidth - 1) div WordBitWidth
  421. type
  422. Curve = enum BLS12_381
  423. BigInt[bits: static int] = object
  424. limbs: array[bits.wordsRequired, SecretWord]
  425. const BLS12_381_Modulus = default(BigInt[381])
  426. macro Mod(C: static Curve): untyped =
  427. ## Get the Modulus associated to a curve
  428. result = bindSym($C & "_Modulus")
  429. macro getCurveBitwidth(C: static Curve): untyped =
  430. result = nnkDotExpr.newTree(
  431. getAST(Mod(C)),
  432. ident"bits"
  433. )
  434. type Fp[C: static Curve] = object
  435. ## Finite Fields / Modular arithmetic
  436. ## modulo the curve modulus
  437. mres: BigInt[getCurveBitwidth(C)]
  438. var x: Fp[BLS12_381]
  439. doAssert x.mres.limbs.len == wordsRequired(getCurveBitWidth(BLS12_381))
  440. # minimized, as if we haven't tested it already:
  441. macro makeIntLit(c: static int): untyped =
  442. result = newLit(c)
  443. type Test[T: static int] = object
  444. myArray: array[makeIntLit(T), int]
  445. var y: Test[2]
  446. doAssert y.myArray.len == 2
  447. var z: Test[4]
  448. doAssert z.myArray.len == 4
  449. block: # issue #16175
  450. type
  451. Thing[D: static uint] = object
  452. when D == 0:
  453. kid: char
  454. else:
  455. kid: Thing[D-1]
  456. var t2 = Thing[3]()
  457. doAssert t2.kid is Thing[2.uint]
  458. doAssert t2.kid.kid is Thing[1.uint]
  459. doAssert t2.kid.kid.kid is Thing[0.uint]
  460. doAssert t2.kid.kid.kid.kid is char
  461. var s = Thing[1]()
  462. doAssert s.kid is Thing[0.uint]
  463. doAssert s.kid.kid is char
  464. block: # issue #23287
  465. template emitTupleType(trait: typedesc): untyped =
  466. trait
  467. type
  468. Traitor[Traits] = ref object of RootObj ##
  469. vtable: emitTupleType(Traits)
  470. type Generic[X] = object
  471. proc test2[Traits](val: Traitor[Generic[Traits]]) =
  472. static: assert val.vtable is Generic[int]
  473. proc test[X](val: Traitor[Generic[X]]) = discard
  474. test2 Traitor[Generic[int]]() # This should error, but passes
  475. test Traitor[Generic[int]]()
  476. block: # issue #20367, example 1
  477. template someTemp(T:type):typedesc = T
  478. type
  479. Foo[T2] = someTemp(T2)
  480. Bar[T1] = Foo[T1]
  481. var u:Foo[float] # works
  482. var v:Bar[float] # Error: invalid type: 'None' in this context: 'Bar[system.float]' for var
  483. block: # issue #20367, example 2
  484. template someOtherTemp(p:static[int]):untyped = array[p,int]
  485. type
  486. Foo2[n:static[int]] = someOtherTemp(n)
  487. Bar2[m:static[int]] = Foo2[m]
  488. var x:Foo2[1] # works
  489. var y:Bar2[1] # Error: undeclared identifier: 'n'