6565 nodeStack: seq [PNode ]
6666 # symStack: seq[PSym]
6767 typStack: seq [PType ]
68+ conf: ConfigRef # used to print the line info when there is a mismatch.
6869
6970# Compare PType, PSym and PNode but ignores fields nifencoder and nifdecoder doesn't support
7071# Assumes `x` is generated by sem.nim and `y` is decoded by icnif/nifdecoder.
@@ -92,6 +93,8 @@ proc eql(x, y: PType; c: var EqlContext): bool =
9293 c.typStack.add x
9394 if not eql (x.n, y.n, c):
9495 echo " type.n mismatch"
96+ debug (x.n)
97+ debug (y.n)
9598 result = false
9699 elif x.kidsLen != y.kidsLen:
97100 echo " type kidsLen mismatch"
@@ -145,6 +148,8 @@ proc eql(x, y: PNode; c: var EqlContext): bool =
145148 result = false
146149 elif x.flags != y.flags:
147150 echo " node flag mismatch: " , x.flags, " /" , y.flags
151+ debug (x)
152+ debug (y)
148153 result = false
149154 elif x.safeLen == y.safeLen:
150155 if c.nodeStack.len != 0 :
@@ -153,43 +158,53 @@ proc eql(x, y: PNode; c: var EqlContext): bool =
153158 # echo "cycle is detected in PNode"
154159 return true
155160 c.nodeStack.add x
156- case x.kind:
157- of nkSym:
158- result = eql (x.sym, y.sym, c)
159- if not result :
160- echo " Symbol mismatch:"
161- debug (x.sym)
162- debug (y.sym)
163- debug (x.sym.typ)
164- debug (y.sym.typ)
165- of nkCharLit .. nkTripleStrLit:
166- result = sameValue (x, y)
167- of nkIdentDefs:
168- assert x.len == 3 and y.len == 3
169- if eql (x[0 ], y[0 ], c) and eql (x[2 ], y[2 ], c) and y[1 ].kind == nkEmpty and y[0 ].sym.typ != nil :
170- result = true
171- else :
172- echo " nkIdentDefs mismatch"
173- result = false
174- of nkTypeDef:
175- assert x.len == 3 and y.len == 3
176- if eql (x[0 ], y[0 ], c) and eql (x[1 ], y[1 ], c):
177- let sym = if y[0 ].kind == nkPostfix:
178- y[0 ][1 ].sym
179- else :
180- y[0 ].sym
181- result = y[2 ].kind == nkEmpty and sym.typ != nil
182- else :
183- result = false
184- if not result :
185- echo " nkTypeDef mismatch"
161+ if x.kind != nkNilLit and not eql (x.typ, y.typ, c):
162+ # ignore node type if it is nil literal.
163+ # current nifencoder doesn't encode the type of nil lit.
164+ echo " PNode type mismatch at " , `$` (c.conf, x.info), " :"
165+ debug (x)
166+ debug (y)
167+ debug (x.typ)
168+ debug (y.typ)
169+ result = false
186170 else :
187- result = true
188- for i in 0 ..< x.safeLen:
189- if not eql (x[i], y[i], c):
171+ case x.kind:
172+ of nkSym:
173+ result = eql (x.sym, y.sym, c)
174+ if not result :
175+ echo " Symbol mismatch:"
176+ debug (x.sym)
177+ debug (y.sym)
178+ debug (x.sym.typ)
179+ debug (y.sym.typ)
180+ of nkCharLit .. nkTripleStrLit:
181+ result = sameValue (x, y)
182+ of nkIdentDefs:
183+ assert x.len == 3 and y.len == 3
184+ if eql (x[0 ], y[0 ], c) and eql (x[2 ], y[2 ], c) and y[1 ].kind == nkEmpty and y[0 ].sym.typ != nil :
185+ result = true
186+ else :
187+ echo " nkIdentDefs mismatch"
190188 result = false
191- break
192- discard c.nodeStack.pop
189+ of nkTypeDef:
190+ assert x.len == 3 and y.len == 3
191+ if eql (x[0 ], y[0 ], c) and eql (x[1 ], y[1 ], c):
192+ let sym = if y[0 ].kind == nkPostfix:
193+ y[0 ][1 ].sym
194+ else :
195+ y[0 ].sym
196+ result = y[2 ].kind == nkEmpty and sym.typ != nil
197+ else :
198+ result = false
199+ if not result :
200+ echo " nkTypeDef mismatch"
201+ else :
202+ result = true
203+ for i in 0 ..< x.safeLen:
204+ if not eql (x[i], y[i], c):
205+ result = false
206+ break
207+ discard c.nodeStack.pop
193208 else :
194209 echo " node length mismatch"
195210 debug (x)
@@ -209,6 +224,7 @@ proc testNifEncDec(graph: ModuleGraph; src: string) =
209224 let n2 = loadNifFromBuffer (nif, fullPath, graphForLoad)
210225 # debug(n2)
211226 var c = EqlContext ()
227+ c.conf = graph.config
212228 assert eql (n, n2, c)
213229
214230var conf = newConfigRefForTest ()
0 commit comments