fixes #22787; marks var section in the loop as reassign preventing cursor (#22800)

fixes #22787

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
(cherry picked from commit efa64aa49b)
This commit is contained in:
ringabout
2023-10-07 13:43:39 +08:00
committed by narimiran
parent 4f78a4dd3e
commit 08d37c2d7e
2 changed files with 41 additions and 0 deletions

View File

@@ -820,6 +820,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])

37
tests/arc/t22787.nim Normal file
View File

@@ -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()