为什么一个不完整的 switch 表达式编译成功
Why does an incomplete switch expression compile successfully
试用 JDK/12 EarlyAccess Build 20, where the JEP-325 Switch Expressions 已集成为预览功能。表达式的示例代码(在 JEP 中也是如此):
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
};
我试图按照与上一个问题中所述相同的过程,关于如何 并使用命令行执行上述代码块:
java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar
出乎我的意料,我收到了以下错误:
Error: Unable to initialize main class
com.Whosebug.nullpointer.expression.SwitchExpressionMustComplete
Caused by: java.lang.VerifyError: Bad local variable type Exception
Details: Location:
com/Whosebug/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/Whosebug/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)
我知道文档指出代码有误,将注释替换为 break 1;
即可解决,但我的问题是:
Q1。 为什么编译阶段会成功? 编译阶段本身不应该失败吗?
Q2。我看到如此详细的错误消息的原因是什么? --enable-preview
功能是否与此有关?
这是一个已知错误。参见 JDK-8212982
有关其状态的详细信息。
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.
试用 JDK/12 EarlyAccess Build 20, where the JEP-325 Switch Expressions 已集成为预览功能。表达式的示例代码(在 JEP 中也是如此):
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
};
我试图按照与上一个问题中所述相同的过程,关于如何
java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar
出乎我的意料,我收到了以下错误:
Error: Unable to initialize main class com.Whosebug.nullpointer.expression.SwitchExpressionMustComplete Caused by: java.lang.VerifyError: Bad local variable type Exception Details: Location: com/Whosebug/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/Whosebug/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)
我知道文档指出代码有误,将注释替换为 break 1;
即可解决,但我的问题是:
Q1。 为什么编译阶段会成功? 编译阶段本身不应该失败吗?
Q2。我看到如此详细的错误消息的原因是什么? --enable-preview
功能是否与此有关?
这是一个已知错误。参见 JDK-8212982 有关其状态的详细信息。
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.