diff options
author | RSDuck <RSDuck@users.noreply.github.com> | 2019-12-21 07:51:19 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-12-21 07:51:19 +0100 |
commit | 9b8afd1dbb41cba16147d3c9412ef8fcd8dbee9c (patch) | |
tree | e859775adad3d25e0be585a2b8f123a9d9e20362 | |
parent | 10bd7d8fa04f1382ff3074e30acf6ba507c9586c (diff) | |
download | Nim-9b8afd1dbb41cba16147d3c9412ef8fcd8dbee9c.tar.gz |
Fix #12785 (#12943)
* Fix #12785 and add test * better variable name
-rw-r--r-- | compiler/ccgstmts.nim | 4 | ||||
-rw-r--r-- | tests/casestmt/t12785.nim | 47 |
2 files changed, 51 insertions, 0 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 34fb06af8..7473e45a6 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -492,6 +492,10 @@ iterator fieldValuePairs(n: PNode): tuple[memberSym, valueSym: PNode] = proc genComputedGoto(p: BProc; n: PNode) = # first pass: Generate array of computed labels: + + # flatten the loop body because otherwise let and var sections + # wrapped inside stmt lists by inject destructors won't be recognised + let n = n.flattenStmts() var casePos = -1 var arraySize: int for i in 0..<n.len: diff --git a/tests/casestmt/t12785.nim b/tests/casestmt/t12785.nim new file mode 100644 index 000000000..bf0f30d42 --- /dev/null +++ b/tests/casestmt/t12785.nim @@ -0,0 +1,47 @@ +discard """ + cmd: '''nim c --newruntime $file''' + output: '''copied +copied +2 +copied +copied +2 +destroyed +destroyed''' +""" + +type + ObjWithDestructor = object + a: int +proc `=destroy`(self: var ObjWithDestructor) = + echo "destroyed" + +proc `=`(self: var ObjWithDestructor, other: ObjWithDestructor) = + echo "copied" + +proc test(a: range[0..1], arg: ObjWithDestructor) = + var iteration = 0 + while true: + {.computedGoto.} + + let + b = int(a) * 2 + c = a + d = arg + e = arg + + discard c + discard d + discard e + + inc iteration + + case a + of 0: + assert false + of 1: + echo b + if iteration == 2: + break + +test(1, ObjWithDestructor()) \ No newline at end of file |