summary refs log tree commit diff stats
path: root/tests/generics
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2024-09-10 13:19:39 +0300
committerGitHub <noreply@github.com>2024-09-10 12:19:39 +0200
commitbaec1955b5453ec71fc12355142dd9a813fa02fb (patch)
tree0c5e4283b7cc857a93319b9635ce6adb7b2861fa /tests/generics
parent21771765a2c1f1fc86d87ad6e032d4050d8a651b (diff)
downloadNim-baec1955b5453ec71fc12355142dd9a813fa02fb.tar.gz
don't instantiate generic body type symbols in generic expressions (#24092)
fixes #24090

Generic body types are normally a sign of an uninstantiated type, and so
give errors when trying to instantiate them. However when instantiating
free user expressions like the nodes of `tyFromExpr`, generic default
params, static values etc, they can be used as arguments to macros or
templates etc (as in the issue). So, we don't try to instantiate generic
body type symbols at all in free expressions such as these (but not in
for example type nodes), and avoid the error.

In the future there should be a "concrete type" check for generic body
types different from the check in type instantiation to deal with things
like #24091, if we do want to allow this use of them.
Diffstat (limited to 'tests/generics')
-rw-r--r--tests/generics/tuninstantiatedgenericcalls.nim26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/generics/tuninstantiatedgenericcalls.nim b/tests/generics/tuninstantiatedgenericcalls.nim
index a349f0d2a..a1f5a0cb6 100644
--- a/tests/generics/tuninstantiatedgenericcalls.nim
+++ b/tests/generics/tuninstantiatedgenericcalls.nim
@@ -409,3 +409,29 @@ block: # weird regression
     # but expected: <T, U>
     x
   doAssert foo(Foo[int](1), Bar[int, int](2)).int == 1
+
+block: # issue #24090
+  type M[V] = object
+  template y[V](N: type M, v: V): M[V] = default(M[V])
+  proc d(x: int | int, f: M[int] = M.y(0)) = discard
+  d(0, M.y(0))
+  type Foo[T] = object
+    x: typeof(M.y(default(T)))
+  var a: Foo[int]
+  doAssert a.x is M[int]
+  var b: Foo[float]
+  doAssert b.x is M[float]
+  doAssert not (compiles do:
+    type Bar[T] = object
+      x: typeof(M()) # actually fails here immediately
+    var bar: Bar[int])
+  doAssert not (compiles do:
+    type Bar[T] = object
+      x: typeof(default(M))
+    var bar: Bar[int]
+    # gives "undeclared identifier x" because of #24091,
+    # normally it should fail in the line above
+    echo bar.x)
+  proc foo[T: M](x: T = default(T)) = discard x
+  foo[M[int]]()
+  doAssert not compiles(foo())
le='Blame the previous revision' href='/akkartik/mu/blame/html/linux/401test.mu.html?h=main&id=dc5a0acf3feea227d03a98cedf427d2aef462320'>^
372367f5 ^

d3a9db3a ^
78357b88 ^
372367f5 ^
78357b88 ^
9cbd4199 ^


78357b88 ^
9cbd4199 ^




























3350c34a ^
9cbd4199 ^
d3a9db3a ^
9cbd4199 ^
3350c34a ^
372367f5 ^
7fa80570 ^


3350c34a ^
372367f5 ^
7fa80570 ^

9cbd4199 ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73