Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 74ce9bf

Browse files
MaxGraeydcodeIO
authored andcommitted
Fix unreachable insertion when returning none (AssemblyScript#557)
1 parent f9a8d2f commit 74ce9bf

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed

src/module.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,13 +1746,8 @@ export class BinaryModule {
17461746
/** Tests if an expression needs an explicit 'unreachable' when it is the terminating statement. */
17471747
export function needsExplicitUnreachable(expr: ExpressionRef): bool {
17481748
// not applicable if pushing a value to the stack
1749-
switch (_BinaryenExpressionGetType(expr)) {
1750-
case NativeType.I32:
1751-
case NativeType.I64:
1752-
case NativeType.F32:
1753-
case NativeType.F64:
1754-
case NativeType.V128: return false;
1755-
}
1749+
if (_BinaryenExpressionGetType(expr) != NativeType.Unreachable) return false;
1750+
17561751
switch (_BinaryenExpressionGetId(expr)) {
17571752
case ExpressionId.Unreachable:
17581753
case ExpressionId.Return: return false;

tests/compiler/function.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
1-
function v(): void {}
1+
function _(): void {}
2+
function v(): void { return; }
23
function i(): i32 { return 0; }
3-
function I(): i64 { return 0; }
4+
function j(): i64 { return 0; }
45
function f(): f32 { return 0; }
5-
function F(): f64 { return 0; }
6+
function d(): f64 { return 0; }
67

8+
_();
79
v();
810
i();
9-
I();
11+
j();
1012
f();
11-
F();
13+
d();
1214

13-
function iv(a: i32): void {}
15+
function i_(a: i32): void {}
16+
function iv(a: i32): void { return; }
1417
function ii(a: i32): i32 { return a; }
15-
function II(a: i64): i64 { return a; }
18+
function jj(a: i64): i64 { return a; }
1619
function ff(a: f32): f32 { return a; }
17-
function FF(a: f64): f64 { return a; }
20+
function dd(a: f64): f64 { return a; }
1821

22+
i_(0);
1923
iv(0);
2024
ii(0);
21-
II(0);
25+
jj(0);
2226
ff(0);
23-
FF(0);
27+
dd(0);
2428

25-
function iiv(a: i32, b: i32): void {}
29+
function ii_(a: i32, b: i32): void {}
30+
function iiv(a: i32, b: i32): void { return; }
2631
function iii(a: i32, b: i32): i32 { return a + b; }
27-
function III(a: i64, b: i32): i64 { return a + b; }
32+
function jjj(a: i64, b: i32): i64 { return a + b; }
2833
function fff(a: f32, b: f32): f32 { return a + b; }
29-
function FFF(a: f64, b: f64): f64 { return a + b; }
34+
function ddd(a: f64, b: f64): f64 { return a + b; }
3035

36+
ii_(1, 2);
3137
iiv(1, 2);
3238
iii(1, 2);
33-
III(1, 2);
39+
jjj(1, 2);
3440
fff(1, 2);
35-
FFF(1, 2);
41+
ddd(1, 2);

tests/compiler/function.untouched.wat

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,107 +21,122 @@
2121
(export "memory" (memory $0))
2222
(export "table" (table $0))
2323
(start $start)
24-
(func $function/v (; 0 ;) (type $FUNCSIG$v)
24+
(func $function/_ (; 0 ;) (type $FUNCSIG$v)
2525
nop
2626
)
27-
(func $function/i (; 1 ;) (type $FUNCSIG$i) (result i32)
27+
(func $function/v (; 1 ;) (type $FUNCSIG$v)
28+
nop
29+
)
30+
(func $function/i (; 2 ;) (type $FUNCSIG$i) (result i32)
2831
i32.const 0
2932
)
30-
(func $function/I (; 2 ;) (type $FUNCSIG$j) (result i64)
33+
(func $function/j (; 3 ;) (type $FUNCSIG$j) (result i64)
3134
i64.const 0
3235
)
33-
(func $function/f (; 3 ;) (type $FUNCSIG$f) (result f32)
36+
(func $function/f (; 4 ;) (type $FUNCSIG$f) (result f32)
3437
f32.const 0
3538
)
36-
(func $function/F (; 4 ;) (type $FUNCSIG$d) (result f64)
39+
(func $function/d (; 5 ;) (type $FUNCSIG$d) (result f64)
3740
f64.const 0
3841
)
39-
(func $function/iv (; 5 ;) (type $FUNCSIG$vi) (param $0 i32)
42+
(func $function/i_ (; 6 ;) (type $FUNCSIG$vi) (param $0 i32)
4043
nop
4144
)
42-
(func $function/ii (; 6 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
45+
(func $function/iv (; 7 ;) (type $FUNCSIG$vi) (param $0 i32)
46+
nop
47+
)
48+
(func $function/ii (; 8 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
4349
local.get $0
4450
)
45-
(func $function/II (; 7 ;) (type $FUNCSIG$jj) (param $0 i64) (result i64)
51+
(func $function/jj (; 9 ;) (type $FUNCSIG$jj) (param $0 i64) (result i64)
4652
local.get $0
4753
)
48-
(func $function/ff (; 8 ;) (type $FUNCSIG$ff) (param $0 f32) (result f32)
54+
(func $function/ff (; 10 ;) (type $FUNCSIG$ff) (param $0 f32) (result f32)
4955
local.get $0
5056
)
51-
(func $function/FF (; 9 ;) (type $FUNCSIG$dd) (param $0 f64) (result f64)
57+
(func $function/dd (; 11 ;) (type $FUNCSIG$dd) (param $0 f64) (result f64)
5258
local.get $0
5359
)
54-
(func $function/iiv (; 10 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
60+
(func $function/ii_ (; 12 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
5561
nop
5662
)
57-
(func $function/iii (; 11 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
63+
(func $function/iiv (; 13 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
64+
nop
65+
)
66+
(func $function/iii (; 14 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
5867
local.get $0
5968
local.get $1
6069
i32.add
6170
)
62-
(func $function/III (; 12 ;) (type $FUNCSIG$jji) (param $0 i64) (param $1 i32) (result i64)
71+
(func $function/jjj (; 15 ;) (type $FUNCSIG$jji) (param $0 i64) (param $1 i32) (result i64)
6372
local.get $0
6473
local.get $1
6574
i64.extend_i32_s
6675
i64.add
6776
)
68-
(func $function/fff (; 13 ;) (type $FUNCSIG$fff) (param $0 f32) (param $1 f32) (result f32)
77+
(func $function/fff (; 16 ;) (type $FUNCSIG$fff) (param $0 f32) (param $1 f32) (result f32)
6978
local.get $0
7079
local.get $1
7180
f32.add
7281
)
73-
(func $function/FFF (; 14 ;) (type $FUNCSIG$ddd) (param $0 f64) (param $1 f64) (result f64)
82+
(func $function/ddd (; 17 ;) (type $FUNCSIG$ddd) (param $0 f64) (param $1 f64) (result f64)
7483
local.get $0
7584
local.get $1
7685
f64.add
7786
)
78-
(func $start:function (; 15 ;) (type $FUNCSIG$v)
87+
(func $start:function (; 18 ;) (type $FUNCSIG$v)
88+
call $function/_
7989
call $function/v
8090
call $function/i
8191
drop
82-
call $function/I
92+
call $function/j
8393
drop
8494
call $function/f
8595
drop
86-
call $function/F
96+
call $function/d
8797
drop
8898
i32.const 0
99+
call $function/i_
100+
i32.const 0
89101
call $function/iv
90102
i32.const 0
91103
call $function/ii
92104
drop
93105
i64.const 0
94-
call $function/II
106+
call $function/jj
95107
drop
96108
f32.const 0
97109
call $function/ff
98110
drop
99111
f64.const 0
100-
call $function/FF
112+
call $function/dd
101113
drop
102114
i32.const 1
103115
i32.const 2
116+
call $function/ii_
117+
i32.const 1
118+
i32.const 2
104119
call $function/iiv
105120
i32.const 1
106121
i32.const 2
107122
call $function/iii
108123
drop
109124
i64.const 1
110125
i32.const 2
111-
call $function/III
126+
call $function/jjj
112127
drop
113128
f32.const 1
114129
f32.const 2
115130
call $function/fff
116131
drop
117132
f64.const 1
118133
f64.const 2
119-
call $function/FFF
134+
call $function/ddd
120135
drop
121136
)
122-
(func $start (; 16 ;) (type $FUNCSIG$v)
137+
(func $start (; 19 ;) (type $FUNCSIG$v)
123138
call $start:function
124139
)
125-
(func $null (; 17 ;) (type $FUNCSIG$v)
140+
(func $null (; 20 ;) (type $FUNCSIG$v)
126141
)
127142
)

0 commit comments

Comments
 (0)