From a97212d84568c9180608f31e8cef1b51feeec177 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 30 May 2018 12:58:14 +0300 Subject: [PATCH] completion: infer ? super for inference variables with lower bounds IDEA-45672 --- .../resolve/CompletionParameterTypeInferencePolicy.java | 2 +- .../source/resolve/graphInference/InferenceSession.java | 2 +- .../smartType/ExpectedSuperOfLowerBound-out.java | 8 ++++++++ .../completion/smartType/ExpectedSuperOfLowerBound.java | 8 ++++++++ .../codeInsight/completion/SmartType18CompletionTest.java | 5 +++++ 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/ExpectedSuperOfLowerBound-out.java create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/ExpectedSuperOfLowerBound.java diff --git a/java/java-impl/src/com/intellij/psi/impl/source/resolve/CompletionParameterTypeInferencePolicy.java b/java/java-impl/src/com/intellij/psi/impl/source/resolve/CompletionParameterTypeInferencePolicy.java index 350b24646f99..9100174f0f3f 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/resolve/CompletionParameterTypeInferencePolicy.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/resolve/CompletionParameterTypeInferencePolicy.java @@ -55,7 +55,7 @@ public class CompletionParameterTypeInferencePolicy extends ProcessCandidatePara @Override public PsiType adjustInferredType(PsiManager manager, PsiType guess, ConstraintType constraintType) { - if (guess != null && !(guess instanceof PsiWildcardType)) { + if (guess != null && !(guess instanceof PsiWildcardType) && guess != PsiType.NULL) { if (constraintType == ConstraintType.SUPERTYPE) return PsiWildcardType.createExtends(manager, guess); else if (constraintType == ConstraintType.SUBTYPE) return PsiWildcardType.createSuper(manager, guess); } 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 850420f11d0d..0424f2fc8d47 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 @@ -1157,7 +1157,7 @@ public class InferenceSession { private PsiType checkBoundsConsistency(PsiSubstitutor substitutor, InferenceVariable var) { PsiType eqBound = getEqualsBound(var, substitutor); if (eqBound != PsiType.NULL && eqBound instanceof PsiPrimitiveType) return PsiType.NULL; - final PsiType lowerBound = getLowerBound(var, substitutor); + final PsiType lowerBound = myPolicy.adjustInferredType(myManager, getLowerBound(var, substitutor), ConstraintType.SUBTYPE); final PsiType upperBound = getUpperBound(var, substitutor); PsiType type; if (eqBound != PsiType.NULL && (myErased || eqBound != null)) { diff --git a/java/java-tests/testData/codeInsight/completion/smartType/ExpectedSuperOfLowerBound-out.java b/java/java-tests/testData/codeInsight/completion/smartType/ExpectedSuperOfLowerBound-out.java new file mode 100644 index 000000000000..99f32602da26 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/ExpectedSuperOfLowerBound-out.java @@ -0,0 +1,8 @@ +import java.util.*; +class MyTest { + void addIfNotNull(T t, Collection collection) {} + { + List l = null; + addIfNotNull("someString", l); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartType/ExpectedSuperOfLowerBound.java b/java/java-tests/testData/codeInsight/completion/smartType/ExpectedSuperOfLowerBound.java new file mode 100644 index 000000000000..ebd849392420 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/ExpectedSuperOfLowerBound.java @@ -0,0 +1,8 @@ +import java.util.*; +class MyTest { + void addIfNotNull(T t, Collection collection) {} + { + List l = null; + addIfNotNull("someString", ); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java index 02a6821afb32..d8c9221ba182 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartType18CompletionTest.java @@ -219,6 +219,11 @@ public void testConvertToObjectStream() { } public void testCollectionsEmptyMap() { doTest(true); } + public void testExpectedSuperOfLowerBound() { + configureByTestName(); + myFixture.complete(CompletionType.SMART, 1); + checkResultByFile("/" + getTestName(false) + "-out.java"); + } private void doTest() { doTest(true);