From 7b91cc930091b933520f69e40b1f0f43a7ea1b8c Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 14 Jan 2020 15:12:00 +0700 Subject: [PATCH] Ignore annotations from expression constraints. We don't account them correctly anyway: if two constraints produce the same type with different annotations the second one will be completely ignored. And it's completely unclear how to combine them properly. This depends on annotation semantics. GitOrigin-RevId: 5d9c9818254e1fc991f144f136c292833753442b --- .../ExpressionCompatibilityConstraint.java | 3 +++ .../dataFlow/fixture/TypeUseLambdaReturn.java | 25 +++++++++++++++++++ .../DataFlowInspection8Test.java | 4 +++ 3 files changed, 32 insertions(+) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/TypeUseLambdaReturn.java 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 0f8edeeab4e8..1bf06dfd52ad 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 @@ -30,6 +30,9 @@ public class ExpressionCompatibilityConstraint extends InputOutputConstraintForm if (!PsiPolyExpressionUtil.isPolyExpression(myExpression)) { PsiType exprType = myExpression.getType(); + if (exprType != null) { + exprType = exprType.annotate(TypeAnnotationProvider.EMPTY); + } if (session.isProperType(myT)) { final boolean assignmentCompatible = exprType == null || TypeConversionUtil.isAssignable(myT, exprType); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/TypeUseLambdaReturn.java b/java/java-tests/testData/inspection/dataFlow/fixture/TypeUseLambdaReturn.java new file mode 100644 index 000000000000..34b94980e47e --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/TypeUseLambdaReturn.java @@ -0,0 +1,25 @@ +import ambiguous.*; + +class Test { + void run1() { + String result = compute(() -> { + return Math.random() > 0.5 ? getResult() : getResult2(); + }); + if (result == null) {} + } + + void run() { + String result = compute(() -> { + return Math.random() > 0.5 ? getResult() : null; + }); + if (result == null) {} + } + + native @NotNull String getResult(); + native @Nullable String getResult2(); + public native static T compute(Computable action); +} + +interface Computable { + T compute(); +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java index cd8fe2486457..f588d08c47ba 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java @@ -155,6 +155,10 @@ public class DataFlowInspection8Test extends DataFlowInspectionTestCase { public void testCapturedWildcardNotNull() { doTest(); } public void testVarargNotNull() { doTestWithCustomAnnotations(); } public void testIgnoreNullabilityOnPrimitiveCast() { doTestWithCustomAnnotations();} + public void testTypeUseLambdaReturn() { + setupTypeUseAnnotations("ambiguous", myFixture); + doTest(); + } public void testArrayComponentAndMethodAnnotationConflict() { setupAmbiguousAnnotations("withTypeUse", myFixture);