From 2f80e7d9c09697273e91223075d92836dda01063 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 21 Feb 2014 13:52:02 +0100 Subject: [PATCH] new inference: reject partial computation --- .../src/com/intellij/psi/LambdaUtil.java | 24 ---------- .../ExpressionCompatibilityConstraint.java | 31 ++++++------- .../highlighting/AmbiguitySpecificReturn.java | 4 +- .../highlighting/ReturnTypeCompatibility.java | 2 +- .../lambda/newLambda/IDEA119535.java | 44 +++++++++++++++++++ .../lambda/NewLambdaHighlightingTest.java | 4 ++ 6 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/IDEA119535.java diff --git a/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java b/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java index 653b92fc494e..d4f91f9b1166 100644 --- a/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/LambdaUtil.java @@ -376,14 +376,6 @@ public class LambdaUtil { final int lambdaIdx = getLambdaIdx(expressionList, expression); if (lambdaIdx > -1) { - PsiType cachedType = null; - final Pair method = MethodCandidateInfo.getCurrentMethod(parent); - if (method != null) { - final PsiParameter[] parameters = method.first.getParameterList().getParameters(); - cachedType = lambdaIdx < parameters.length ? method.second.substitute(getNormalizedType(parameters[adjustLambdaIdx(lambdaIdx, method.first, parameters)])) : null; - if (!tryToSubstitute) return cachedType; - } - PsiElement gParent = expressionList.getParent(); if (gParent instanceof PsiAnonymousClass) { @@ -399,22 +391,6 @@ public class LambdaUtil { final int finalLambdaIdx = adjustLambdaIdx(lambdaIdx, (PsiMethod)resolve, parameters); if (finalLambdaIdx < parameters.length) { if (!tryToSubstitute) return getNormalizedType(parameters[finalLambdaIdx]); - if (cachedType != null) { - final PsiMethod interfaceMethod = getFunctionalInterfaceMethod(cachedType); - if (interfaceMethod != null) { - final PsiClassType.ClassResolveResult cachedResult = PsiUtil.resolveGenericsClassInType(cachedType); - if (paramIdx == -1) { - if (!dependsOnTypeParams(cachedType, cachedType, expression) && !dependsOnTypeParams(getFunctionalInterfaceReturnType(cachedType), cachedType, expression)) { - return cachedType; - } - } - else { - if (!dependsOnTypeParams(cachedResult.getSubstitutor().substitute(interfaceMethod.getParameterList().getParameters()[paramIdx].getType()), cachedType, expression)) { - return cachedType; - } - } - } - } return PsiResolveHelper.ourGuard.doPreventingRecursion(expression, true, new Computable() { @Override public PsiType compute() { diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ExpressionCompatibilityConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ExpressionCompatibilityConstraint.java index d6a1d834c60f..30628e36beb6 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ExpressionCompatibilityConstraint.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/ExpressionCompatibilityConstraint.java @@ -15,7 +15,6 @@ */ package com.intellij.psi.impl.source.resolve.graphInference.constraints; -import com.intellij.openapi.util.Pair; import com.intellij.psi.*; import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; import com.intellij.psi.impl.source.resolve.graphInference.InferenceVariable; @@ -78,9 +77,8 @@ public class ExpressionCompatibilityConstraint extends InputOutputConstraintForm if (myExpression instanceof PsiCallExpression) { final PsiExpressionList argumentList = ((PsiCallExpression)myExpression).getArgumentList(); if (argumentList != null) { - final Pair pair = MethodCandidateInfo.getCurrentMethod(argumentList); - final JavaResolveResult resolveResult = pair == null ? ((PsiCallExpression)myExpression).resolveMethodGenerics() : null; - final PsiMethod method = pair != null ? pair.first : (PsiMethod)resolveResult.getElement(); + final JavaResolveResult resolveResult = ((PsiCallExpression)myExpression).resolveMethodGenerics(); + final PsiMethod method = (PsiMethod)resolveResult.getElement(); PsiType returnType = null; PsiTypeParameter[] typeParams = null; if (method != null && !method.isConstructor()) { @@ -105,21 +103,18 @@ public class ExpressionCompatibilityConstraint extends InputOutputConstraintForm session.addCapturedVariable(typeParam); } PsiSubstitutor substitutor = PsiSubstitutor.EMPTY; - if (pair == null) { - if (method != null) { - //typeParams are already included - final Collection params = session.getTypeParams(); - InferenceSession callSession = new InferenceSession(params.toArray(new PsiTypeParameter[params.size()]), ((MethodCandidateInfo)resolveResult).getSiteSubstitutor(), myExpression.getManager(), myExpression); - final PsiExpression[] args = argumentList.getExpressions(); - final PsiParameter[] parameters = method.getParameterList().getParameters(); - callSession.initExpressionConstraints(parameters, args, myExpression, method); - callSession.registerConstraints(returnType, myT); - if (callSession.repeatInferencePhases(true)) { - session.liftBounds(callSession.getInferenceVariables()); - } + if (method != null) { + //typeParams are already included + final Collection params = session.getTypeParams(); + InferenceSession callSession = new InferenceSession(params.toArray(new PsiTypeParameter[params.size()]), resolveResult instanceof MethodCandidateInfo ? ((MethodCandidateInfo)resolveResult).getSiteSubstitutor() + : PsiSubstitutor.EMPTY, myExpression.getManager(), myExpression); + final PsiExpression[] args = argumentList.getExpressions(); + final PsiParameter[] parameters = method.getParameterList().getParameters(); + callSession.initExpressionConstraints(parameters, args, myExpression, method); + callSession.registerConstraints(returnType, myT); + if (callSession.repeatInferencePhases(true)) { + session.liftBounds(callSession.getInferenceVariables()); } - } else { - substitutor = pair.second; } final PsiType capturedReturnType = myExpression instanceof PsiMethodCallExpression ? PsiMethodCallExpressionImpl.captureReturnType((PsiMethodCallExpression)myExpression, method, returnType, substitutor) diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/AmbiguitySpecificReturn.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/AmbiguitySpecificReturn.java index 5b9d5d74e31e..5ecd4f5f77ff 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/AmbiguitySpecificReturn.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/AmbiguitySpecificReturn.java @@ -1,8 +1,8 @@ class IntStream { private void foo(IntStream s) { - s.map(i -> 1 << i); + s.map(i -> 1 << i); s.map(i -> 1); - s.map(i -> i); + s.map(i -> i); } public static void main(String[] args) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/ReturnTypeCompatibility.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/ReturnTypeCompatibility.java index 10cd3fffeb86..0f7aa7012061 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/ReturnTypeCompatibility.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/ReturnTypeCompatibility.java @@ -25,7 +25,7 @@ class ReturnTypeIncompatibility { } public static void main(String[] args) { - call(i-> {return i;}); + call(i-> {return i;}); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/IDEA119535.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/IDEA119535.java new file mode 100644 index 000000000000..9c883308ff1a --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/IDEA119535.java @@ -0,0 +1,44 @@ +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.stream.Collector; + +class Stuff { + public enum Type { A } + private final int value; + private final Type type; + public Stuff(int value, Type type) { + this.value = value; + this.type = type; + } + public int getValue() { + return value; + } + public Type getType() { + return type; + } +} + +class FakeErrors { + { + + Collector>> collector = + groupingBy(Stuff::getType, + reducing((d1, d2) -> { + boolean b = d1.getValue() > d2.getValue(); + return d1; + })); + } + + public static Collector> reducing(BinaryOperator op) { + return null; + } + + public static + Collector> groupingBy(Function classifier, + Collector downstream) { + return null; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/NewLambdaHighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/NewLambdaHighlightingTest.java index 727972a00e5c..a57993717ef8 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/NewLambdaHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/NewLambdaHighlightingTest.java @@ -126,6 +126,10 @@ public class NewLambdaHighlightingTest extends LightDaemonAnalyzerTestCase { doTest(); } + public void testIDEA119535() throws Exception { + doTest(); + } + private void doTest() { doTest(false); }