From c20dfc0165dc177206afe11d6c3778688c6206b8 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Thu, 28 Nov 2019 09:56:31 +0100 Subject: [PATCH] java inference: ignore method reference constraints when starting from them EA-209460 - IAE: ObjectUtils.$$$reportNull$$$ GitOrigin-RevId: 8476b832fcc1c85c10aed6ea56f65645bfd02b03 --- .../graphInference/InferenceSession.java | 4 +-- ...reateMethodFromMethodRefApplicability.java | 29 +++++++++++++++++++ .../lambda/NewMethodRefHighlightingTest.java | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newMethodRef/CreateMethodFromMethodRefApplicability.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 74af44e228fe..fa5f387541fd 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 @@ -393,7 +393,7 @@ public class InferenceSession { final PsiType parameterType = nestedSubstitutor.substitute(getParameterType(parameters, i, siteSubstitutor, varargs)); if (!isPertinentToApplicability(arg, parentMethod)) { ExpressionCompatibilityConstraint compatibilityConstraint = new ExpressionCompatibilityConstraint(arg, parameterType); - if (arg instanceof PsiLambdaExpression && ignoreLambdaConstraintTree(arg) || dependsOnIgnoredConstraint(ignoredConstraints, compatibilityConstraint)) { + if (arg instanceof PsiFunctionalExpression && ignoreLambdaConstraintTree(arg) || dependsOnIgnoredConstraint(ignoredConstraints, compatibilityConstraint)) { ignoredConstraints.add(compatibilityConstraint); continue; } @@ -440,7 +440,7 @@ public class InferenceSession { public static boolean ignoreLambdaConstraintTree(PsiExpression arg) { for (PsiElement expr : MethodCandidateInfo.ourOverloadGuard.currentStack()) { - if (PsiTreeUtil.getParentOfType(expr, PsiLambdaExpression.class) == arg) { + if (PsiTreeUtil.getParentOfType(expr, PsiFunctionalExpression.class, false) == arg) { return true; } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newMethodRef/CreateMethodFromMethodRefApplicability.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newMethodRef/CreateMethodFromMethodRefApplicability.java new file mode 100644 index 000000000000..4ce8a26f649d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/newMethodRef/CreateMethodFromMethodRefApplicability.java @@ -0,0 +1,29 @@ + +import java.util.*; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +class DemoApplicationTests { + + void test2() { + var a = Stream.of("") + .map(s -> s.substring(1)) + .map(s -> s.length()) + .map(l1 -> String.valueOf(l1)).collect(Collectors.toList()); + + Stream.of("") + .map(l -> a.get(l.length())) + .map(s -> new DemoApplicationTests()).findAny() + .map(d -> d.foo(this::bar)); + } + + void foo(Consumer c) {} + + void foo(Function c) {} + + void bar(long j) {} + + void bar(int i) {} +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/NewMethodRefHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/NewMethodRefHighlightingTest.java index ee12c15f1d3a..918418cb3aa2 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/NewMethodRefHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/NewMethodRefHighlightingTest.java @@ -196,6 +196,7 @@ public class NewMethodRefHighlightingTest extends LightDaemonAnalyzerTestCase { public void testDontIgnoreIncompatibilitiesDuringFirstApplicabilityCheck() { doTest(); } public void testCaptureOnDedicatedParameterOfSecondSearch() { doTest(); } public void testVoidConflict() { doTest(); } + public void testCreateMethodFromMethodRefApplicability() { doTest(); } public void testErrorMessageOnTopCallWhenFunctionalInterfaceIsNotInferred() { doTest(); } private void doTest() {