RFR: 8276836: Error in javac caused by switch expression without result expressions: Internal error: stack sim error
Jan Lahoda
jlahoda at openjdk.java.net
Tue May 24 12:53:38 UTC 2022
Normally, a switch expression must yield a value. But, there are cases where the switch expression has a value in the javac frontend, but, due to optimizations, not when the code is generated. For example:
int i = 1 + switch (0) { default -> {if (true) throw new RuntimeException(); else yield 0; } };
javac will optimize the else section away, and the switch expression will always end abruptly. But, javac's simulated stack will still contain the left value of the binary operator, which will never be removed, and javac will consequently fail:
.java:2: error: Internal error: stack sim error on t()
private void t() {
^
1 error
The proposed solution is twofold:
- the throw statement/instruction should clear the stack, which adheres more closely to the runtime behavior of the instruction. This is the change in `Code`.
- skipping code generation for expressions when the code is not "alive", as such code does not have any sense. This is similar to how statements are generated. This is the change in `Gen`.
-------------
Commit messages:
- 8276836: Error in javac caused by switch expression without result expressions: Internal error: stack sim error
Changes: https://git.openjdk.java.net/jdk/pull/8867/files
Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=8867&range=00
Issue: https://bugs.openjdk.java.net/browse/JDK-8276836
Stats: 219 lines in 3 files changed: 218 ins; 0 del; 1 mod
Patch: https://git.openjdk.java.net/jdk/pull/8867.diff
Fetch: git fetch https://git.openjdk.java.net/jdk pull/8867/head:pull/8867
PR: https://git.openjdk.java.net/jdk/pull/8867
More information about the compiler-dev
mailing list