diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index 7214220ba9d..d7313ea2067 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -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) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties index 709ce943098..78b24f14bb9 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties @@ -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 diff --git a/test/langtools/tools/javac/diags/examples/SelectorTypeNotAllowed.java b/test/langtools/tools/javac/diags/examples/SelectorTypeNotAllowed.java new file mode 100644 index 00000000000..056f3c56c8b --- /dev/null +++ b/test/langtools/tools/javac/diags/examples/SelectorTypeNotAllowed.java @@ -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 -> {} + } + } +} \ No newline at end of file diff --git a/test/langtools/tools/javac/switchextra/SwitchNoExtraTypes.out b/test/langtools/tools/javac/switchextra/SwitchNoExtraTypes.out index eb2e3eff4f6..32854d2de20 100644 --- a/test/langtools/tools/javac/switchextra/SwitchNoExtraTypes.out +++ b/test/langtools/tools/javac/switchextra/SwitchNoExtraTypes.out @@ -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