Skip to content

Commit

Permalink
make fold prevention shallow
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Oct 17, 2024
1 parent 34ae295 commit db604a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
25 changes: 9 additions & 16 deletions compiler/transf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type
contSyms, breakSyms: seq[PSym] # to transform 'continue' and 'break'
deferDetected, tooEarly: bool
isIntroducingNewLocalVars: bool # true if we are in `introducingNewLocalVars` (don't transform yields)
inAddr: bool
flags: TransformFlags
graph: ModuleGraph
idgen: IdGenerator
Expand Down Expand Up @@ -103,12 +102,12 @@ proc newTemp(c: PTransf, typ: PType, info: TLineInfo): PNode =
else:
result = newSymNode(r)

proc transform(c: PTransf, n: PNode): PNode
proc transform(c: PTransf, n: PNode, noConstFold = false): PNode

proc transformSons(c: PTransf, n: PNode): PNode =
proc transformSons(c: PTransf, n: PNode, noConstFold = false): PNode =
result = newTransNode(n)
for i in 0..<n.len:
result[i] = transform(c, n[i])
result[i] = transform(c, n[i], noConstFold)

proc newAsgnStmt(c: PTransf, kind: TNodeKind, le: PNode, ri: PNode; isFirstWrite: bool): PNode =
result = newTransNode(kind, ri.info, 2)
Expand Down Expand Up @@ -481,8 +480,8 @@ proc transformYield(c: PTransf, n: PNode): PNode =
result.add(introduceNewLocalVars(c, c.transCon.forLoopBody))
c.isIntroducingNewLocalVars = false

proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds): PNode =
result = transformSons(c, n)
proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds, isAddr = false): PNode =
result = transformSons(c, n, noConstFold = isAddr)
# inlining of 'var openarray' iterators; bug #19977
if n.typ.kind != tyOpenArray and (c.graph.config.backend == backendCpp or sfCompileToCpp in c.module.flags): return
var n = result
Expand Down Expand Up @@ -1010,7 +1009,7 @@ proc transformDerefBlock(c: PTransf, n: PNode): PNode =
result[i] = e0[i]
result[e0.len-1] = newTreeIT(nkHiddenDeref, n.info, n.typ, e0[e0.len-1])

proc transform(c: PTransf, n: PNode): PNode =
proc transform(c: PTransf, n: PNode, noConstFold = false): PNode =
when false:
var oldDeferAnchor: PNode
if n.kind in {nkElifBranch, nkOfBranch, nkExceptBranch, nkElifExpr,
Expand Down Expand Up @@ -1077,15 +1076,9 @@ proc transform(c: PTransf, n: PNode): PNode =
of nkCallKinds:
result = transformCall(c, n)
of nkHiddenAddr:
let oldInAddr = c.inAddr
c.inAddr = true
result = transformAddrDeref(c, n, {nkHiddenDeref})
c.inAddr = oldInAddr
result = transformAddrDeref(c, n, {nkHiddenDeref}, isAddr = true)
of nkAddr:
let oldInAddr = c.inAddr
c.inAddr = true
result = transformAddrDeref(c, n, {nkDerefExpr, nkHiddenDeref})
c.inAddr = oldInAddr
result = transformAddrDeref(c, n, {nkDerefExpr, nkHiddenDeref}, isAddr = true)
of nkDerefExpr:
result = transformAddrDeref(c, n, {nkAddr, nkHiddenAddr})
of nkHiddenDeref:
Expand Down Expand Up @@ -1165,7 +1158,7 @@ proc transform(c: PTransf, n: PNode): PNode =
let exprIsPointerCast = n.kind in {nkCast, nkConv, nkHiddenStdConv} and
n.typ != nil and
n.typ.kind == tyPointer
if not exprIsPointerCast and not c.inAddr:
if not exprIsPointerCast and not noConstFold:
var cnst = getConstExpr(c.module, result, c.idgen, c.graph)
# we inline constants if they are not complex constants:
if cnst != nil and not dontInlineConstant(n, cnst):
Expand Down
6 changes: 6 additions & 0 deletions tests/iter/tfoldedaddr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ block: # issue #24305 with array

for k in demo([17]):
echo k

block: # related regression
proc main =
let a = [0, 1, 2]
let x = addr a[low(a)]
main()

0 comments on commit db604a8

Please sign in to comment.