RFR: 8268320: Better error recovery for broken patterns in switch

Jan Lahoda jlahoda at openjdk.java.net
Fri Jun 25 11:48:12 UTC 2021


On Thu, 10 Jun 2021 15:41:36 GMT, Vicente Romero <vromero at openjdk.org> wrote:

>> Trying to improve error recovery in case the user wanted to use a type-pattern in the switch construct, but didn't put a binding variable name after the type name. E.g.:
>> 
>> $ cat test/langtools/tools/javac/diags/examples/PatternExpected.java
>> [snip]
>> class PatternSwitch {
>>     private void doSwitch(Object o) {
>>         switch (o) {
>>             case String: break;
>>             default: break;
>>         }
>>     }
>> }
>> $ javac --enable-preview -source 17  test/langtools/tools/javac/diags/examples/PatternExpected.java
>> test/langtools/tools/javac/diags/examples/PatternExpected.java:32: error: type pattern expected
>>             case String: break;
>>                  ^
>> Note: test/langtools/tools/javac/diags/examples/PatternExpected.java uses preview features of Java SE 17.
>> Note: Recompile with -Xlint:preview for details.
>> 1 error
>
> test/langtools/tools/javac/patterns/SwitchErrors-no-preview.out line 1:
> 
>> 1: SwitchErrors.java:35:31: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.pattern.switch)
> 
> I don't get why you want to test what happens when the `--enable-preview + source` options are not passed

I apologize for a belated answer. The idea to test what happens when preview is disabled is that the error recovery messages are different. The error message won't suggest to use a pattern when pattern matching in switch is not enabled. That may or may not be the ideal behavior, but it seemed prudent to verify that behavior. I can split it into a separate test case, to make that more obvious, if desired.

-------------

PR: https://git.openjdk.java.net/jdk/pull/4441


More information about the compiler-dev mailing list