javac throws an AssertionError while compiling a switch on types

Brian Goetz brian.goetz at oracle.com
Mon Jun 7 19:14:40 UTC 2021


The specification (and design) does allow this.

We talked about revising the positioning rules regarding `default`, but 
in the end decided not to do anything about this.  So putting `default` 
ahead of `String s` is allowed.  The interpretation of `default` is 
"anything not handled by some other case, except null."

In point of fact, what this means is that we have to test all the 
patterns before concluding something goes in the default bucket, not 
just the patterns that precede the default case.  So Strings fall into 
the `case String s` bucket, and everything else (except null) falls into 
the default bucket.  This is weird enough behavior we might consider 
issuing a warning.


On 6/7/2021 10:17 AM, Jan Lahoda wrote:
> Thanks for the report, Remi.
>
>
> I believe the specification currently allows this (default does not 
> have to be last, and does not dominate anything), so javac should be 
> able to handle this code. I've filled:
>
> https://bugs.openjdk.java.net/browse/JDK-8268333
>
>
> Jan
>
>
> On 07. 06. 21 11:54, Remi Forax wrote:
>> Hi all,
>> javac does like this code, there is a check missing because javac 
>> should not reach Gen with a code like this.
>>
>>      Object o = null;
>>      var value = switch(o) {
>>            default -> 0;
>>            case String s -> 0;
>>      };
>>      System.out.println(value);
>>
>> An exception has occurred in the compiler (17-internal). Please file 
>> a bug against the Java compiler via the Java bug reporting page 
>> (http://bugreport.java.com) after checking the Bug Database 
>> (http://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.AssertionError
>>     at 
>> jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155)
>>     at 
>> jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46)
>>     at 
>> jdk.compiler/com.sun.tools.javac.jvm.Gen.handleSwitch(Gen.java:1310)
>>     at 
>> jdk.compiler/com.sun.tools.javac.jvm.Gen.doHandleSwitchExpression(Gen.java:1238)
>>     at 
>> jdk.compiler/com.sun.tools.javac.jvm.Gen.visitSwitchExpression(Gen.java:1202)
>>     at 
>> jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitchExpression.accept(JCTree.java:1381)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:877)
>>     at 
>> jdk.compiler/com.sun.tools.javac.jvm.Gen.visitLetExpr(Gen.java:2376)
>>     at 
>> jdk.compiler/com.sun.tools.javac.tree.JCTree$LetExpr.accept(JCTree.java:3288)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:877)
>>     at 
>> jdk.compiler/com.sun.tools.javac.jvm.Gen.visitVarDef(Gen.java:1081)
>>     at 
>> jdk.compiler/com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:1028)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:631)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:682)
>>     at 
>> jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1097)
>>     at 
>> jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1092)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:967)
>>     at 
>> jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:930)
>>     at 
>> jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:922)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
>>     at jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2415)
>>     at 
>> jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:737)
>>     at 
>> jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1617)
>>     at 
>> jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1585)
>>     at 
>> jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:946)
>>     at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:317)
>>     at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:176)
>>     at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
>>     at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)
>>
>> regards,
>> Rémi

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210607/92e385a7/attachment-0001.htm>


More information about the compiler-dev mailing list