RFR: 8276836: Error in javac caused by switch expression without result expressions: Internal error: stack sim error

Vicente Romero vromero at openjdk.java.net
Fri May 27 16:21:41 UTC 2022


On Tue, 24 May 2022 12:47:44 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> 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`.

looks sensible

-------------

Marked as reviewed by vromero (Reviewer).

PR: https://git.openjdk.java.net/jdk/pull/8867


More information about the compiler-dev mailing list