Skip to content

Commit

Permalink
[WASM] Make sure that EmbeddedStatements that return a value never …
Browse files Browse the repository at this point in the history
…exit without going through an explicit `YieldStatement`.

PiperOrigin-RevId: 724486775
  • Loading branch information
rluble authored and copybara-github committed Feb 7, 2025
1 parent e1f307e commit ab5125b
Show file tree
Hide file tree
Showing 5 changed files with 680 additions and 588 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,14 @@ public boolean enterEmbeddedStatement(EmbeddedStatement embeddedStatement) {
" (result " + environment.getWasmType(embeddedStatement.getTypeDescriptor()) + ")");

StatementTranspiler.render(statement, sourceBuilder, environment);
if (!TypeDescriptors.isPrimitiveVoid(embeddedStatement.getTypeDescriptor())) {
// If the embedded statement returns a value, it should have an explicit yield and never
// reach the end; we add an unreachable instruction at the end to make the code
// verifiable. This prevents wasm from rejecting exhaustive switch expressions that will
// never reach this point but wouldn't be verifiable in the resulting wasm.
sourceBuilder.newLine();
sourceBuilder.append("(unreachable)");
}
sourceBuilder.closeParens();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javaemul.internal.annotations.Wasm;
import jsinterop.annotations.JsEnum;
import jsinterop.annotations.JsFunction;
import jsinterop.annotations.JsMethod;
Expand Down Expand Up @@ -146,8 +145,6 @@ public static void testJsEnumSwitch() {
acceptsSupplierOfSupplier(() -> (() -> ComparableJsEnum.ONE));
}

// TODO(b/395108282): Remove nop once the bug is fixed.
@Wasm("nop")
private static void testExhaustiveJsEnumSwitchExpression() {
ComparableJsEnum comparableJsEnum =
ComparableJsEnum.ONE.getValue() == 1 ? ComparableJsEnum.TWO : null;
Expand Down
Loading

0 comments on commit ab5125b

Please sign in to comment.