most specific check: accept lambdas when target type is type parameter (IDEA-124725)

This commit is contained in:
Anna Kozlova
2014-05-05 17:06:34 +04:00
parent 78e9bb7840
commit 288ccfa195
3 changed files with 19 additions and 0 deletions

View File

@@ -153,6 +153,10 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
if (methodParameters.length == 0) continue;
final PsiParameter param = i < methodParameters.length ? methodParameters[i] : methodParameters[methodParameters.length - 1];
final PsiType paramType = param.getType();
// http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2.1
// A lambda expression or a method reference expression is potentially compatible with a type variable if the type variable is a type parameter of the candidate method.
final PsiClass paramClass = PsiUtil.resolveClassInType(paramType);
if (paramClass instanceof PsiTypeParameter && ((PsiTypeParameter)paramClass).getOwner() == method) continue;
if (!lambdaExpression.isAcceptable(((MethodCandidateInfo)conflict).getSubstitutor(false).substitute(paramType), lambdaExpression.hasFormalParameterTypes())) {
iterator.remove();
}

View File

@@ -0,0 +1,11 @@
import java.util.function.Supplier;
class Test {
public static void main(String... args) {
c<error descr="Cannot resolve method 'c(<lambda expression>, <lambda expression>)'">(() -> 3, () -> 10)</error>;
}
public static <T> void c(Supplier<T> s1, Supplier<T> s2) {}
public static <T> void c(Supplier<T> s1, T value) {}
}

View File

@@ -99,6 +99,10 @@ public class MostSpecificResolutionTest extends LightDaemonAnalyzerTestCase {
doTest();
}
public void testTargetTypeParameter() throws Exception {
doTest(false);
}
private void doTest() {
doTest(true);
}