tuninstantiatedgenericcalls.nim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 #22342, type section version of #22607
  307. type GenAlias[isInt: static bool] = (
  308. when isInt:
  309. int
  310. else:
  311. float
  312. )
  313. doAssert GenAlias[true] is int
  314. doAssert GenAlias[false] is float
  315. proc foo(T: static bool): GenAlias[T] = discard
  316. doAssert foo(true) is int
  317. doAssert foo(false) is float
  318. proc foo[T: static bool](v: var GenAlias[T]) =
  319. v += 1
  320. var x: int
  321. foo[true](x)
  322. doAssert not compiles(foo[false](x))
  323. foo[true](x)
  324. doAssert x == 2
  325. var y: float
  326. foo[false](y)
  327. doAssert not compiles(foo[true](y))
  328. foo[false](y)
  329. doAssert y == 2
  330. block: # `when`, test no constant semchecks
  331. type Foo[T] = (
  332. when false:
  333. {.error: "bad".}
  334. elif defined(neverDefined):
  335. {.error: "bad 2".}
  336. else:
  337. T
  338. )
  339. var x: Foo[int]
  340. type Bar[T] = (
  341. when true:
  342. T
  343. elif defined(js):
  344. {.error: "bad".}
  345. else:
  346. {.error: "bad 2".}
  347. )
  348. var y: Bar[int]
  349. block: # weird regression
  350. type
  351. Foo[T] = distinct int
  352. Bar[T, U] = distinct int
  353. proc foo[T, U](x: static Foo[T], y: static Bar[T, U]): Foo[T] =
  354. # signature gives:
  355. # Error: cannot instantiate Bar
  356. # got: <typedesc[T], U>
  357. # but expected: <T, U>
  358. x
  359. doAssert foo(Foo[int](1), Bar[int, int](2)).int == 1
  360. block: # issue #24090
  361. type M[V] = object
  362. template y[V](N: type M, v: V): M[V] = default(M[V])
  363. proc d(x: int | int, f: M[int] = M.y(0)) = discard
  364. d(0, M.y(0))
  365. type Foo[T] = object
  366. x: typeof(M.y(default(T)))
  367. var a: Foo[int]
  368. doAssert a.x is M[int]
  369. var b: Foo[float]
  370. doAssert b.x is M[float]
  371. doAssert not (compiles do:
  372. type Bar[T] = object
  373. x: typeof(M()) # actually fails here immediately
  374. var bar: Bar[int])
  375. doAssert not (compiles do:
  376. type Bar[T] = object
  377. x: typeof(default(M))
  378. var bar: Bar[int]
  379. # gives "undeclared identifier x" because of #24091,
  380. # normally it should fail in the line above
  381. echo bar.x)
  382. proc foo[T: M](x: T = default(T)) = discard x
  383. foo[M[int]]()
  384. doAssert not compiles(foo())
  385. block: # above but encountered by sigmatch using replaceTypeVarsN
  386. type Opt[T] = object
  387. x: T
  388. proc none[T](x: type Opt, y: typedesc[T]): Opt[T] = discard
  389. proc foo[T](x: T, a = Opt.none(int)) = discard
  390. foo(1, a = Opt.none(int))
  391. foo(1)
  392. block: # real version of above
  393. type Opt[T] = object
  394. x: T
  395. template none(x: type Opt, T: type): Opt[T] = Opt[T]()
  396. proc foo[T](x: T, a = Opt.none(int)) = discard
  397. foo(1, a = Opt.none(int))
  398. foo(1)
  399. block: # issue #20880
  400. type
  401. Child[n: static int] = object
  402. data: array[n, int]
  403. Parent[n: static int] = object
  404. child: Child[3*n]
  405. const n = 3
  406. doAssert $(typeof Parent[n*3]()) == "Parent[9]"
  407. doAssert $(typeof Parent[1]().child) == "Child[3]"
  408. doAssert Parent[1]().child.data.len == 3
  409. {.experimental: "dynamicBindSym".}
  410. block: # issue #16774
  411. type SecretWord = distinct uint64
  412. const WordBitWidth = 8 * sizeof(uint64)
  413. func wordsRequired(bits: int): int {.compileTime.} =
  414. ## Compute the number of limbs required
  415. # from the **announced** bit length
  416. (bits + WordBitWidth - 1) div WordBitWidth
  417. type
  418. Curve = enum BLS12_381
  419. BigInt[bits: static int] = object
  420. limbs: array[bits.wordsRequired, SecretWord]
  421. const BLS12_381_Modulus = default(BigInt[381])
  422. macro Mod(C: static Curve): untyped =
  423. ## Get the Modulus associated to a curve
  424. result = bindSym($C & "_Modulus")
  425. macro getCurveBitwidth(C: static Curve): untyped =
  426. result = nnkDotExpr.newTree(
  427. getAST(Mod(C)),
  428. ident"bits"
  429. )
  430. type Fp[C: static Curve] = object
  431. ## Finite Fields / Modular arithmetic
  432. ## modulo the curve modulus
  433. mres: BigInt[getCurveBitwidth(C)]
  434. var x: Fp[BLS12_381]
  435. doAssert x.mres.limbs.len == wordsRequired(getCurveBitWidth(BLS12_381))
  436. # minimized, as if we haven't tested it already:
  437. macro makeIntLit(c: static int): untyped =
  438. result = newLit(c)
  439. type Test[T: static int] = object
  440. myArray: array[makeIntLit(T), int]
  441. var y: Test[2]
  442. doAssert y.myArray.len == 2
  443. var z: Test[4]
  444. doAssert z.myArray.len == 4
  445. block: # issue #16175
  446. type
  447. Thing[D: static uint] = object
  448. when D == 0:
  449. kid: char
  450. else:
  451. kid: Thing[D-1]
  452. var t2 = Thing[3]()
  453. doAssert t2.kid is Thing[2.uint]
  454. doAssert t2.kid.kid is Thing[1.uint]
  455. doAssert t2.kid.kid.kid is Thing[0.uint]
  456. doAssert t2.kid.kid.kid.kid is char
  457. var s = Thing[1]()
  458. doAssert s.kid is Thing[0.uint]
  459. doAssert s.kid.kid is char