inference variables: ensure variable is not mixed with type parameter for recursive calls (IDEA-147639)

This commit is contained in:
Anna Kozlova
2015-11-09 16:13:15 +01:00
parent 289aea179d
commit c798f3ab6b
3 changed files with 51 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ package com.intellij.psi.impl.source.resolve.graphInference;
import com.intellij.psi.*;
import com.intellij.psi.impl.light.LightTypeParameter;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
@@ -150,7 +151,12 @@ public class InferenceVariable extends LightTypeParameter {
@Override
public boolean isEquivalentTo(PsiElement another) {
return this == another || getDelegate() == another;
if (this == another) return true;
if (getDelegate() == another && myContext != null && !PsiTreeUtil.isAncestor(((PsiTypeParameter)another).getOwner(), myContext, false)) {
return true;
}
return false;
}
@Override
@@ -163,6 +169,11 @@ public class InferenceVariable extends LightTypeParameter {
return getDelegate().toString();
}
@Override
public PsiTypeParameterListOwner getOwner() {
return null;
}
public PsiElement getCallContext() {
return myContext;
}

View File

@@ -0,0 +1,35 @@
import java.util.*;
import java.util.stream.Collectors;
class Test {
private <S> List<S> bar(List<S> list) {
return null;
}
private <E> List<List<E>> foo(List<E> l) {
foo(bar(l));
final List<List<Object>> perms = foo(null);
return null;
}
public static <E> List<E> pipe(E head, List<E> tail) {
List<E> newList = new ArrayList<>(tail);
newList.add(0, head);
return newList;
}
public static <E> List<E> subtract(List<E> list, E e) {
List<E> newList = new ArrayList<>(list);
newList.remove(e);
return newList;
}
public static <E> List<List<E>> perms(List<E> l) {
return l.isEmpty()
? Collections.singletonList(Collections.emptyList())
: l.stream().flatMap(h -> perms(subtract(l, h)).stream()
.map(t -> pipe(h, t))).collect(Collectors.toList());
}
public static void main(String[] args) {
System.out.println(perms(Arrays.asList("a", "b", "c")));
}
}

View File

@@ -319,6 +319,10 @@ public class GraphInferenceHighlightingTest extends LightDaemonAnalyzerTestCase
doTest();
}
public void testRecursiveCallsWithNestedInference() throws Exception {
doTest();
}
private void doTest() throws Exception {
doTest(false);
}