Skip to content

Commit

Permalink
Compile incrementors in for loops within the initializer's flow
Browse files Browse the repository at this point in the history
It turns out that incrementors were compiled with the body's flow, which
meant that the incrementor had access to local variables declared in the
body. Now, incrementors no longer have access to such variables.

Fixes AssemblyScript#2825.
  • Loading branch information
CountBleck committed Mar 12, 2024
1 parent de174c5 commit b7afa67
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2600,6 +2600,7 @@ export class Compiler extends DiagnosticEmitter {
if (possiblyLoops) {
let incrementor = statement.incrementor;
if (incrementor) {
this.currentFlow = flow;
bodyStmts.push(
this.compileExpression(incrementor, Type.void, Constraints.ConvImplicit | Constraints.WillDrop)
);
Expand Down
55 changes: 55 additions & 0 deletions tests/compiler/issues/2825.debug.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(module
(type $0 (func))
(type $1 (func (param i32 i32 i32 i32)))
(type $2 (func (param i32) (result i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(global $~lib/memory/__data_end i32 (i32.const 60))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 32828))
(global $~lib/memory/__heap_base i32 (i32.const 32828))
(memory $0 1)
(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 "memory" (memory $0))
(start $~start)
(func $issues/2825/increment (param $x i32) (result i32)
local.get $x
i32.const 1234
i32.ne
i32.eqz
if
i32.const 0
i32.const 32
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
local.get $x
i32.const 1
i32.add
return
)
(func $start:issues/2825
(local $i i32)
(local $i|1 i32)
i32.const 0
local.set $i
loop $for-loop|0
local.get $i
i32.const 10
i32.lt_s
if
i32.const 1234
local.set $i|1
local.get $i
call $issues/2825/increment
local.set $i
br $for-loop|0
end
end
)
(func $~start
call $start:issues/2825
)
)
1 change: 1 addition & 0 deletions tests/compiler/issues/2825.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
36 changes: 36 additions & 0 deletions tests/compiler/issues/2825.release.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(module
(type $0 (func (param i32 i32 i32 i32)))
(type $1 (func))
(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 "memory" (memory $0))
(start $~start)
(func $~start
(local $0 i32)
loop $for-loop|0
local.get $0
i32.const 10
i32.lt_s
if
local.get $0
i32.const 1234
i32.eq
if
i32.const 0
i32.const 1056
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
local.get $0
i32.const 1
i32.add
local.set $0
br $for-loop|0
end
end
)
)
8 changes: 8 additions & 0 deletions tests/compiler/issues/2825.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function increment(x: i32): i32 {
assert(x !== 1234);
return x + 1;
}

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

0 comments on commit b7afa67

Please sign in to comment.