Skip to content

Commit 7b501b2

Browse files
authored
Fix invalid flow assumption in 'if' compilation (#1244)
1 parent 465f995 commit 7b501b2

File tree

4 files changed

+149
-5
lines changed

4 files changed

+149
-5
lines changed

src/compiler.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,11 +2581,7 @@ export class Compiler extends DiagnosticEmitter {
25812581
}
25822582
elseFlow.freeScopedLocals();
25832583
this.currentFlow = flow;
2584-
if (elseTerminates && !thenTerminates) {
2585-
flow.inherit(thenFlow);
2586-
} else {
2587-
flow.inheritMutual(thenFlow, elseFlow);
2588-
}
2584+
flow.inheritMutual(thenFlow, elseFlow);
25892585
return module.if(condExpr,
25902586
module.flatten(thenStmts),
25912587
module.flatten(elseStmts)

tests/compiler/while.optimized.wat

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,36 @@
14151415
call $~lib/builtins/abort
14161416
unreachable
14171417
end
1418+
i32.const 0
1419+
global.set $while/ran
1420+
i32.const 0
1421+
local.set $3
1422+
loop $while-continue|067
1423+
local.get $3
1424+
i32.const 1
1425+
i32.add
1426+
local.tee $3
1427+
i32.const 1
1428+
i32.lt_s
1429+
br_if $while-continue|067
1430+
end
1431+
i32.const 1
1432+
global.set $while/ran
1433+
i32.const 0
1434+
global.set $while/ran
1435+
i32.const 0
1436+
local.set $3
1437+
loop $while-continue|08
1438+
local.get $3
1439+
i32.const 1
1440+
i32.add
1441+
local.tee $3
1442+
i32.const 1
1443+
i32.lt_s
1444+
br_if $while-continue|08
1445+
end
1446+
i32.const 1
1447+
global.set $while/ran
14181448
)
14191449
(func $~start
14201450
global.get $~started

tests/compiler/while.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,35 @@ function testRefAutorelease(): void {
175175
ran = false;
176176
testRefAutorelease();
177177
assert(ran);
178+
179+
function testIfImplicitContinueThen(): void {
180+
var i = 0;
181+
while (true) {
182+
i++;
183+
if (i < 1) {
184+
// continue
185+
} else {
186+
break;
187+
}
188+
}
189+
ran = true;
190+
}
191+
ran = false;
192+
testIfImplicitContinueThen();
193+
assert(ran);
194+
195+
function testIfImplicitContinueElse(): void {
196+
var i = 0;
197+
while (true) {
198+
i++;
199+
if (i >= 1) {
200+
break;
201+
} else {
202+
// continue
203+
}
204+
}
205+
ran = true;
206+
}
207+
ran = false;
208+
testIfImplicitContinueElse();
209+
assert(ran);

tests/compiler/while.untouched.wat

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,66 @@
21542154
local.get $1
21552155
call $~lib/rt/pure/__release
21562156
)
2157+
(func $while/testIfImplicitContinueThen
2158+
(local $0 i32)
2159+
(local $1 i32)
2160+
i32.const 0
2161+
local.set $0
2162+
block $while-break|0
2163+
loop $while-continue|0
2164+
i32.const 1
2165+
local.set $1
2166+
local.get $1
2167+
if
2168+
local.get $0
2169+
i32.const 1
2170+
i32.add
2171+
local.set $0
2172+
local.get $0
2173+
i32.const 1
2174+
i32.lt_s
2175+
if
2176+
nop
2177+
else
2178+
br $while-break|0
2179+
end
2180+
br $while-continue|0
2181+
end
2182+
end
2183+
end
2184+
i32.const 1
2185+
global.set $while/ran
2186+
)
2187+
(func $while/testIfImplicitContinueElse
2188+
(local $0 i32)
2189+
(local $1 i32)
2190+
i32.const 0
2191+
local.set $0
2192+
block $while-break|0
2193+
loop $while-continue|0
2194+
i32.const 1
2195+
local.set $1
2196+
local.get $1
2197+
if
2198+
local.get $0
2199+
i32.const 1
2200+
i32.add
2201+
local.set $0
2202+
local.get $0
2203+
i32.const 1
2204+
i32.ge_s
2205+
if
2206+
br $while-break|0
2207+
else
2208+
nop
2209+
end
2210+
br $while-continue|0
2211+
end
2212+
end
2213+
end
2214+
i32.const 1
2215+
global.set $while/ran
2216+
)
21572217
(func $start:while
21582218
i32.const 0
21592219
global.set $while/ran
@@ -2310,6 +2370,32 @@
23102370
call $~lib/builtins/abort
23112371
unreachable
23122372
end
2373+
i32.const 0
2374+
global.set $while/ran
2375+
call $while/testIfImplicitContinueThen
2376+
global.get $while/ran
2377+
i32.eqz
2378+
if
2379+
i32.const 0
2380+
i32.const 32
2381+
i32.const 193
2382+
i32.const 1
2383+
call $~lib/builtins/abort
2384+
unreachable
2385+
end
2386+
i32.const 0
2387+
global.set $while/ran
2388+
call $while/testIfImplicitContinueElse
2389+
global.get $while/ran
2390+
i32.eqz
2391+
if
2392+
i32.const 0
2393+
i32.const 32
2394+
i32.const 209
2395+
i32.const 1
2396+
call $~lib/builtins/abort
2397+
unreachable
2398+
end
23132399
)
23142400
(func $~start
23152401
global.get $~started

0 commit comments

Comments
 (0)