[java-psi] PsiMethodCallExpressionImpl: on ambiguous call use upper bound of types if possible

GitOrigin-RevId: 7de48b2542e2fe0a0f8503ab3dd3c4d34784747c
This commit is contained in:
Tagir Valeev
2025-10-10 07:53:21 +00:00
committed by intellij-monorepo-bot
parent 84f0a34d82
commit 3a2c9be754
5 changed files with 36 additions and 7 deletions
@@ -164,7 +164,8 @@ public class PsiMethodCallExpressionImpl extends ExpressionPsiElement implements
theOnly = type;
}
else if (!theOnly.equals(type)) {
return null;
theOnly = GenericsUtil.getLeastUpperBound(type, theOnly, file.getManager());
if (theOnly == null) return null;
}
}
@@ -0,0 +1,8 @@
class Main {
String foo(int x) { return "1";}
StringBuilder foo(String x) { return new StringBuilder();}
void test() {
foo(1.25).le<caret>
}
}
@@ -0,0 +1,8 @@
class Main {
String foo(int x) { return "1";}
StringBuilder foo(String x) { return new StringBuilder();}
void test() {
foo(1.25).length()<caret>
}
}
@@ -3251,4 +3251,11 @@ public class NormalCompletionTest extends NormalCompletionTestCase {
myFixture.completeBasic();
assertTrue(myFixture.getLookupElementStrings().contains("A"));
}
@NeedsIndex.ForStandardLibrary
public void testAmbiguousCallLeastUpperBound() {
configureByTestName();
myFixture.completeBasic();
myFixture.checkResultByFile(getTestName(false) + "_after.java");
}
}
@@ -345,19 +345,24 @@ public class Java8ExpressionsCheckTest extends LightDaemonAnalyzerTestCase {
PsiMethodCallExpression outerCall = (PsiMethodCallExpression) innerCall.getParent().getParent();
assertAmbiguous(outerCall);
assertAmbiguous(innerCall);
assertAmbiguous(outerCall, "java.util.Collection<? extends java.lang.Object>");
assertAmbiguous(innerCall, "java.util.Collection<? extends java.lang.Object>");
dropCaches();
assertAmbiguous(innerCall);
assertAmbiguous(outerCall);
assertAmbiguous(innerCall, "java.util.Collection<? extends java.lang.Object>");
assertAmbiguous(outerCall, "java.util.Collection<? extends java.lang.Object>");
}
private static void assertAmbiguous(PsiMethodCallExpression call) {
private static void assertAmbiguous(PsiMethodCallExpression call, @Nullable String expectedType) {
assertNull(call.getText(), call.resolveMethod());
assertSize(2, call.multiResolve(false));
assertNull(call.getText(), call.getType());
PsiType type = call.getType();
if (expectedType == null) {
assertNull(call.getText(), type);
} else {
assertEquals(call.getText(), expectedType, type.getCanonicalText());
}
}
public void testAdditionalConstraintsBasedOnLambdaResolution() {