mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 05:21:29 +07:00
lambda: conditional inference updated
This commit is contained in:
@@ -19,6 +19,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.scope.MethodProcessorSetupFailedException;
|
||||
import com.intellij.psi.scope.processor.MethodCandidatesProcessor;
|
||||
import com.intellij.psi.scope.util.PsiScopesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
@@ -100,7 +101,7 @@ public class GraphInferencePolicy extends ProcessCandidateParameterTypeInference
|
||||
|
||||
public static void forget(PsiElement parent) {
|
||||
if (parent instanceof PsiExpression) {
|
||||
final PsiElement gParent = parent.getParent();
|
||||
PsiElement gParent = PsiUtil.skipParenthesizedExprUp(parent.getParent());
|
||||
if (gParent instanceof PsiExpressionList) {
|
||||
final PsiElement ggParent = gParent.getParent();
|
||||
if (ggParent instanceof PsiCallExpression) {
|
||||
|
||||
@@ -43,12 +43,11 @@ public class ProcessCandidateParameterTypeInferencePolicy extends DefaultParamet
|
||||
PsiCallExpression contextCall,
|
||||
PsiTypeParameter typeParameter) {
|
||||
PsiExpression[] expressions = expressionList.getExpressions();
|
||||
PsiElement parent = innerMethodCall.getParent();
|
||||
while (parent instanceof PsiParenthesizedExpression) {
|
||||
innerMethodCall = (PsiExpression)parent;
|
||||
PsiElement parent = innerMethodCall;
|
||||
while (parent.getParent() instanceof PsiParenthesizedExpression) {
|
||||
parent = parent.getParent();
|
||||
}
|
||||
int i = ArrayUtil.find(expressions, innerMethodCall);
|
||||
int i = ArrayUtil.find(expressions, parent);
|
||||
if (i < 0) return null;
|
||||
PsiMethod owner = (PsiMethod)typeParameter.getOwner();
|
||||
if (owner == null) return null;
|
||||
|
||||
@@ -1202,7 +1202,7 @@ public class PsiResolveHelperImpl implements PsiResolveHelper {
|
||||
final PsiExpression elseExpression = ((PsiConditionalExpression)parent).getElseExpression();
|
||||
final PsiType[] paramTypes = {((PsiMethod)typeParameter.getOwner()).getReturnType()};
|
||||
if (methodCall.equals(PsiUtil.skipParenthesizedExprDown(elseExpression)) && thenExpression != null) {
|
||||
final PsiType thenType = ourGraphGuard.doPreventingRecursion(thenExpression, true, new Computable<PsiType>() {
|
||||
final PsiType thenType = ourGraphGuard.doPreventingRecursion(parent, true, new Computable<PsiType>() {
|
||||
@Override
|
||||
public PsiType compute() {
|
||||
return thenExpression.getType();
|
||||
@@ -1212,7 +1212,7 @@ public class PsiResolveHelperImpl implements PsiResolveHelper {
|
||||
pair = inferTypeForMethodTypeParameterInner(typeParameter, paramTypes, new PsiType[] {thenType}, substitutor, null, policy);
|
||||
}
|
||||
} else if (methodCall.equals(PsiUtil.skipParenthesizedExprDown(thenExpression)) && elseExpression != null) {
|
||||
final PsiType elseType = ourGraphGuard.doPreventingRecursion(elseExpression, true, new Computable<PsiType>() {
|
||||
final PsiType elseType = ourGraphGuard.doPreventingRecursion(parent, true, new Computable<PsiType>() {
|
||||
@Override
|
||||
public PsiType compute() {
|
||||
return elseExpression.getType();
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
public class Main {
|
||||
|
||||
interface Function<T, U> {
|
||||
T fun(U t);
|
||||
}
|
||||
|
||||
interface Sortable<X> {
|
||||
Sortable<X> sort(Comparator<X> c);
|
||||
}
|
||||
|
||||
static class Inner {
|
||||
String foo() { return ""; }
|
||||
}
|
||||
|
||||
<T2, U2> Comparator<U2> comparing(Function<T2, U2> mapper) { return null; }
|
||||
|
||||
void testAssignmentContext(Sortable<Inner> sortable, boolean cond) {
|
||||
Comparator<Inner> comparing0 = comparing(Inner::foo);
|
||||
Comparator<Inner> comparing = comparing((p) -> p.foo());
|
||||
Sortable<Inner> p1 = sortable.sort(comparing);
|
||||
Sortable<Inner> p2 = sortable.sort(comparing(x->x.foo()));
|
||||
Sortable<Inner> p3 = sortable.sort(cond ? comparing(Inner::foo) : comparing(x -> x.foo()));
|
||||
Sortable<Inner> p4 = sortable.sort((cond ? comparing(Inner::foo) : comparing(x -> x.foo())));
|
||||
}
|
||||
|
||||
void testMethodContext(Sortable<Inner> list, boolean cond) {
|
||||
testMethodContext(list.sort(comparing(Inner::foo)), true);
|
||||
testMethodContext(list.sort(comparing(x->x.foo())), true);
|
||||
testMethodContext(list.sort(cond ? comparing(Inner::foo) : comparing(x -> x.foo())), true);
|
||||
testMethodContext(list.sort((cond ? comparing(Inner::foo) : comparing(x -> x.foo()))), true);
|
||||
}
|
||||
|
||||
interface Comparator<T> {
|
||||
int compare(T o1, T o2);
|
||||
}
|
||||
}
|
||||
@@ -228,6 +228,10 @@ public class LambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testConditionalInferenceFromOppositePart() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() throws Exception {
|
||||
doTest(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user