8312619: Strange error message when switching over long

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2023-07-26 10:35:15 +00:00
parent 1f81e5b19e
commit cc2a75e11c
4 changed files with 50 additions and 7 deletions

View File

@ -1683,11 +1683,16 @@ public class Attr extends JCTree.Visitor {
boolean stringSwitch = types.isSameType(seltype, syms.stringType);
boolean errorEnumSwitch = TreeInfo.isErrorEnumSwitch(selector, cases);
boolean intSwitch = types.isAssignable(seltype, syms.intType);
boolean errorPrimitiveSwitch = seltype.isPrimitive() && !intSwitch;
boolean patternSwitch;
if (!enumSwitch && !stringSwitch && !errorEnumSwitch && !intSwitch) {
if (!enumSwitch && !stringSwitch && !errorEnumSwitch &&
!intSwitch && !errorPrimitiveSwitch) {
preview.checkSourceLevel(selector.pos(), Feature.PATTERN_SWITCH);
patternSwitch = true;
} else {
if (errorPrimitiveSwitch) {
log.error(selector.pos(), Errors.SelectorTypeNotAllowed(seltype));
}
patternSwitch = cases.stream()
.flatMap(c -> c.labels.stream())
.anyMatch(l -> l.hasTag(PATTERNCASELABEL) ||
@ -1770,7 +1775,7 @@ public class Attr extends JCTree.Visitor {
} else if (!constants.add(s)) {
log.error(label.pos(), Errors.DuplicateCaseLabel);
}
} else if (!stringSwitch && !intSwitch) {
} else if (!stringSwitch && !intSwitch && !errorPrimitiveSwitch) {
log.error(label.pos(), Errors.ConstantLabelNotCompatible(pattype, seltype));
} else if (!constants.add(pattype.constValue())) {
log.error(c.pos(), Errors.DuplicateCaseLabel);
@ -1793,7 +1798,9 @@ public class Attr extends JCTree.Visitor {
if (!primaryType.hasTag(TYPEVAR)) {
primaryType = chk.checkClassOrArrayType(pat.pos(), primaryType);
}
checkCastablePattern(pat.pos(), seltype, primaryType);
if (!errorPrimitiveSwitch) {
checkCastablePattern(pat.pos(), seltype, primaryType);
}
Type patternType = types.erasure(primaryType);
JCExpression guard = c.guard;
if (guardBindings == null && guard != null) {

View File

@ -555,6 +555,10 @@ compiler.err.cannot.assign.not.declared.guard=\
compiler.err.constant.label.not.compatible=\
constant label of type {0} is not compatible with switch selector type {1}
# 0: type
compiler.err.selector.type.not.allowed=\
selector type {0} is not allowed
compiler.err.flows.through.to.pattern=\
illegal fall-through to a pattern

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.selector.type.not.allowed
public class SelectorTypeNotAllowed {
private void noLong(long sel) {
switch (sel) {
default -> {}
}
}
}

View File

@ -1,5 +1,5 @@
SwitchNoExtraTypes.java:12:18: compiler.err.constant.label.not.compatible: boolean, boolean
SwitchNoExtraTypes.java:18:18: compiler.err.constant.label.not.compatible: int, long
SwitchNoExtraTypes.java:24:18: compiler.err.constant.label.not.compatible: int, float
SwitchNoExtraTypes.java:30:18: compiler.err.constant.label.not.compatible: int, double
SwitchNoExtraTypes.java:11:16: compiler.err.selector.type.not.allowed: boolean
SwitchNoExtraTypes.java:17:16: compiler.err.selector.type.not.allowed: long
SwitchNoExtraTypes.java:23:16: compiler.err.selector.type.not.allowed: float
SwitchNoExtraTypes.java:29:16: compiler.err.selector.type.not.allowed: double
4 errors