Skip to content

Commit 9b8afd1

Browse files
RSDuckAraq
authored andcommitted
Fix #12785 (#12943)
* Fix #12785 and add test * better variable name
1 parent 10bd7d8 commit 9b8afd1

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

compiler/ccgstmts.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ iterator fieldValuePairs(n: PNode): tuple[memberSym, valueSym: PNode] =
492492

493493
proc genComputedGoto(p: BProc; n: PNode) =
494494
# first pass: Generate array of computed labels:
495+
496+
# flatten the loop body because otherwise let and var sections
497+
# wrapped inside stmt lists by inject destructors won't be recognised
498+
let n = n.flattenStmts()
495499
var casePos = -1
496500
var arraySize: int
497501
for i in 0..<n.len:

tests/casestmt/t12785.nim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
discard """
2+
cmd: '''nim c --newruntime $file'''
3+
output: '''copied
4+
copied
5+
2
6+
copied
7+
copied
8+
2
9+
destroyed
10+
destroyed'''
11+
"""
12+
13+
type
14+
ObjWithDestructor = object
15+
a: int
16+
proc `=destroy`(self: var ObjWithDestructor) =
17+
echo "destroyed"
18+
19+
proc `=`(self: var ObjWithDestructor, other: ObjWithDestructor) =
20+
echo "copied"
21+
22+
proc test(a: range[0..1], arg: ObjWithDestructor) =
23+
var iteration = 0
24+
while true:
25+
{.computedGoto.}
26+
27+
let
28+
b = int(a) * 2
29+
c = a
30+
d = arg
31+
e = arg
32+
33+
discard c
34+
discard d
35+
discard e
36+
37+
inc iteration
38+
39+
case a
40+
of 0:
41+
assert false
42+
of 1:
43+
echo b
44+
if iteration == 2:
45+
break
46+
47+
test(1, ObjWithDestructor())

0 commit comments

Comments
 (0)