summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorjcosborn <jcosborn@users.noreply.github.com>2017-05-14 08:08:41 -0500
committerAndreas Rumpf <rumpf_a@web.de>2017-05-14 15:08:41 +0200
commitec50dab57d91fb33380a39479bafbeae48ed1a83 (patch)
treeb0036cc43b223aaaee2049fe66ff67a10653a917
parentb826f85731fb20cbdc00a2ab5a64ec852251c56d (diff)
downloadNim-ec50dab57d91fb33380a39479bafbeae48ed1a83.tar.gz
fixed macros.getTypeImpl for a symbol of type tyGenericBody (#5788) (#5812)
* fixed macros.getTypeImpl for a symbol of type tyGenericBody
* added test case and commented test code
-rw-r--r--compiler/vmdeps.nim7
-rw-r--r--tests/macros/tgettypeinst.nim65
-rw-r--r--web/news/e031_version_0_16_2.rst2
3 files changed, 55 insertions, 19 deletions
diff --git a/compiler/vmdeps.nim b/compiler/vmdeps.nim
index 8c7388643..b2b1ec92b 100644
--- a/compiler/vmdeps.nim
+++ b/compiler/vmdeps.nim
@@ -175,7 +175,12 @@ proc mapTypeToAstX(t: PType; info: TLineInfo;
           result.add mapTypeToAst(t.sons[i], info)
     else:
       result = mapTypeToAstX(t.lastSon, info, inst, allowRecursion)
-  of tyGenericBody, tyOrdinal:
+  of tyGenericBody:
+    if inst:
+      result = mapTypeToAstX(t.lastSon, info, inst, true)
+    else:
+      result = mapTypeToAst(t.lastSon, info)
+  of tyOrdinal:
     result = mapTypeToAst(t.lastSon, info)
   of tyDistinct:
     if inst:
diff --git a/tests/macros/tgettypeinst.nim b/tests/macros/tgettypeinst.nim
index 255eff949..f89aa5e0b 100644
--- a/tests/macros/tgettypeinst.nim
+++ b/tests/macros/tgettypeinst.nim
@@ -21,49 +21,67 @@ proc symToIdent(x: NimNode): NimNode =
       for c in x:
         result.add symToIdent(c)
 
+# check getTypeInst and getTypeImpl for given symbol x
 macro testX(x,inst0: typed; recurse: static[bool]; implX: stmt): typed =
+  # check that getTypeInst(x) equals inst0
   let inst = x.getTypeInst
-  let impl = x.getTypeImpl
-  let inst0r = inst0.symToIdent.treeRepr
   let instr = inst.symToIdent.treeRepr
-  #echo inst0r
+  let inst0r = inst0.symToIdent.treeRepr
   #echo instr
+  #echo inst0r
   doAssert(instr == inst0r)
+
+  # check that getTypeImpl(x) is correct
+  #  if implX is nil then compare to inst0
+  #  else we expect implX to be a type definition
+  #       and we extract the implementation from that
+  let impl = x.getTypeImpl
   var impl0 =
     if implX.kind == nnkNilLit: inst0
     else: implX[0][2]
-  let impl0r = impl0.symToIdent.treerepr
   let implr = impl.symToIdent.treerepr
-  #echo impl0r
+  let impl0r = impl0.symToIdent.treerepr
   #echo implr
+  #echo impl0r
   doAssert(implr == impl0r)
-  template echoString(s:string) = echo s.replace("\n","\n  ")
+
   result = newStmtList()
+  #template echoString(s: string) = echo s.replace("\n","\n  ")
   #result.add getAst(echoString("  " & inst0.repr))
   #result.add getAst(echoString("  " & inst.repr))
   #result.add getAst(echoString("  " & impl0.repr))
   #result.add getAst(echoString("  " & impl.repr))
+
   if recurse:
-    template testDecl(n, m :typed) =
+    # now test using a newly formed variable of type getTypeInst(x)
+    template testDecl(n,m: typed) =
       testV(n, false):
         type _ = m
     result.add getAst(testDecl(inst.symToIdent, impl.symToIdent))
 
+# test with a variable (instance) of type
 template testV(inst, recurse, impl) =
   block:
     #echo "testV(" & astToStr(inst) & ", " & $recurse & "):" & astToStr(impl)
     var x: inst
     testX(x, inst, recurse, impl)
-template testT(inst, recurse) =
+
+# test with a newly created typedesc (myType)
+# using the passed type as the implementation
+template testT(impl, recurse) =
   block:
-    type myType = inst
+    type myType = impl
     testV(myType, recurse):
-      type _ = inst
+      type _ = impl
 
+# test a built-in type whose instance is equal to the implementation
 template test(inst) =
   testT(inst, false)
   testV(inst, true, nil)
-template test(inst, impl) = testV(inst, true, impl)
+
+# test a custom type with provided implementation
+template test(inst, impl) =
+  testV(inst, true, impl)
 
 type
   Model = object of RootObj
@@ -87,9 +105,12 @@ type
     value:T
   Foo[N:static[int],T] = object
   Bar[N:static[int],T] = object
-    #baz:Foo[N+1,GenericObject[T]]
+    #baz:Foo[N+1,GenericObject[T]]  # currently fails
     baz:Foo[N,GenericObject[T]]
 
+  Generic[T] = seq[int]
+  Concrete = Generic[int]
+
 test(bool)
 test(char)
 test(int)
@@ -97,13 +118,17 @@ test(float)
 test(ptr int)
 test(ref int)
 test(array[1..10,Bar[2,Foo[3,float]]])
+test(array[MyEnum,Bar[2,Foo[3,float]]])
 test(distinct Bar[2,Foo[3,float]])
 test(tuple[a:int,b:Foo[-1,float]])
-#test(MyEnum):
-#  type _ = enum
-#    valueA, valueB, valueC
-test(set[MyEnum])
 test(seq[int])
+test(set[MyEnum])
+test(proc (a: int, b: Foo[2,float]))
+test(proc (a: int, b: Foo[2,float]): Bar[3,int])
+
+test(MyEnum):
+  type _ = enum
+    valueA, valueB, valueC
 test(Bar[2,Foo[3,float]]):
   type _ = object
     baz: Foo[2, GenericObject[Foo[3, float]]]
@@ -118,8 +143,12 @@ test(Tree):
     value: int
     left: ref Tree
     right: ref Tree
-test(proc (a: int, b: Foo[2,float]))
-test(proc (a: int, b: Foo[2,float]): Bar[3,int])
+test(Concrete):
+  type _ = Generic[int]
+test(Generic[int]):
+  type _ = seq[int]
+test(Generic[float]):
+  type _ = seq[int]
 
 # bug #4862
 static:
diff --git a/web/news/e031_version_0_16_2.rst b/web/news/e031_version_0_16_2.rst
index 63884700f..4f49bd8d9 100644
--- a/web/news/e031_version_0_16_2.rst
+++ b/web/news/e031_version_0_16_2.rst
@@ -45,6 +45,8 @@ Changes affecting backwards compatibility
   AST that is the same as what is used to define an enum.  Previously the AST
   returned had a repeated ``EnumTy`` node and was missing the initial pragma
   node (which is currently empty for an enum).
+- ``macros.getTypeImpl`` now correctly returns the implementation for a symbol
+  of type ``tyGenericBody``.
 - If the dispatcher parameter's value used in multi method is ``nil``,
   a ``NilError`` exception is raised. The old behavior was that the method
   would be a ``nop`` then.
v1.5.1&id=2ca2e862e616eba862ac80f5ee0003d1aa984a32'>^
34a60763 ^
4c13e1f2 ^




34a60763 ^



4c13e1f2 ^

34a60763 ^
4c13e1f2 ^


34a60763 ^
4c13e1f2 ^



34a60763 ^
4c13e1f2 ^

34a60763 ^
4c13e1f2 ^





34a60763 ^

4c13e1f2 ^

34a60763 ^
4c13e1f2 ^

c776804d ^
4c13e1f2 ^




34a60763 ^
4c13e1f2 ^


























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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239