Skip to content

Commit

Permalink
fixes #22787; marks var section in the loop as reassign preventing …
Browse files Browse the repository at this point in the history
…cursor (#22800)

fixes #22787

---------

Co-authored-by: Andreas Rumpf <[email protected]>
  • Loading branch information
ringabout and Araq authored Oct 7, 2023
1 parent f111009 commit efa64aa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/varpartitions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,10 @@ proc computeLiveRanges(c: var Partitions; n: PNode) =
registerVariable(c, child[i])
#deps(c, child[i], last)

if c.inLoop > 0 and child[0].kind == nkSym: # bug #22787
let vid = variableId(c, child[0].sym)
if child[^1].kind != nkEmpty:
markAsReassigned(c, vid)
of nkAsgn, nkFastAsgn, nkSinkAsgn:
computeLiveRanges(c, n[0])
computeLiveRanges(c, n[1])
Expand Down
37 changes: 37 additions & 0 deletions tests/arc/t22787.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
discard """
joinable: false
"""

import std/assertions

proc foo =
var s:seq[string]
var res = ""

for i in 0..3:
s.add ("test" & $i)
s.add ("test" & $i)

var lastname:string

for i in s:
var name = i[0..4]

if name != lastname:
res.add "NEW:" & name & "\n"
else:
res.add name & ">" & lastname & "\n"

lastname = name

doAssert res == """
NEW:test0
test0>test0
NEW:test1
test1>test1
NEW:test2
test2>test2
NEW:test3
test3>test3
"""
foo()

0 comments on commit efa64aa

Please sign in to comment.