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: for loop statement incrementor will be processed in correct context #2839

Merged
merged 1 commit into from
Apr 10, 2024
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
16 changes: 8 additions & 8 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,14 @@ export class Compiler extends DiagnosticEmitter {
bodyStmts.length = 1;
}

if (condKind == ConditionKind.True) {
// Body executes at least once
flow.inherit(bodyFlow);
} else {
// Otherwise executes conditionally
flow.mergeBranch(bodyFlow);
}

// Compile the incrementor if it possibly executes
let possiblyLoops = possiblyContinues || possiblyFallsThrough;
if (possiblyLoops) {
Expand All @@ -2618,14 +2626,6 @@ export class Compiler extends DiagnosticEmitter {
}
}

// Body executes at least once
if (condKind == ConditionKind.True) {
flow.inherit(bodyFlow);

// Otherwise executes conditionally
} else {
flow.mergeBranch(bodyFlow);
}

// Finalize
outerFlow.inherit(flow);
Expand Down
14 changes: 14 additions & 0 deletions tests/compiler/issues/2825.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
(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")
(table $0 1 1 funcref)
(elem $0 (i32.const 1))
(export "init" (func $issues/2825/init))
(export "memory" (memory $0))
(start $~start)
(func $issues/2825/increment (param $x i32) (result i32)
Expand Down Expand Up @@ -49,6 +50,19 @@
end
end
)
(func $issues/2825/init
(local $not_unassigned i32)
loop $for-loop|0
i32.const 1
if
i32.const 0
local.set $not_unassigned
local.get $not_unassigned
drop
br $for-loop|0
end
end
)
(func $~start
call $start:issues/2825
)
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/issues/2825.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
{}
11 changes: 9 additions & 2 deletions tests/compiler/issues/2825.release.wat
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
(module
(type $0 (func (param i32 i32 i32 i32)))
(type $1 (func))
(type $0 (func))
(type $1 (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data $0 (i32.const 1036) ",")
(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")
(export "init" (func $issues/2825/init))
(export "memory" (memory $0))
(start $~start)
(func $issues/2825/init
loop $for-loop|0
br $for-loop|0
end
unreachable
)
(func $~start
(local $0 i32)
loop $for-loop|0
Expand Down
9 changes: 8 additions & 1 deletion tests/compiler/issues/2825.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ function increment(x: i32): i32 {

for (let i = 0; i < 10; i = increment(i)) {
let i = 1234;
}
}

export function init(): void {
let not_unassigned: i32;
for (; ; not_unassigned) {
not_unassigned = 0;
}
}
Loading