From 52cf7dfde01ca70e867607c2df7de66263c51025 Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 18 Oct 2024 08:37:05 +0300 Subject: [PATCH] shallow fold prevention for `addr`, `nkHiddenAddr` (#24322) fixes #24305, refs #23807 Since #23014 `nkHiddenAddr` is produced to fast assign array elements in iterators. However the array access inside this `nkHiddenAddr` can get folded at compile time, generating invalid code. In #23807, compile time folding of regular `addr` expressions was changed to be prevented in `transf` but `nkHiddenAddr` was not updated alongside it. The method for preventing folding in `addr` in #23807 was also faulty, it should only trigger on the immediate child node of the address rather than all nodes nested inside it. This caused a regression as outlined in [this comment](https://github.com/nim-lang/Nim/pull/24322#issuecomment-2419560182). To fix both issues, `addr` and `nkHiddenAddr` now both shallowly prevent constant folding for their immediate children. --- compiler/transf.nim | 22 +++++++++------------- tests/iter/tfoldedaddr.nim | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 tests/iter/tfoldedaddr.nim diff --git a/compiler/transf.nim b/compiler/transf.nim index cfeff18cac..7ee9683743 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -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 @@ -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..