From 853235664fea2ad3e9d2f6abb8124654b8762b7f Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 13 Mar 2014 14:56:59 +0100 Subject: [PATCH] new inference: resolve bounds which are not mentioned by additional constraints before starting algorithm (cherry picked from commit f4c3b393d749a7ad27936014c932492643a277e4) --- .../graphInference/InferenceSession.java | 21 ++++++++++++++++++- ...alConstraintDependsOnNonMentionedVars.java | 13 ++++++++++++ .../lambda/NewLambdaHighlightingTest.java | 4 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/AdditionalConstraintDependsOnNonMentionedVars.java diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java index b8abab6bc997..ff63763de91c 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/InferenceSession.java @@ -788,6 +788,25 @@ public class InferenceSession { } private boolean proceedWithAdditionalConstraints(Set additionalConstraints) { + final Set mentionedVars = new HashSet(); + for (ConstraintFormula constraint : additionalConstraints) { + if (constraint instanceof InputOutputConstraintFormula) { + final Set inputVariables = ((InputOutputConstraintFormula)constraint).getInputVariables(this); + if (inputVariables != null) { + mentionedVars.addAll(inputVariables); + } + final Set outputVariables = ((InputOutputConstraintFormula)constraint).getOutputVariables(inputVariables, this); + if (outputVariables != null) { + mentionedVars.addAll(outputVariables); + } + } + } + + final Set readyVariables = new LinkedHashSet(myInferenceVariables.values()); + readyVariables.removeAll(mentionedVars); + + final PsiSubstitutor siteSubstitutor = resolveBounds(readyVariables, mySiteSubstitutor); + while (!additionalConstraints.isEmpty()) { //extract subset of constraints final Set subset = buildSubset(additionalConstraints); @@ -804,7 +823,7 @@ public class InferenceSession { } //resolve input variables - PsiSubstitutor substitutor = resolveSubset(varsToResolve, mySiteSubstitutor); + PsiSubstitutor substitutor = resolveSubset(varsToResolve, siteSubstitutor); if (substitutor == null) { return false; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/AdditionalConstraintDependsOnNonMentionedVars.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/AdditionalConstraintDependsOnNonMentionedVars.java new file mode 100644 index 000000000000..9bf687c7e2a8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newLambda/AdditionalConstraintDependsOnNonMentionedVars.java @@ -0,0 +1,13 @@ +import java.util.function.Function; + +abstract class Test { + abstract Collector create(Foo foo, Function fun); + abstract Foo toArray(Ts identity); + + Collector foo(Tf t) { + return create(toArray(t), a -> a[0]); + } + + interface Collector {} + class Foo {} +} 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 566cd7d1b4f3..3401e384ed3f 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 @@ -162,6 +162,10 @@ public class NewLambdaHighlightingTest extends LightDaemonAnalyzerTestCase { doTest(); } + public void testAdditionalConstraintDependsOnNonMentionedVars() throws Exception { + doTest(); + } + private void doTest() { doTest(false); }