lambda: return type compatibility test

This commit is contained in:
anna
2012-09-17 19:35:41 +02:00
parent 45ba1d054a
commit 4adc3db92c
3 changed files with 43 additions and 1 deletions

View File

@@ -306,7 +306,14 @@ public class LambdaUtil {
@Nullable
private static PsiType getReturnType(PsiClass psiClass, MethodSignature methodSignature) {
final PsiMethod method = getMethod(psiClass, methodSignature);
return method != null ? method.getReturnType() : null;
if (method != null) {
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) return null;
return TypeConversionUtil.getSuperClassSubstitutor(containingClass, psiClass, PsiSubstitutor.EMPTY).substitute(method.getReturnType());
}
else {
return null;
}
}
@Nullable

View File

@@ -0,0 +1,31 @@
class Test {
interface I<A, B> {
B f(A a);
}
interface II<A, B> extends I<A,B> { }
static class Foo<A> {
boolean forAll(final I<A, Boolean> f) {
return false;
}
String forAll(final II<A, String> f) {
return "";
}
String forAll2(final II<A, String> f) {
return "";
}
}
void foo(Foo<String> as, final Foo<Character> ac) {
boolean b1 = as.forAll(s -> ac.forAll(c -> false));
String s1 = as.forAll(s -> ac.forAll(c -> ""));
<error descr="Incompatible types. Found: 'java.lang.String', required: 'boolean'">boolean b2 = as.forAll(s -> ac.forAll(c -> ""));</error>
String s2 = as.forAll2(s -> ac.forAll2(<error descr="Incompatible return type boolean in lambda expression">c -> false</error>));
boolean b3 = as.forAll((I<String, Boolean>)s -> ac.forAll((I<Character, Boolean>)<error descr="Incompatible return type String in lambda expression">c -> ""</error>));
String s3 = as.forAll((II<String, String>)s -> ac.forAll((II<Character, String>)<error descr="Incompatible return type boolean in lambda expression">c -> false</error>));
}
}

View File

@@ -100,6 +100,10 @@ public class LambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testLambdaRawOrNot() throws Exception {
doTest();
}
public void testReturnTypeCompatibility1() throws Exception {
doTest();
}
public void testNoInferenceResult() throws Exception {
doTest();