From 1466b0155ea02cc4269f19f45a10fe0af41a005f Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Mon, 11 Apr 2016 20:18:14 +0200 Subject: [PATCH] bad code green: reject return type constraint if unchecked conversion was applied during applicability check and return type is type parameter --- .../resolve/graphInference/InferenceSession.java | 6 ++++++ .../genericsHighlighting8/IDEA56754.java | 2 +- .../UncheckedWarningsInsideIncorporationPhase.java | 2 +- ...ypeInReturnConstraintWithUncheckedConversion.java | 12 ++++++++++++ .../lambda/GraphInferenceHighlightingTest.java | 4 ++++ 5 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/graphInference/PrimitiveTypeInReturnConstraintWithUncheckedConversion.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 2a4a42aed5fd..5319fcaa6e19 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 @@ -689,6 +689,12 @@ public class InferenceSession { public void registerReturnTypeConstraints(PsiType returnType, PsiType targetType) { returnType = substituteWithInferenceVariables(returnType); if (myErased) { + final InferenceVariable inferenceVariable = getInferenceVariable(returnType); + if (inferenceVariable != null) { + final PsiSubstitutor substitutor = resolveSubset(Collections.singletonList(inferenceVariable), mySiteSubstitutor); + returnType = substitutor.substitute(inferenceVariable); + if (returnType == null) return; + } addConstraint(new TypeCompatibilityConstraint(targetType, TypeConversionUtil.erasure(returnType))); } else if (FunctionalInterfaceParameterizationUtil.isWildcardParameterized(returnType)) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/IDEA56754.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/IDEA56754.java index ee489aab75ad..7ce30a00c928 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/IDEA56754.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/IDEA56754.java @@ -1,5 +1,5 @@ class Foo { public T bar(Class type, String str) { - return Enum.valueOf(type, str); + return Enum.valueOf(type, str); } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/UncheckedWarningsInsideIncorporationPhase.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/UncheckedWarningsInsideIncorporationPhase.java index 7fca5e334bb3..0d3c34cd90e7 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/UncheckedWarningsInsideIncorporationPhase.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/UncheckedWarningsInsideIncorporationPhase.java @@ -4,7 +4,7 @@ abstract class Group { } public T get(Key key) { - return getCategory(key); + return getCategory(key); } public abstract > R getCategory(Key key); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/graphInference/PrimitiveTypeInReturnConstraintWithUncheckedConversion.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/graphInference/PrimitiveTypeInReturnConstraintWithUncheckedConversion.java new file mode 100644 index 000000000000..a5f0d6bf5909 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/graphInference/PrimitiveTypeInReturnConstraintWithUncheckedConversion.java @@ -0,0 +1,12 @@ +import java.util.List; + +class Test { + T foo(List l) { + return l.get(0); + } + + void m(List l){ + boolean foo = foo(l); + String s = foo(l); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GraphInferenceHighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GraphInferenceHighlightingTest.java index 1e18b3fee52a..97a71067c856 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GraphInferenceHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GraphInferenceHighlightingTest.java @@ -429,6 +429,10 @@ public class GraphInferenceHighlightingTest extends LightDaemonAnalyzerTestCase doTest(); } + public void testPrimitiveTypeInReturnConstraintWithUncheckedConversion() throws Exception { + doTest(); + } + public void testVariableNamesOfNestedCalls() throws Exception { IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_8, getModule(), getTestRootDisposable()); String filePath = BASE_PATH + "/" + getTestName(false) + ".java";