JDK-8212982: Rule cases in switch expression accepted even if complete normally (original) (raw)

Jan Lahoda jan.lahoda at oracle.com
Tue Nov 20 17:14:35 UTC 2018


Hi,

Bug: https://bugs.openjdk.java.net/browse/JDK-8212982

The issue here is that javac accepts switch expressions that complete normally without providing a value, which is not correct. A (simpler) fix is to enhance Flow with the necessary checks + enhancements to place the errors at a good place.

Webrev: http://cr.openjdk.java.net/~jlahoda/8212982/webrev.00/

This patch has a problem in cases like:

public class SE { private String t(int i) { return switch (i) { default: break ""; System.err.println(0); }; } }

This produces:

$ javac --enable-preview --source 12 SE.java SE.java:6: error: unreachable statement System.err.println(0); ^ SE.java:7: error: switch expression completes without providing a value }; ^ (switch expressions must either provide a value or throw for all possible input values)

The second error is caused by the first one (Flow will reset "alive" to "true" when reporting the "unreachable statement" error as an error recovery). A patch that changes the "alive" field to be tri-state (ALIVE, NOT_ALIVE, RECOVERY) so that it can suppress the second error in case of this recovery is here:

Webrev 2: http://cr.openjdk.java.net/~jlahoda/8212982/webrev.00b/

(The only differences between the patches are in the Flow.java and ExpressionSwitchUnreachable.out.)

This is longer, but I think it provides better errors, so I'd prefer this patch, but I am also fine with the first one.

Any feedback is welcome!

Thanks, Jan



More information about the compiler-dev mailing list