mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-22 00:41:28 +00:00
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.
This commit is contained in:
32
tests/iter/tfoldedaddr.nim
Normal file
32
tests/iter/tfoldedaddr.nim
Normal file
@@ -0,0 +1,32 @@
|
||||
discard """
|
||||
output: '''
|
||||
23
|
||||
23
|
||||
23
|
||||
23
|
||||
23
|
||||
23
|
||||
'''
|
||||
"""
|
||||
|
||||
block: # issue #24305
|
||||
iterator demo(a: openArray[int]): int =
|
||||
for k in countUp(a[0], 19):
|
||||
yield 23
|
||||
|
||||
for k in demo(@[17]):
|
||||
echo k
|
||||
|
||||
block: # issue #24305 with array
|
||||
iterator demo(a: array[1, int]): int =
|
||||
for k in countUp(a[0], 19):
|
||||
yield 23
|
||||
|
||||
for k in demo([17]):
|
||||
echo k
|
||||
|
||||
block: # related regression
|
||||
proc main =
|
||||
let a = [0, 1, 2]
|
||||
let x = addr a[low(a)]
|
||||
main()
|
||||
Reference in New Issue
Block a user