From fe8feb46c6b69d2a2b63c83af88d0f9f03a31435 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 28 Sep 2022 19:12:08 +0800 Subject: [PATCH] fixes #19457 seqs are not properly updated in loop with ARC/ORC (#19922) * test CI * fixes #19457 * add comments Co-authored-by: sandytypical <43030857+xflywind@users.noreply.github.com> --- compiler/varpartitions.nim | 10 ++++++++++ tests/arc/t19457.nim | 16 +++++++++++++++ tests/arc/topt_refcursors.nim | 37 +++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 tests/arc/t19457.nim diff --git a/compiler/varpartitions.nim b/compiler/varpartitions.nim index 403aca38ca..4841a0c479 100644 --- a/compiler/varpartitions.nim +++ b/compiler/varpartitions.nim @@ -767,6 +767,11 @@ proc traverse(c: var Partitions; n: PNode) = # mutate(graph) # connect(graph, cursorVar) for child in n: traverse(c, child) + + if n.kind == nkWhileStmt: + traverse(c, n[0]) + # variables in while condition has longer alive time than local variables + # in the while loop body else: for child in n: traverse(c, child) @@ -854,6 +859,11 @@ proc computeLiveRanges(c: var Partitions; n: PNode) = inc c.inLoop for child in n: computeLiveRanges(c, child) dec c.inLoop + + if n.kind == nkWhileStmt: + computeLiveRanges(c, n[0]) + # variables in while condition has longer alive time than local variables + # in the while loop body of nkElifBranch, nkElifExpr, nkElse, nkOfBranch: inc c.inConditional for child in n: computeLiveRanges(c, child) diff --git a/tests/arc/t19457.nim b/tests/arc/t19457.nim new file mode 100644 index 0000000000..78447ce82a --- /dev/null +++ b/tests/arc/t19457.nim @@ -0,0 +1,16 @@ +discard """ + matrix: "--gc:refc; --gc:arc" +""" + +# bug #19457 +proc gcd(x, y: seq[int]): seq[int] = + var + a = x + b = y + while b[0] > 0: + let c = @[a[0] mod b[0]] + a = b + b = c + return a + +doAssert gcd(@[1], @[2]) == @[1] \ No newline at end of file diff --git a/tests/arc/topt_refcursors.nim b/tests/arc/topt_refcursors.nim index f097150a34..c13d81badc 100644 --- a/tests/arc/topt_refcursors.nim +++ b/tests/arc/topt_refcursors.nim @@ -5,21 +5,28 @@ discard """ var it_cursor - jt_cursor -it_cursor = root -block :tmp: - while ( - not (it_cursor == nil)): - echo [it_cursor.s] - it_cursor = it_cursor.ri -jt_cursor = root -block :tmp_1: - while ( - not (jt_cursor == nil)): - var ri_1_cursor - ri_1_cursor = jt_cursor.ri - echo [jt_cursor.s] - jt_cursor = ri_1_cursor + jt +try: + it_cursor = root + block :tmp: + while ( + not (it_cursor == nil)): + echo [it_cursor.s] + it_cursor = it_cursor.ri + `=copy`(jt, root) + block :tmp_1: + while ( + not (jt == nil)): + var ri_1 + try: + `=copy`(ri_1, jt.ri) + echo [jt.s] + `=sink`(jt, ri_1) + wasMoved(ri_1) + finally: + `=destroy`(ri_1) +finally: + `=destroy`(jt) -- end of expandArc ------------------------''' """