From d336733d3e0316b30155e122459c2fc70b54845c Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Wed, 2 Nov 2016 15:43:49 +0100 Subject: [PATCH] ensure bounds are promoted on derived type otherwise bounds from super could appear on unbounded wildcards (IDEA-162882) --- .../StrictSubtypingConstraint.java | 17 ++++++++++---- .../BoundsPromotionForDerivedType.java | 23 +++++++++++++++++++ .../lambda/GenericsHighlighting8Test.java | 5 +++- 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/BoundsPromotionForDerivedType.java diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/StrictSubtypingConstraint.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/StrictSubtypingConstraint.java index 115a67f4d9ce..298228c3f408 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/StrictSubtypingConstraint.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/graphInference/constraints/StrictSubtypingConstraint.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import com.intellij.psi.impl.source.resolve.graphInference.InferenceBound; import com.intellij.psi.impl.source.resolve.graphInference.InferenceSession; import com.intellij.psi.impl.source.resolve.graphInference.InferenceVariable; import com.intellij.psi.util.InheritanceUtil; -import com.intellij.psi.util.PsiUtil; import com.intellij.psi.util.TypeConversionUtil; import java.util.HashSet; @@ -141,15 +140,23 @@ public class StrictSubtypingConstraint implements ConstraintFormula { if (sType == null) return false; final PsiClassType.ClassResolveResult SResult = sType.resolveGenerics(); PsiClass SClass = SResult.getElement(); + + if (SClass == null) return false; + + PsiSubstitutor substitutor = SResult.getSubstitutor(); + for (PsiTypeParameter typeParameter : SClass.getTypeParameters()) { + substitutor = substitutor.put(typeParameter, substitutor.substituteWithBoundsPromotion(typeParameter)); + } + if (((PsiClassType)myT).isRaw()) { - return SClass != null && InheritanceUtil.isInheritorOrSelf(SClass, CClass, true); + return InheritanceUtil.isInheritorOrSelf(SClass, CClass, true); } final PsiSubstitutor tSubstitutor = TResult.getSubstitutor(); - final PsiSubstitutor sSubstitutor = SClass != null ? TypeConversionUtil.getClassSubstitutor(CClass, SClass, SResult.getSubstitutor()) : null; + final PsiSubstitutor sSubstitutor = TypeConversionUtil.getClassSubstitutor(CClass, SClass, substitutor); if (sSubstitutor != null) { for (PsiTypeParameter parameter : CClass.getTypeParameters()) { final PsiType tSubstituted = tSubstitutor.substitute(parameter); - final PsiType sSubstituted = sSubstitutor.substituteWithBoundsPromotion(parameter); + final PsiType sSubstituted = sSubstitutor.substitute(parameter); if (tSubstituted == null ^ sSubstituted == null) { return false; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/BoundsPromotionForDerivedType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/BoundsPromotionForDerivedType.java new file mode 100644 index 000000000000..d2945040607a --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/BoundsPromotionForDerivedType.java @@ -0,0 +1,23 @@ + +import java.util.Collection; + +interface NumberCollection extends Collection { +} + +interface IntegerCollection extends NumberCollection {} +interface IntegerCollection1 extends NumberCollection {} + +class Test { + > Collection filter(Collection input) { + return null; + } + + public void foo(Collection> input) { + Collection filtered = filter(input); + } + + public void foo1(Collection> input) { + Collection filtered = filter(input); + } +} + diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GenericsHighlighting8Test.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GenericsHighlighting8Test.java index 630785397c4b..c74c36172b7a 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GenericsHighlighting8Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GenericsHighlighting8Test.java @@ -21,7 +21,6 @@ import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection; import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection; import com.intellij.codeInspection.unusedImport.UnusedImportLocalInspection; import com.intellij.openapi.projectRoots.JavaSdkVersion; -import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.LanguageLevelProjectExtension; import com.intellij.pom.java.LanguageLevel; import com.intellij.testFramework.IdeaTestUtil; @@ -1009,4 +1008,8 @@ public class GenericsHighlighting8Test extends LightDaemonAnalyzerTestCase { public void testTypeParameterBoundsWithSubstitutionWhenMethodHierarchyIsChecked() throws Exception { doTest(); } + + public void testBoundsPromotionForDerivedType() throws Exception { + doTest(); + } }