Integrated: 8331212: Error recovery for broken switch expressions could be improved

Jan Lahoda jlahoda at openjdk.org
Wed May 1 12:22:05 UTC 2024


On Mon, 29 Apr 2024 11:51:32 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Consider code like:
> 
> class Test {
>     public boolean test() {
>         return switch (0) {
>             case 0 -> true;
>             default -> {}
>         };
>     }
> }
> 
> 
> when compiling this, javac reports and error, and then crashes with an exception:
> 
> $ javac -XDdev /tmp/Test.java
> /tmp/Test.java:5: error: switch rule completes without providing a value
>             default -> {}
>                         ^
>   (switch rules in switch expressions must either provide a value or throw)
> 1 error
> An exception has occurred in the compiler (23-internal). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
> java.lang.NullPointerException: Cannot read field "type" because "tree" is null
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scanCond(Flow.java:2363)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitYield(Flow.java:2990)
>         at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCYield.accept(JCTree.java:1673)
>         at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:463)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:2095)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scanSyntheticBreak(Flow.java:475)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.handleSwitch(Flow.java:2802)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitSwitchExpression(Flow.java:2777)
>         at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitchExpression.accept(JCTree.java:1395)
>         at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:463)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scan(Flow.java:2095)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.scanExpr(Flow.java:2340)
>         at jdk.compiler/com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitReturn(Flow.java:3023)
>         at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1724)
>         at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:...

This pull request has now been integrated.

Changeset: 44dc8500
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/44dc85001d8c17a12efebd1a69d52e0b7e4e95e4
Stats:     94 lines in 2 files changed: 93 ins; 0 del; 1 mod

8331212: Error recovery for broken switch expressions could be improved

Reviewed-by: asotona

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

PR: https://git.openjdk.org/jdk/pull/18997


More information about the compiler-dev mailing list