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

Rémi Forax forax at openjdk.java.net
Fri Jun 25 12:21:08 UTC 2021

On Fri, 25 Jun 2021 11:42:59 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

>> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 1748:
>>> 1746:                                     }
>>> 1747:                                 } else if (!stringSwitch && !types.isAssignable(seltype, syms.intType)) {
>>> 1748:                                     log.error(pat.pos(), Errors.ConstantLabelNotCompatible(pattype, seltype));
>> I wonder if in the long term it's not better instead of having several ad-hoc way to find the kind of switch it is,
>> `types.isAssignable(seltype, syms.intType)`, `stringSwitch` or `pattype.constValue() == null && s.kind == TYP`
>> to have an enum with 4 values INT_SWITCH, STRING_SWITCH, ENUM_SWITCH and PATTERN_SWITCH.
> I apologize for a belated answer. It is my plan to try to categorize switches, but when considering error recovery and various desugaring stages, it is not trivial. Also, please note that some of the conditions are not a property of the switch, but rather a property of the case (like in this case - saying `case String:` instead of `case String s:` is more a property of the case, than the switch.

Yes, it's not trivial, and it's not for that PR, but it's usually a good idea in a compiler to have such kind of canonical representation because it avoid bugs between different passes that does those checks in various non compatible ways. Especially when you have different desugaring stages.


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

More information about the compiler-dev mailing list