<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>promoted to amber-spec-experts because we are talking about the spec more than the implementation.<br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>De: </b>"Brian Goetz" <brian.goetz@oracle.com><br><b>À: </b>"jan lahoda" <jan.lahoda@oracle.com>, "Remi Forax" <forax@univ-mlv.fr><br><b>Cc: </b>"compiler-dev" <compiler-dev@openjdk.java.net><br><b>Envoyé: </b>Lundi 7 Juin 2021 21:14:40<br><b>Objet: </b>Re: javac throws an AssertionError while compiling a switch on types<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><font size="+1"><font face="monospace">The specification (and
design) does allow this. <br><br>
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." <br><br>
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.</font></font></blockquote><div><br></div><div>I'm not a big fan of warning if it can be an error.<br data-mce-bogus="1"></div><div>I'm old enough to remember when Java was touted as a clean language because it has no warnings (Go is now the new kid on the block on that front).</div><div>I'm Ok with warnings because of backward compatibility, otherwise, i prefer errors, especially in this case because it helps to convey the semantics.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>I like the fact that the total pattern has to be the last case. Default is as you say the total pattern minus null, so it seems logical to require it to be the last case too, to convey that the total pattern and default are semantically cousins.</div><div><br data-mce-bogus="1"></div><div>As you said, it goes against the fact that you can put default everywhere which is nice in term of code sharing, that's why we have decided to not do anything about it the last time we talk about default.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>I think i better understand the tradeoff around default that the last time we talk about, so i think we should revisit the spec (for Java 18) to require default to be at the end.<br data-mce-bogus="1"></div><div>But i may have miss a part of the picture ?<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><font size="+1"><font face="monospace"><br></font></font><br>
<div class="moz-cite-prefix">On 6/7/2021 10:17 AM, Jan Lahoda wrote:<br>
</div>
<blockquote cite="mid:267de959-1b1b-2f09-c045-85b3d9526829@oracle.com">Thanks
for the report, Remi.
<br>
<br>
<br>
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:
<br>
<br>
<a class="moz-txt-link-freetext" href="https://bugs.openjdk.java.net/browse/JDK-8268333" target="_blank">https://bugs.openjdk.java.net/browse/JDK-8268333</a>
<br>
<br>
<br>
Jan
<br>
<br>
<br>
On 07. 06. 21 11:54, Remi Forax wrote:
<br>
<blockquote>Hi all,
<br>
javac does like this code, there is a check missing because
javac should not reach Gen with a code like this.
<br>
<br>
Object o = null;
<br>
var value = switch(o) {
<br>
default -> 0;
<br>
case String s -> 0;
<br>
};
<br>
System.out.println(value);
<br>
<br>
An exception has occurred in the compiler (17-internal). Please
file a bug against the Java compiler via the Java bug reporting
page (<a class="moz-txt-link-freetext" href="http://bugreport.java.com" target="_blank">http://bugreport.java.com</a>) after checking the Bug Database
(<a class="moz-txt-link-freetext" href="http://bugs.java.com" target="_blank">http://bugs.java.com</a>) for duplicates. Include your program, the
following diagnostic, and the parameters passed to the Java
compiler in your report. Thank you.
<br>
java.lang.AssertionError
<br>
at
jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155)
<br>
at
jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.handleSwitch(Gen.java:1310)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.doHandleSwitchExpression(Gen.java:1238)<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.visitSwitchExpression(Gen.java:1202)<br>
at
jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitchExpression.accept(JCTree.java:1381)<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:877)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.visitLetExpr(Gen.java:2376)
<br>
at
jdk.compiler/com.sun.tools.javac.tree.JCTree$LetExpr.accept(JCTree.java:3288)<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genExpr(Gen.java:877)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.visitVarDef(Gen.java:1081)
<br>
at
jdk.compiler/com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:1028)<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:631)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genStats(Gen.java:682)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.visitBlock(Gen.java:1097)
<br>
at
jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1092)<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genStat(Gen.java:645)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genMethod(Gen.java:967)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.visitMethodDef(Gen.java:930)
<br>
at
jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:922)<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genDef(Gen.java:610)
<br>
at
jdk.compiler/com.sun.tools.javac.jvm.Gen.genClass(Gen.java:2415)
<br>
at
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:737)<br>
at
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1617)<br>
at
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1585)<br>
at
jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:946)<br>
at
jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:317)
<br>
at
jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:176)
<br>
at
jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
<br>
at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)
<br>
<br>
regards,
<br>
Rémi
<br>
</blockquote>
</blockquote>
<br><br></blockquote></div></div></body></html>