From 838cf2d73cb6d63ad1f4866e14c718e4c0f85109 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 24 Jun 2024 10:56:00 +0200 Subject: [PATCH] [java-incomplete-model] Assume that any unresolved class-type is assignable to java.lang.Object Partially improves inference in incomplete mode GitOrigin-RevId: 1d9c7c75afd524ea02980be8f96c19fba1eaaf54 --- .../com/intellij/psi/util/TypeConversionUtil.java | 3 +-- .../CastInToArrayCallWithUnresolvedType-out.java | 8 ++++++++ .../incompleteHighlighting/InferenceFailure.java | 4 ++++ .../castMethodParameters15/afterIncorrectType.java | 14 -------------- .../afterIncorrectTypeParens.java | 14 -------------- .../beforeIncorrectType.java | 2 +- .../beforeIncorrectTypeParens.java | 2 +- .../foreach/afterForEachNested.java | 2 +- .../quickFix/streamToLoop/afterCollector.java | 2 +- .../completion/SmartType18CompletionTest.java | 3 ++- .../intellij/refactoring/TypeMigrationTest.java | 2 -- .../refactoring/typeMigration/t49/after/Test.items | 4 ++-- .../refactoring/typeMigration/t49/after/test.java | 2 +- .../refactoring/typeMigration/t50/after/Test.items | 4 ++-- .../refactoring/typeMigration/t50/after/test.java | 2 +- .../toPrimitivesAndString/after/Test.items | 1 - .../highlighting/typecheck/TypeCheckEnum.groovy | 2 +- 17 files changed, 26 insertions(+), 45 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/CastInToArrayCallWithUnresolvedType-out.java delete mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/afterIncorrectType.java delete mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/afterIncorrectTypeParens.java diff --git a/java/java-psi-api/src/com/intellij/psi/util/TypeConversionUtil.java b/java/java-psi-api/src/com/intellij/psi/util/TypeConversionUtil.java index 0ab981f40aa9..457d69192e74 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/TypeConversionUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/TypeConversionUtil.java @@ -985,8 +985,7 @@ public final class TypeConversionUtil { PsiClass leftResultElement = leftResult.getElement(); PsiClass rightResultElement = rightResult.getElement(); if (leftResultElement == null || rightResultElement == null) { - if (leftResultElement == null && rightResultElement != null && - left instanceof PsiClassType && left.equalsToText(JAVA_LANG_OBJECT)) { + if (left instanceof PsiClassType && left.equalsToText(JAVA_LANG_OBJECT)) { return true; } if (leftResultElement != rightResultElement) return false; diff --git a/java/java-tests/testData/codeInsight/completion/smartType/CastInToArrayCallWithUnresolvedType-out.java b/java/java-tests/testData/codeInsight/completion/smartType/CastInToArrayCallWithUnresolvedType-out.java new file mode 100644 index 000000000000..d7df593686ff --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/CastInToArrayCallWithUnresolvedType-out.java @@ -0,0 +1,8 @@ +public class SomeClass { + + { + java.util.List l; + Unresolved[] a = l.toArray((Unresolved[]) expr); + } + +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/incompleteHighlighting/InferenceFailure.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/incompleteHighlighting/InferenceFailure.java index ed1950d170d5..fbc6902b24f7 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/incompleteHighlighting/InferenceFailure.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/incompleteHighlighting/InferenceFailure.java @@ -2,6 +2,10 @@ import java.util.*; import foo.bar.baz.*; class Test { + void testDiamond() { + List<X> list = new ArrayList<>(); + } + void test() { List data = new ArrayList<>( Arrays.asList(a.X, a.Y)); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/afterIncorrectType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/afterIncorrectType.java deleted file mode 100644 index cf641717c7bc..000000000000 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/afterIncorrectType.java +++ /dev/null @@ -1,14 +0,0 @@ -// "Cast argument to 'Map'" "true-preview" - -import java.util.*; - -class X { - void run(Foo single) {} - void run(Map map) {} - - void test(Bar bar) { - run((Map) Collections.singletonMap(getFoo(), bar)); - } - - native Foo getFoo(); -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/afterIncorrectTypeParens.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/afterIncorrectTypeParens.java deleted file mode 100644 index cf641717c7bc..000000000000 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/afterIncorrectTypeParens.java +++ /dev/null @@ -1,14 +0,0 @@ -// "Cast argument to 'Map'" "true-preview" - -import java.util.*; - -class X { - void run(Foo single) {} - void run(Map map) {} - - void test(Bar bar) { - run((Map) Collections.singletonMap(getFoo(), bar)); - } - - native Foo getFoo(); -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/beforeIncorrectType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/beforeIncorrectType.java index 5433639fa4d6..a2887ad5c5ae 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/beforeIncorrectType.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/beforeIncorrectType.java @@ -1,4 +1,4 @@ -// "Cast argument to 'Map'" "true-preview" +// "Cast argument to 'Map'" "false" import java.util.*; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/beforeIncorrectTypeParens.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/beforeIncorrectTypeParens.java index e98f4eba9cb4..59178db8990a 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/beforeIncorrectTypeParens.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15/beforeIncorrectTypeParens.java @@ -1,4 +1,4 @@ -// "Cast argument to 'Map'" "true-preview" +// "Cast argument to 'Map'" "false" import java.util.*; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/afterForEachNested.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/afterForEachNested.java index 6f1c6495e583..145733ae3731 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/afterForEachNested.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/afterForEachNested.java @@ -6,7 +6,7 @@ public class Test { public static void main(String[] args) { for (String module : args) { VirtualFile[] sourceRoots = foo(module); - Arrays.stream(sourceRoots).forEach((VirtualFile sourceRoot) -> sourceRoot.substring()); + Arrays.stream(sourceRoots).forEach(sourceRoot -> sourceRoot.substring()); } } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamToLoop/afterCollector.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamToLoop/afterCollector.java index dc9469c87fa8..98710ff27374 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamToLoop/afterCollector.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamToLoop/afterCollector.java @@ -115,7 +115,7 @@ public class Main { // Unresolved reference void f(Collection c) { - R treeSet = new TreeSet(); + TreeSet treeSet = new TreeSet(); for (Foo foo : c) { treeSet.add(foo); } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java index c723adc4d672..be1d973c853c 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java @@ -353,7 +353,8 @@ public class SmartType18CompletionTest extends LightFixtureCompletionTestCase { public void testFilterInaccessibleConstructors() { doAntiTest(); } - public void testCastInToArrayCallWithUnresolvedType() { doAntiTest(); } + @NeedsIndex.ForStandardLibrary(reason = "java.util.List") + public void testCastInToArrayCallWithUnresolvedType() { doTest(false); } @NeedsIndex.ForStandardLibrary public void testNoDuplicateEmptyList() { diff --git a/java/typeMigration/test/com/intellij/refactoring/TypeMigrationTest.java b/java/typeMigration/test/com/intellij/refactoring/TypeMigrationTest.java index e8a735b2d44c..ac7d301bfec1 100644 --- a/java/typeMigration/test/com/intellij/refactoring/TypeMigrationTest.java +++ b/java/typeMigration/test/com/intellij/refactoring/TypeMigrationTest.java @@ -309,7 +309,6 @@ public class TypeMigrationTest extends TypeMigrationTestBase { } // Set f; foo(AbstractSet s){f = s} -> Setf; foo(AbstractSet s){f = s} - // TODO: This test produces incorrect result since Java 8 public void testT49() { doTestFieldType("f", myFactory.createTypeFromText("java.util.Set", null)); @@ -317,7 +316,6 @@ public class TypeMigrationTest extends TypeMigrationTestBase { //captured wildcard: Set s; Set c1 = s; -> // Set s; Set c1 = s; - // TODO: This test produces incorrect result since Java 8 public void testT50() { doTestFieldType("c1", myFactory.createTypeFromText("java.util.Set", null)); diff --git a/java/typeMigration/testData/refactoring/typeMigration/t49/after/Test.items b/java/typeMigration/testData/refactoring/typeMigration/t49/after/Test.items index 530625dc5d72..b65d6bce02d1 100644 --- a/java/typeMigration/testData/refactoring/typeMigration/t49/after/Test.items +++ b/java/typeMigration/testData/refactoring/typeMigration/t49/after/Test.items @@ -1,8 +1,8 @@ Types: PsiField:f : java.util.Set -PsiParameter:s : java.util.AbstractSet +PsiParameter:s : java.util.AbstractSet PsiReferenceExpression:f : java.util.Set -PsiReferenceExpression:s : java.util.AbstractSet +PsiReferenceExpression:s : java.util.AbstractSet Conversions: diff --git a/java/typeMigration/testData/refactoring/typeMigration/t49/after/test.java b/java/typeMigration/testData/refactoring/typeMigration/t49/after/test.java index 9fa6c53700dd..91c41afa26e2 100644 --- a/java/typeMigration/testData/refactoring/typeMigration/t49/after/test.java +++ b/java/typeMigration/testData/refactoring/typeMigration/t49/after/test.java @@ -1,7 +1,7 @@ import java.util.*; class Test { Set f; - void foo(AbstractSet s) { + void foo(AbstractSet s) { f = s; } diff --git a/java/typeMigration/testData/refactoring/typeMigration/t50/after/Test.items b/java/typeMigration/testData/refactoring/typeMigration/t50/after/Test.items index aa1a3087510c..269aff16c292 100644 --- a/java/typeMigration/testData/refactoring/typeMigration/t50/after/Test.items +++ b/java/typeMigration/testData/refactoring/typeMigration/t50/after/Test.items @@ -1,7 +1,7 @@ Types: -PsiField:c : java.util.Set +PsiField:c : java.util.Set PsiField:c1 : java.util.Set -PsiReferenceExpression:c : java.util.Set +PsiReferenceExpression:c : java.util.Set Conversions: diff --git a/java/typeMigration/testData/refactoring/typeMigration/t50/after/test.java b/java/typeMigration/testData/refactoring/typeMigration/t50/after/test.java index 97bdfff7ff98..50625082114d 100644 --- a/java/typeMigration/testData/refactoring/typeMigration/t50/after/test.java +++ b/java/typeMigration/testData/refactoring/typeMigration/t50/after/test.java @@ -1,5 +1,5 @@ import java.util.*; class Test { - Set c; + Set c; Set c1 = c; } \ No newline at end of file diff --git a/java/typeMigration/testData/refactoring/typeMigrationByLongAdder/toPrimitivesAndString/after/Test.items b/java/typeMigration/testData/refactoring/typeMigrationByLongAdder/toPrimitivesAndString/after/Test.items index 0691617eddd0..9367698a1fce 100644 --- a/java/typeMigration/testData/refactoring/typeMigrationByLongAdder/toPrimitivesAndString/after/Test.items +++ b/java/typeMigration/testData/refactoring/typeMigrationByLongAdder/toPrimitivesAndString/after/Test.items @@ -25,4 +25,3 @@ this.i.intValue() -> $ New expression type changes: Fails: -i->java.util.concurrent.atomic.LongAdder diff --git a/plugins/groovy/testdata/highlighting/typecheck/TypeCheckEnum.groovy b/plugins/groovy/testdata/highlighting/typecheck/TypeCheckEnum.groovy index 65383c2811eb..38cbdc4be8a7 100644 --- a/plugins/groovy/testdata/highlighting/typecheck/TypeCheckEnum.groovy +++ b/plugins/groovy/testdata/highlighting/typecheck/TypeCheckEnum.groovy @@ -60,7 +60,7 @@ class FooEnum { e = new Object[0] e = { int a, int t -> println(a + t) } e = s - e = "aaa" =~ /aaa/ + e = "aaa" =~ /aaa/ } def enumVariable() {