[java-highlighting] pattern of any type is not applicable at primitive types (JEP 427)

IDEA-302943

GitOrigin-RevId: a47bb507a501be23d03f84cb4891a66fec13fb0f
This commit is contained in:
Andrey.Cherkasov
2022-09-29 12:24:44 +04:00
committed by intellij-monorepo-bot
parent 1eca882374
commit e07cf816af
3 changed files with 18 additions and 2 deletions

View File

@@ -556,7 +556,13 @@ public class SwitchBlockHighlightingModel {
holder.add(info);
return;
}
if (!TypeConversionUtil.areTypesConvertible(mySelectorType, patternType)) {
if (!TypeConversionUtil.areTypesConvertible(mySelectorType, patternType) ||
// 14.30.3 A type pattern that declares a pattern variable of a reference type U is applicable at another
// reference type T if T is downcast convertible to U (5.5) (JEP 427)
// There is no rule that says that a reference type applies to a primitive type
// There is no restriction on primitive types in JEP 406 and JEP 420:
// 14.30.1 The pattern is of type T and e is downcast compatible with T (5.5).
(mySelectorType instanceof PsiPrimitiveType && HighlightingFeature.PATTERN_GUARDS_AND_RECORD_PATTERNS.isAvailable(label))) {
HighlightInfo error =
HighlightUtil.createIncompatibleTypeHighlightInfo(mySelectorType, patternType, elementToReport.getTextRange(), 0);
holder.add(error);

View File

@@ -131,6 +131,8 @@ class Main {
String str;
str = switch (i) {
case <error descr="'null' cannot be converted to 'int'">null</error> -> "ok";
case Integer integer -> "int";
case Object obj -> "Object";
default -> "not ok";
};
}

View File

@@ -1,5 +1,5 @@
class X {
int switchTest(Object obj) {
int switchTest1(Object obj) {
return switch (obj) {
case <error descr="Old patterns from JEP 406 are not available since Java 19 preview">(String s)</error> -> 1;
case <error descr="Old patterns from JEP 406 are not available since Java 19 preview">Integer i && predicate()</error> -> 2;
@@ -11,6 +11,14 @@ class X {
};
}
int switchTest2(int i) {
return switch (i) {
case <error descr="Incompatible types. Found: 'java.lang.Integer', required: 'int'">Integer integer</error> -> "int";
case <error descr="Incompatible types. Found: 'java.lang.Object', required: 'int'">Object obj</error> -> "Object";
default -> "not ok";
};
}
int instanceofTest(Object obj) {
if (obj instanceof (<error descr="Old patterns from JEP 406 are not available since Java 19 preview">Integer i && predicate()</error>)) {
return 1;