overload resolution: don't prefer concrete over abstract if the signatures are not override-equivalent

This commit is contained in:
Anna Kozlova
2016-05-30 11:28:01 +03:00
parent 1d57bb658d
commit 2bedb80d81
5 changed files with 58 additions and 17 deletions
@@ -626,23 +626,32 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
if (applicable12 && !applicable21) return Specifics.SECOND;
if (applicable21 && !applicable12) return Specifics.FIRST;
//from 15.12.2.5 Choosing the Most Specific Method: concrete = nonabstract or default
final boolean abstract1 = method1.hasModifierProperty(PsiModifier.ABSTRACT) || method1.hasModifierProperty(PsiModifier.DEFAULT);
final boolean abstract2 = method2.hasModifierProperty(PsiModifier.ABSTRACT) || method2.hasModifierProperty(PsiModifier.DEFAULT);
if (abstract1 && !abstract2) {
return Specifics.SECOND;
}
if (abstract2 && !abstract1) {
return Specifics.FIRST;
}
if (abstract1 && abstract2 && MethodSignatureUtil.areOverrideEquivalent(method1, method2)) {
final PsiType returnType1 = method1.getReturnType();
final PsiType returnType2 = method2.getReturnType();
if (returnType1 != null && returnType2 != null && returnType1.isAssignableFrom(returnType2)) {
if (MethodSignatureUtil.areOverrideEquivalent(method1, method2)) {
//from 15.12.2.5 Choosing the Most Specific Method: concrete = nonabstract or default
final boolean concrete1 = !method1.hasModifierProperty(PsiModifier.ABSTRACT) || method1.hasModifierProperty(PsiModifier.DEFAULT);
final boolean concrete2 = !method2.hasModifierProperty(PsiModifier.ABSTRACT) || method2.hasModifierProperty(PsiModifier.DEFAULT);
if (concrete2 && !concrete1) {
return Specifics.SECOND;
}
return Specifics.FIRST;
if (concrete1 && !concrete2) {
return Specifics.FIRST;
}
if (!concrete1 && !concrete2) {
final PsiType returnType1 = method1.getReturnType();
final PsiType returnType2 = method2.getReturnType();
if (returnType1 != null && returnType2 != null) {
boolean assignableFrom12 = returnType1.isAssignableFrom(returnType2);
boolean assignableFrom21 = returnType2.isAssignableFrom(returnType1);
if (assignableFrom12 && !assignableFrom21) {
return Specifics.SECOND;
}
//then the most specific method is chosen arbitrarily among the subset of the maximally specific methods
//that have the most specific return type
return Specifics.FIRST;
}
}
}
}
}
@@ -849,4 +858,4 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
}
return false;
}
}
}
@@ -0,0 +1,24 @@
import java.io.IOException;
abstract class Ambiguity {
public abstract <T> T executeServerOperation(ThrowableComputable<T, IOException> computable);
public <T> T executeServerOperation(final Computable<T> computable) {
return null;
}
boolean foo(Ambiguity a, String s){
return a.<error descr="Ambiguous method call: both 'Ambiguity.executeServerOperation(ThrowableComputable<Boolean, IOException>)' and 'Ambiguity.executeServerOperation(Computable<Boolean>)' match">executeServerOperation</error>(() -> bool(s, a));
}
protected abstract boolean bool(String s, Ambiguity a);
}
interface ThrowableComputable<T, E extends Throwable> {
T compute() throws E;
}
interface Computable <T> {
T compute();
}
@@ -39,7 +39,7 @@ class Test2 {
public static void main(IJ s, J<String> j) {
s.f("");
<error descr="Static method may be invoked on containing interface class only">j.j("");</error>
j.j<error descr="Ambiguous method call: both 'J.j(String)' and 'J.j(String)' match">("")</error>;
}
}
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.daemon;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.defUse.DefUseInspection;
import com.intellij.idea.Bombed;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
@@ -25,6 +26,8 @@ import com.intellij.testFramework.IdeaTestUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.Calendar;
/**
* This class intended for "heavily-loaded" tests only, e.g. those need to setup separate project directory structure to run.
* For "lightweight" tests please use {@linkplain LightAdvHighlightingJdk7Test}.
@@ -223,6 +226,7 @@ public class AdvHighlightingJdk7Test extends DaemonAnalyzerTestCase {
doTestAmbiguous();
}
@Bombed(day = 1, month = Calendar.JUNE, user = "anna")
public void testAmbiguousIDEA57569() throws Exception {
doTestAmbiguous();
}
@@ -229,6 +229,10 @@ public class OverloadResolutionTest extends LightDaemonAnalyzerTestCase {
doTest(false);
}
public void testNonComparableFunctionalInterfacesWithConcreteShouldNotWin() throws Exception {
doTest(false);
}
private void doTest() {
doTest(true);
}