Skip to content

Commit

Permalink
Fix comments not being ignored for exprStructuralEquivalent
Browse files Browse the repository at this point in the history
and split exprStructuralEquivalent into 3 different procs:
- exprStructuralEquivalent ignores comments and uses lax sym equality
- exprStructuralEquivalentStrictSym ignores comments but checks syms
  for strict equality
- exprStructuralEquivalentStrictSymAndComm doesn't ignore comments
  and checks syms for strict equality
  • Loading branch information
Clyybber committed Mar 11, 2024
1 parent 25eae51 commit eb0c14e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
23 changes: 13 additions & 10 deletions compiler/ast/trees.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,33 @@ template structEquiv*(
if not self(a[i], b[i]): return false
result = true

proc exprStructuralEquivalentLaxSym(a, b: PNode): bool =
structEquiv(exprStructuralEquivalentLaxSym,
proc exprStructuralEquivalent*(a, b: PNode): bool =
structEquiv(exprStructuralEquivalent,
relaxKindCheck = false,
# don't go nuts here: same symbol as string is enough:
symCheck = a.sym.name.id == b.sym.name.id,
floatCheck = sameFloatIgnoreNan(a.floatVal, b.floatVal),
commentCheck = a.comment == b.comment,
commentCheck = true,
typeCheck = true
)

proc exprStructuralEquivalentStrictSym(a, b: PNode): bool =
proc exprStructuralEquivalentStrictSym*(a, b: PNode): bool =
structEquiv(exprStructuralEquivalentStrictSym,
relaxKindCheck = false,
symCheck = a.sym == b.sym,
floatCheck = sameFloatIgnoreNan(a.floatVal, b.floatVal),
commentCheck = a.comment == b.comment,
commentCheck = true,
typeCheck = true
)

proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool =
if strictSymEquality:
exprStructuralEquivalentStrictSym(a, b)
else:
exprStructuralEquivalentLaxSym(a, b)
proc exprStructuralEquivalentStrictSymAndComm*(a, b: PNode): bool =
structEquiv(exprStructuralEquivalentStrictSymAndComm,
relaxKindCheck = false,
symCheck = a.sym == b.sym,
floatCheck = sameFloatIgnoreNan(a.floatVal, b.floatVal),
commentCheck = a.comment == b.comment,
typeCheck = true
)

proc getMagic*(op: PNode): TMagic =
if op == nil: return mNone
Expand Down
2 changes: 1 addition & 1 deletion compiler/ic/replayer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ proc replayStateChanges*(module: PSym; g: ModuleGraph) =
else:
block search:
for existing in g.cacheSeqs[destKey]:
if exprStructuralEquivalent(existing, val, strictSymEquality = true):
if exprStructuralEquivalentStrictSymAndComm(existing, val):
# comment equality DOES matter here
break search
g.cacheSeqs[destKey].add val
Expand Down
2 changes: 1 addition & 1 deletion compiler/sem/semfold.nim
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ proc evalOp*(m: TMagic, n, a, b, c: PNode; idgen: IdGenerator; g: ModuleGraph):
result.typ = n.typ
of mEqProc:
result = newIntNodeT(toInt128(ord(
exprStructuralEquivalent(a, b, strictSymEquality=true))), n, idgen, g)
exprStructuralEquivalentStrictSym(a, b))), n, idgen, g)
# comment equality doesn't matter here (getConstExpr doesn't return AST with comments)
else: discard

Expand Down
5 changes: 2 additions & 3 deletions compiler/vm/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1550,8 +1550,7 @@ proc rawExecute(c: var TCtx, t: var VmThread, pc: var int): YieldReason =
of opcEqNimNode:
decodeBC(rkInt)
regs[ra].intVal =
ord(exprStructuralEquivalent(regs[rb].nimNode, regs[rc].nimNode,
strictSymEquality=true))
ord(exprStructuralEquivalentStrictSymAndComm(regs[rb].nimNode, regs[rc].nimNode))
# comment equality DOES matter here
of opcSameNodeType:
decodeBC(rkInt)
Expand Down Expand Up @@ -2835,7 +2834,7 @@ proc rawExecute(c: var TCtx, t: var VmThread, pc: var int): YieldReason =
else:
block search:
for existing in g.cacheSeqs[destKey]:
if exprStructuralEquivalent(existing, val, strictSymEquality=true):
if exprStructuralEquivalentStrictSymAndComm(existing, val):
# comment equality DOES matter here
break search
g.cacheSeqs[destKey].add val
Expand Down

0 comments on commit eb0c14e

Please sign in to comment.