Skip to content

Commit c8c1a53

Browse files
committed
fix return unreachable in BlockStatement causes validate failed (#2187)
1 parent 96d6831 commit c8c1a53

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/compiler.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,15 +2793,24 @@ export class Compiler extends DiagnosticEmitter {
27932793
}
27942794

27952795
// Otherwise emit a normal return
2796-
return !expr
2797-
? isLastInBody
2798-
? module.nop()
2799-
: module.return()
2800-
: isLastInBody
2801-
? expr
2802-
: this.currentType == Type.void
2803-
? module.block(null, [ expr, module.return() ])
2804-
: module.return(expr);
2796+
if (expr) {
2797+
if (getExpressionId(expr) == ExpressionId.Unreachable) {
2798+
// emit unreachable directly
2799+
return expr;
2800+
}
2801+
if (isLastInBody) {
2802+
return expr;
2803+
}
2804+
if (this.currentType == Type.void) {
2805+
return module.block(null, [ expr, module.return() ]);
2806+
}
2807+
return module.return(expr);
2808+
} else {
2809+
if (isLastInBody) {
2810+
return module.nop();
2811+
}
2812+
return module.return();
2813+
}
28052814
}
28062815

28072816
private compileSwitchStatement(

src/passes/shadowstack.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ class InstrumentReturns extends Pass {
658658
var stmts = new Array<ExpressionRef>();
659659
if (value) {
660660
let returnType = _BinaryenExpressionGetType(value);
661+
if (returnType != TypeRef.Unreachable) return;
661662
let temp = this.parentPass.getSharedTemp(this.currentFunction, returnType);
662663
// t = value
663664
stmts.push(

0 commit comments

Comments
 (0)