Why does an incomplete switch expression compile successfully

1k views Asked by At

Trying out JDK/12 EarlyAccess Build 20, where the JEP-325 Switch Expressions has been integrated as a preview feature. A sample code for the expressions (as in the JEP as well):

Scanner scanner = new Scanner(System.in);
Day day = Day.valueOf(scanner.next().toUpperCase());
int i = switch (day) {
    case MONDAY,TUESDAY, WEDNESDAY:
        break 0;
    default:
        System.out.println("Second half of the week");
        // ERROR! Group doesn't contain a break with value
};

I was trying to follow the same procedure as stated in a previous question on how to Compile a JDK12 preview feature with Maven and execute the above block of code using the command line:

java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar

Somewhat to my expectation I got the following error :

Error: Unable to initialize main class
com.stackoverflow.nullpointer.expression.SwitchExpressionMustComplete
Caused by: java.lang.VerifyError: Bad local variable type Exception
Details:   Location:
    com/stackoverflow/nullpointer/expression/SwitchExpressionMustComplete.main([Ljava/lang/String;)V @66: iload   
Reason:
    Type top (current frame, locals[4]) is not assignable to integer   
Current Frame:
    bci: @66
    flags: { }
    locals: { '[Ljava/lang/String;', 'java/util/Scanner', 'com/stackoverflow/nullpointer/Day' }
    stack: { }   
Bytecode:
    0000000: bb00 0259 b200 03b7 0004 4c2b b600 05b8
    0000010: 0006 4db2 0007 2cb6 0008 2eaa 0000 001f
    0000020: 0000 0001 0000 0003 0000 0019 0000 0019
    0000030: 0000 0019 0336 04a7 000b b200 0912 0ab6
    0000040: 000b 1504 3eb1                        
Stackmap Table:
    append_frame(@52,Object[#2],Object[#34])
    same_frame(@58)
    same_frame(@66)

I am aware that the document points out that the code is erroneous and replacing the comment with break 1; resolves it, but the questions I have are:

Q1. Why does the compile phase succeed for the same? Shouldn't that fail at compile time itself?

Q2. What is the cause that I see such detailed error message? Could the --enable-preview feature responsible for this?

1

There are 1 answers

4
Brian Goetz On BEST ANSWER

This is a known bug. See JDK-8212982 for details on its status.

This code:

public class SwitchBug { 

    static String hold(String item) { 
        return switch(item) { 
            case String s -> { System.out.println(s); } 
            default -> "temp"; 
        }; 
    } 

    public static void main(String[] args) { 
        System.out.println(hold("bug")); 
    } 
}

compiles and produces:

bug 
temp 

This program should not compile, as the first case completes normally.