-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fallback throw for exhaustive switch expressions on unexpected va…
…lues. PiperOrigin-RevId: 726187098
- Loading branch information
1 parent
5e6e77e
commit 37f43d1
Showing
11 changed files
with
165 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...piler/java/com/google/j2cl/transpiler/passes/AddSwitchExpressionsExhaustivenessCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2025 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.google.j2cl.transpiler.passes; | ||
|
||
import com.google.j2cl.transpiler.ast.AbstractVisitor; | ||
import com.google.j2cl.transpiler.ast.CompilationUnit; | ||
import com.google.j2cl.transpiler.ast.RuntimeMethods; | ||
import com.google.j2cl.transpiler.ast.SwitchCase; | ||
import com.google.j2cl.transpiler.ast.SwitchExpression; | ||
|
||
/** | ||
* Instruments exhaustive switch expressions with no default to handle unexpected values. | ||
* | ||
* <p>Exhaustive switch expressions that don't explicitly handle the default case are instrumented | ||
* to check for unexpected values. In the JVM this is done to generate correct code in the presence | ||
* of library skew, which is not an issue in J2CL. However a similar situation could happen with | ||
* JsEnums where it is quite easy to receive an unexpected value. | ||
* | ||
* <p>The same reasoning could be applied to switch statements; i.e. if we determine that the switch | ||
* statement is exhaustive and lacks default handling it could be instrumented. But the nuance in | ||
* switch statements is that the default handling might be present outside the switch construct as | ||
* in the following code. | ||
* | ||
* <pre>{@code | ||
* enum X { A, B;} | ||
* | ||
* .... | ||
* switch (e) { | ||
* case A: | ||
* return 1; | ||
* case B: | ||
* return 2; | ||
* } | ||
* // Handle unexpected value here | ||
* throw new AssertionError(); | ||
* }</pre> | ||
*/ | ||
public class AddSwitchExpressionsExhaustivenessCheck extends NormalizationPass { | ||
@Override | ||
public void applyTo(CompilationUnit compilationUnit) { | ||
compilationUnit.accept( | ||
new AbstractVisitor() { | ||
@Override | ||
public void exitSwitchExpression(SwitchExpression switchExpression) { | ||
if (switchExpression.hasDefaultCase()) { | ||
return; | ||
} | ||
|
||
// The switch expression does not have a default case; that means that the frontend | ||
// deemed it exhaustive, hence add a default case with a runtime check. | ||
// TODO(b/395953418): Reject switch expressions with no default on native jsenums. | ||
var expressionType = switchExpression.getTypeDescriptor().toRawTypeDescriptor(); | ||
var isCritical = expressionType.isNative() && expressionType.isJsEnum(); | ||
|
||
var sourcePosition = switchExpression.getSourcePosition(); | ||
var checkMethodCall = | ||
RuntimeMethods.createCheckCriticalExhaustiveCall(isCritical) | ||
.makeStatement(sourcePosition); | ||
switchExpression | ||
.getCases() | ||
.add(SwitchCase.newBuilder().setStatements(checkMethodCall).build()); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1485,23 +1485,28 @@ | |
(block | ||
(block | ||
(block | ||
(block ;; evaluate expression and jump | ||
(br_table 2 1 0 3 (local.get $$expression)) | ||
(block | ||
(block ;; evaluate expression and jump | ||
(br_table 2 1 0 3 (local.get $$expression)) | ||
) | ||
;; case 2: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/jsenum/readable-j2wasm.js/jsenum/Main.java:153:22 | ||
(i32.const 2) | ||
(br $SWITCH) | ||
) | ||
;; case 2: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/jsenum/readable-j2wasm.js/jsenum/Main.java:153:22 | ||
(i32.const 2) | ||
;; case 1: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/jsenum/readable-j2wasm.js/jsenum/Main.java:154:22 | ||
(i32.const 1) | ||
(br $SWITCH) | ||
) | ||
;; case 1: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/jsenum/readable-j2wasm.js/jsenum/Main.java:154:22 | ||
(i32.const 1) | ||
;; case 0: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/jsenum/readable-j2wasm.js/jsenum/Main.java:155:23 | ||
(i32.const 0) | ||
(br $SWITCH) | ||
) | ||
;; case 0: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/jsenum/readable-j2wasm.js/jsenum/Main.java:155:23 | ||
(i32.const 0) | ||
(br $SWITCH) | ||
;; default: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/jsenum/readable-j2wasm.js/jsenum/Main.java:152:8 | ||
(call [email protected] ) | ||
) | ||
) | ||
(unreachable) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,18 +323,23 @@ | |
;;@ transpiler/javatests/com/google/j2cl/readable/java/switchexpression/readable-j2wasm.js/switchexpression/SwitchExpression.java:82:8 | ||
(block | ||
(block | ||
(block ;; evaluate expression and jump | ||
(br_table 0 1 2 (local.get $$expression)) | ||
(block | ||
(block ;; evaluate expression and jump | ||
(br_table 0 1 2 (local.get $$expression)) | ||
) | ||
;; case 0: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/switchexpression/readable-j2wasm.js/switchexpression/SwitchExpression.java:83:20 | ||
(i64.const 0) | ||
(br $SWITCH) | ||
) | ||
;; case 0: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/switchexpression/readable-j2wasm.js/switchexpression/SwitchExpression.java:83:20 | ||
(i64.const 0) | ||
;; case 1: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/switchexpression/readable-j2wasm.js/switchexpression/SwitchExpression.java:84:20 | ||
(i64.const 1) | ||
(br $SWITCH) | ||
) | ||
;; case 1: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/switchexpression/readable-j2wasm.js/switchexpression/SwitchExpression.java:84:20 | ||
(i64.const 1) | ||
(br $SWITCH) | ||
;; default: | ||
;;@ transpiler/javatests/com/google/j2cl/readable/java/switchexpression/readable-j2wasm.js/switchexpression/SwitchExpression.java:82:8 | ||
(call [email protected] ) | ||
) | ||
) | ||
(unreachable) | ||
|