[java-incomplete-model] Assume that any unresolved class-type is assignable to java.lang.Object

Partially improves inference in incomplete mode

GitOrigin-RevId: 1d9c7c75afd524ea02980be8f96c19fba1eaaf54
This commit is contained in:
Tagir Valeev
2024-06-24 10:56:00 +02:00
committed by intellij-monorepo-bot
parent 5835df8dd6
commit 838cf2d73c
17 changed files with 26 additions and 45 deletions

View File

@@ -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;

View File

@@ -0,0 +1,8 @@
public class SomeClass {
{
java.util.List<Unresolved> l;
Unresolved[] a = l.toArray((Unresolved[]) <caret>expr);
}
}

View File

@@ -2,6 +2,10 @@ import java.util.*;
import <info descr="Not resolved until the project is fully loaded">foo</info>.<info descr="Not resolved until the project is fully loaded">bar</info>.<info descr="Not resolved until the project is fully loaded">baz</info>.*;
class Test {
void testDiamond() {
List<<info descr="Not resolved until the project is fully loaded">X</info>> list = new ArrayList<>();
}
void test() {
List<String> data = new ArrayList<>(
Arrays.asList(<info descr="Not resolved until the project is fully loaded">a</info>.<info descr="Not resolved until the project is fully loaded">X</info>, <info descr="Not resolved until the project is fully loaded">a</info>.<info descr="Not resolved until the project is fully loaded">Y</info>));

View File

@@ -1,14 +0,0 @@
// "Cast argument to 'Map<Foo, Bar>'" "true-preview"
import java.util.*;
class X {
void run(Foo single) {}
void run(Map<Foo, Bar> map) {}
void test(Bar bar) {
run((Map<Foo, Bar>) Collections.singletonMap(getFoo(), bar));
}
native Foo getFoo();
}

View File

@@ -1,14 +0,0 @@
// "Cast argument to 'Map<Foo, Bar>'" "true-preview"
import java.util.*;
class X {
void run(Foo single) {}
void run(Map<Foo, Bar> map) {}
void test(Bar bar) {
run((Map<Foo, Bar>) Collections.singletonMap(getFoo(), bar));
}
native Foo getFoo();
}

View File

@@ -1,4 +1,4 @@
// "Cast argument to 'Map<Foo, Bar>'" "true-preview"
// "Cast argument to 'Map<Foo, Bar>'" "false"
import java.util.*;

View File

@@ -1,4 +1,4 @@
// "Cast argument to 'Map<Foo, Bar>'" "true-preview"
// "Cast argument to 'Map<Foo, Bar>'" "false"
import java.util.*;

View File

@@ -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());
}
}
}

View File

@@ -115,7 +115,7 @@ public class Main {
// Unresolved reference
void f(Collection<? extends Foo> c) {
R treeSet = new TreeSet();
TreeSet treeSet = new TreeSet();
for (Foo foo : c) {
treeSet.add(foo);
}

View File

@@ -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() {

View File

@@ -309,7 +309,6 @@ public class TypeMigrationTest extends TypeMigrationTestBase {
}
// Set<String> f; foo(AbstractSet<String> s){f = s} -> Set<Integer>f; foo(AbstractSet<Integer> s){f = s}
// TODO: This test produces incorrect result since Java 8
public void testT49() {
doTestFieldType("f",
myFactory.createTypeFromText("java.util.Set<B>", null));
@@ -317,7 +316,6 @@ public class TypeMigrationTest extends TypeMigrationTestBase {
//captured wildcard: Set<? extends JComponent> s; Set<? extends JComponent> c1 = s; ->
// Set<? extends JButton> s; Set<? extends JButton> c1 = s;
// TODO: This test produces incorrect result since Java 8
public void testT50() {
doTestFieldType("c1",
myFactory.createTypeFromText("java.util.Set<? extends JButton>", null));

View File

@@ -1,8 +1,8 @@
Types:
PsiField:f : java.util.Set<B>
PsiParameter:s : java.util.AbstractSet<E>
PsiParameter:s : java.util.AbstractSet<B>
PsiReferenceExpression:f : java.util.Set<B>
PsiReferenceExpression:s : java.util.AbstractSet<E>
PsiReferenceExpression:s : java.util.AbstractSet<B>
Conversions:

View File

@@ -1,7 +1,7 @@
import java.util.*;
class Test {
Set<B> f;
void foo(AbstractSet<E> s) {
void foo(AbstractSet<B> s) {
f = s;
}

View File

@@ -1,7 +1,7 @@
Types:
PsiField:c : java.util.Set<E>
PsiField:c : java.util.Set<JButton>
PsiField:c1 : java.util.Set<? extends JButton>
PsiReferenceExpression:c : java.util.Set<E>
PsiReferenceExpression:c : java.util.Set<JButton>
Conversions:

View File

@@ -1,5 +1,5 @@
import java.util.*;
class Test {
Set<E> c;
Set<JButton> c;
Set<? extends JButton> c1 = c;
}

View File

@@ -25,4 +25,3 @@ this.i.intValue() -> $
New expression type changes:
Fails:
i->java.util.concurrent.atomic.LongAdder

View File

@@ -60,7 +60,7 @@ class FooEnum {
<error descr="Cannot assign 'Object[]' to 'E'">e</error> = new Object[0]
<error descr="Cannot assign 'Closure<Void>' to 'E'">e</error> = { int a, int t -> println(a + t) }
<error descr="Cannot assign 'Object' to 'E'">e</error> = s
<error descr="Cannot return 'Matcher' from method returning 'Object'"><error descr="Cannot assign 'Matcher' to 'E'">e</error> = "aaa" =~ /aaa/</error>
<error descr="Cannot assign 'Matcher' to 'E'">e</error> = "aaa" =~ /aaa/
}
def enumVariable() {