Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12785 #12943

Merged
merged 2 commits into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 47 additions & 0 deletions tests/casestmt/t12785.nim
Original file line number Diff line number Diff line change
@@ -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())