Skip to content

Commit 64cba2e

Browse files
authored
fix: for statement increment will be processed in correct context (#2839)
1 parent 721236d commit 64cba2e

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

src/compiler.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,14 @@ export class Compiler extends DiagnosticEmitter {
25952595
bodyStmts.length = 1;
25962596
}
25972597

2598+
if (condKind == ConditionKind.True) {
2599+
// Body executes at least once
2600+
flow.inherit(bodyFlow);
2601+
} else {
2602+
// Otherwise executes conditionally
2603+
flow.mergeBranch(bodyFlow);
2604+
}
2605+
25982606
// Compile the incrementor if it possibly executes
25992607
let possiblyLoops = possiblyContinues || possiblyFallsThrough;
26002608
if (possiblyLoops) {
@@ -2618,14 +2626,6 @@ export class Compiler extends DiagnosticEmitter {
26182626
}
26192627
}
26202628

2621-
// Body executes at least once
2622-
if (condKind == ConditionKind.True) {
2623-
flow.inherit(bodyFlow);
2624-
2625-
// Otherwise executes conditionally
2626-
} else {
2627-
flow.mergeBranch(bodyFlow);
2628-
}
26292629

26302630
// Finalize
26312631
outerFlow.inherit(flow);

tests/compiler/issues/2825.debug.wat

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(data $0 (i32.const 12) ",\00\00\00\00\00\00\00\00\00\00\00\02\00\00\00\1c\00\00\00i\00s\00s\00u\00e\00s\00/\002\008\002\005\00.\00t\00s\00")
1111
(table $0 1 1 funcref)
1212
(elem $0 (i32.const 1))
13+
(export "init" (func $issues/2825/init))
1314
(export "memory" (memory $0))
1415
(start $~start)
1516
(func $issues/2825/increment (param $x i32) (result i32)
@@ -49,6 +50,19 @@
4950
end
5051
end
5152
)
53+
(func $issues/2825/init
54+
(local $not_unassigned i32)
55+
loop $for-loop|0
56+
i32.const 1
57+
if
58+
i32.const 0
59+
local.set $not_unassigned
60+
local.get $not_unassigned
61+
drop
62+
br $for-loop|0
63+
end
64+
end
65+
)
5266
(func $~start
5367
call $start:issues/2825
5468
)

tests/compiler/issues/2825.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{}
1+
{}

tests/compiler/issues/2825.release.wat

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
(module
2-
(type $0 (func (param i32 i32 i32 i32)))
3-
(type $1 (func))
2+
(type $0 (func))
3+
(type $1 (func (param i32 i32 i32 i32)))
44
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
55
(memory $0 1)
66
(data $0 (i32.const 1036) ",")
77
(data $0.1 (i32.const 1048) "\02\00\00\00\1c\00\00\00i\00s\00s\00u\00e\00s\00/\002\008\002\005\00.\00t\00s")
8+
(export "init" (func $issues/2825/init))
89
(export "memory" (memory $0))
910
(start $~start)
11+
(func $issues/2825/init
12+
loop $for-loop|0
13+
br $for-loop|0
14+
end
15+
unreachable
16+
)
1017
(func $~start
1118
(local $0 i32)
1219
loop $for-loop|0

tests/compiler/issues/2825.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ function increment(x: i32): i32 {
55

66
for (let i = 0; i < 10; i = increment(i)) {
77
let i = 1234;
8-
}
8+
}
9+
10+
export function init(): void {
11+
let not_unassigned: i32;
12+
for (; ; not_unassigned) {
13+
not_unassigned = 0;
14+
}
15+
}

0 commit comments

Comments
 (0)