new inference: check containing class type params if they are in bounds

This commit is contained in:
Anna Kozlova
2014-02-07 17:18:29 +01:00
parent 3bda220da4
commit 5fb1dbb2ce
3 changed files with 19 additions and 6 deletions

View File

@@ -62,8 +62,14 @@ public class GenericsHighlightUtil {
public static HighlightInfo checkInferredTypeArguments(PsiTypeParameterListOwner listOwner,
PsiElement call,
PsiSubstitutor substitutor) {
final Pair<PsiTypeParameter, PsiType> inferredTypeArgument =
GenericsUtil.findTypeParameterWithBoundError(listOwner.getTypeParameters(), substitutor, call, false);
return checkInferredTypeArguments(listOwner.getTypeParameters(), call, substitutor);
}
@Nullable
public static HighlightInfo checkInferredTypeArguments(PsiTypeParameter[] typeParameters,
PsiElement call,
PsiSubstitutor substitutor) {
final Pair<PsiTypeParameter, PsiType> inferredTypeArgument = GenericsUtil.findTypeParameterWithBoundError(typeParameters, substitutor, call, false);
if (inferredTypeArgument != null) {
final PsiType extendsType = inferredTypeArgument.second;
final PsiTypeParameter typeParameter = inferredTypeArgument.first;

View File

@@ -40,6 +40,7 @@ import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTagValue;
import com.intellij.psi.util.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.MostlySingularMultiMap;
import gnu.trove.THashMap;
@@ -1250,7 +1251,13 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
}
if (!myHolder.hasErrorResults() && method instanceof PsiTypeParameterListOwner) {
myHolder.add(GenericsHighlightUtil.checkInferredTypeArguments((PsiTypeParameterListOwner)method, expression, result.getSubstitutor()));
PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner)method).getTypeParameters();
if (method instanceof PsiMethod) {
final PsiClass containingClass = ((PsiMethod)method).getContainingClass();
assert containingClass != null : method;
typeParameters = ArrayUtil.mergeArrays(typeParameters, containingClass.getTypeParameters());
}
myHolder.add(GenericsHighlightUtil.checkInferredTypeArguments(typeParameters, expression, result.getSubstitutor()));
}
}

View File

@@ -29,10 +29,10 @@ class Test {
static void meth4(I3 s) { }
static {
meth1<error descr="'meth1(Test.I1)' in 'Test' cannot be applied to '(<method reference>)'">(Foo::new)</error>;
meth1(<error descr="Inferred type 'java.lang.String' for type parameter 'X' is not within its bound; should extend 'java.lang.Number'">Foo::new</error>);
meth2(Foo::new);
meth3<error descr="'meth3(Test.I3)' in 'Test' cannot be applied to '(<method reference>)'">(Foo::new)</error>;
meth4<error descr="Cannot resolve method 'meth4(<method reference>)'">(Foo::new)</error>;
meth3(<error descr="Inferred type 'java.lang.Object' for type parameter 'X' is not within its bound; should extend 'java.lang.Number'">Foo::new</error>);
meth4<error descr="Ambiguous method call: both 'Test.meth4(I1)' and 'Test.meth4(I2)' match">(Foo::new)</error>;
meth1(<error descr="Inferred type 'java.lang.String' for type parameter 'X' is not within its bound; should extend 'java.lang.Number'">Test::foo</error>);
meth2(Test::foo);