overload resolution: clear inference errors from nested calls when top level expression is abadoned as non-applicable (IDEA-150718; IDEA-153222)

This commit is contained in:
Anna.Kozlova
2016-03-18 15:28:00 +01:00
parent cf20da777d
commit 034012ae4e
3 changed files with 58 additions and 0 deletions

View File

@@ -110,6 +110,7 @@ public class MethodCandidateInfo extends CandidateInfo{
public int getPertinentApplicabilityLevel() {
if (myPertinentApplicabilityLevel == 0) {
myPertinentApplicabilityLevel = getPertinentApplicabilityLevelInner();
pullInferenceErrorMessagesFromSubexpressions();
}
return myPertinentApplicabilityLevel;
}
@@ -476,6 +477,41 @@ public class MethodCandidateInfo extends CandidateInfo{
}
return errorMessage;
}
private void pullInferenceErrorMessagesFromSubexpressions() {
if (myPertinentApplicabilityLevel == ApplicabilityLevel.NOT_APPLICABLE && myArgumentList instanceof PsiExpressionList) {
String errorMessage = null;
for (PsiExpression expression : ((PsiExpressionList)myArgumentList).getExpressions()) {
final String message = clearErrorMessageInSubexpressions(expression);
if (message != null) {
errorMessage = message;
}
}
if (errorMessage != null) {
setInferenceError(errorMessage);
}
}
}
private static String clearErrorMessageInSubexpressions(PsiExpression expression) {
expression = PsiUtil.skipParenthesizedExprDown(expression);
if (expression instanceof PsiConditionalExpression) {
String message = clearErrorMessageInSubexpressions(((PsiConditionalExpression)expression).getThenExpression());
if (message != null) {
return message;
}
return clearErrorMessageInSubexpressions(((PsiConditionalExpression)expression).getElseExpression());
}
else if (expression instanceof PsiCallExpression) {
final JavaResolveResult result = ((PsiCallExpression)expression).resolveMethodGenerics();
if (result instanceof MethodCandidateInfo) {
final String message = ((MethodCandidateInfo)result).getInferenceErrorMessage();
((MethodCandidateInfo)result).setInferenceError(null);
return message;
}
}
return null;
}
public CurrentCandidateProperties createProperties() {
return new CurrentCandidateProperties(this, getSiteSubstitutor(), isVarargs(), false);

View File

@@ -0,0 +1,18 @@
import java.util.*;
class MyTest {
{
Set<Integer> set = new TreeSet<>(Comparator.comparing(b -> ""));
Set<Integer> set1 = new TreeSet<>(Comparator.comparing(b -> b != null ? "" : ""));
}
void f(List<String> l) {
setCategories(l.stream().toArray(size -> new String[size]));
setCategories(l.stream().toArray(String[]::new));
}
private void setCategories(String... strings) {}
}

View File

@@ -406,6 +406,10 @@ public class GraphInferenceHighlightingTest extends LightDaemonAnalyzerTestCase
doTest();
}
public void testPullingErrorMessagesFromSubExpressionsToTheTopLevel() throws Exception {
doTest();
}
public void testVariableNamesOfNestedCalls() throws Exception {
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_8, getModule(), getTestRootDisposable());
String filePath = BASE_PATH + "/" + getTestName(false) + ".java";