Pattern matching for switch: Spec and Javac inconsistency regarding enhanced switch statements

Jan Lahoda jan.lahoda at oracle.com
Wed Jul 7 08:04:55 UTC 2021


Thanks Ilyas. I've filled:

https://bugs.openjdk.java.net/browse/JDK-8270006


Jan


On 07. 07. 21 9:17, Ilyas Selimov wrote:
> Hello!
>
> I found a difference between completeness rules for enhanced switch 
> statements in spec draft 
> (http://cr.openjdk.java.net/~gbierman/jep406/jep406-20210527/specs/patterns-switch-jls.html#jls-14.11.2 
> <http://cr.openjdk.java.net/~gbierman/jep406/jep406-20210527/specs/patterns-switch-jls.html#jls-14.11.2>) 
>
> and its javac implementation (OpenJDK 17 build 17-ea+29-2576).
>
> > An enhanced switch statement is one where either (i) the type of the 
> selector expression is not char, byte, short, int, Character, Byte, 
> Short, Integer, String, or an enum type,
> or (ii) at least one of the switch labels has a pattern case label 
> element or a null case label element.
>
> According to the statement, the next switch statement is enhanced:
> void test(String s) {
>   switch (s) {
>     case null:
>       break;
>   }
> }
>
> > If the switch statement is an enhanced switch statement, then the 
> switch block must be complete for the selector expression.
>
> As one contains neither total pattern, nor default label, it's 
> incomplete, but javac compiles it correctly.
> Maybe I missed PR that fixed the issue.
>
> Thanks,
> Ilyas


More information about the compiler-dev mailing list