diff --git a/java/java-psi-api/src/com/intellij/psi/util/TypesDistinctProver.java b/java/java-psi-api/src/com/intellij/psi/util/TypesDistinctProver.java index 820b3ff0c57b..f6200d1f7a91 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/TypesDistinctProver.java +++ b/java/java-psi-api/src/com/intellij/psi/util/TypesDistinctProver.java @@ -157,12 +157,17 @@ public final class TypesDistinctProver { if (rejectInconsistentRaw && level > 0 && extendsBound1 instanceof PsiClassType && extendsBound2 instanceof PsiClassType && (((PsiClassType)extendsBound1).isRaw() ^ ((PsiClassType)extendsBound2).isRaw())) return true; + if (type1.equals(type2)) return false; + if (level > 1) return true; return proveExtendsBoundsDistinct(type1, type2, boundClass1, boundClass2); } return provablyDistinct(extendsBound1, extendsBound2, 1); } if (type2.isExtends()) return provablyDistinct(type2, type1, rejectInconsistentRaw, level); - if (type1.isExtends() && !type2.isBounded() && level > 1) return PsiUtil.resolveClassInType(type1.getExtendsBound()) instanceof PsiTypeParameter; + if (type1.isExtends() && !type2.isBounded() && level > 1) { + PsiType bound = type1.getExtendsBound(); + return bound instanceof PsiClassType || bound instanceof PsiArrayType; + } if (type1.isExtends() && type2.isSuper()) { final PsiType extendsBound = type1.getExtendsBound(); final PsiType superBound = type2.getSuperBound(); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/InvalidCastWithNestedGeneric.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/InvalidCastWithNestedGeneric.java new file mode 100644 index 000000000000..8d57bab5031a --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/InvalidCastWithNestedGeneric.java @@ -0,0 +1,86 @@ +import java.util.Set; + +@SuppressWarnings("unused") +public class InvalidCast { + public static void main(String[] args) { + + } + + static class X1 { + } + + static class X2 extends X1 { + } + + public void t1(Set> t) { + Set> t2 = (Set>) t; + } + + public void t2(Set> t) { + Set> t2 = (Set>) t; + } + + public void t3(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t4(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t5(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t6(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t7(Set> t) { + Set> t2 = (Set>) t; + } + + public void t8(Set> t) { + Set> t2 = (Set>) t; + } + + public void t9(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t10(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t11(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t12(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t13(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t14(Set> t) { + Set> t2 = (Set>) t; //error + } + + public void t15(Set> t) { + Set> t2 = (Set>) t; // error + } + + public void t16(Set> t) { + Set> t2 = (Set>) t; // error + } + + public void t17(Set> t) { + Set> t2 = (Set>) t; + } + + public void t18(Set> t) { + Set> t2 = (Set>) t; // error + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/GenericsHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/GenericsHighlightingTest.java index 1afe39946182..b1ebb7771bc8 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/GenericsHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/GenericsHighlightingTest.java @@ -455,4 +455,6 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase { public void testOnlyUncheckedWarningCastWithDuplicatedArguments(){doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, true);} public void testCastUnboxingConversionWithWidening(){doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, true);} public void testVarCaptureForLoop(){doTest(LanguageLevel.JDK_10, JavaSdkVersion.JDK_10, true);} + + public void testInvalidCastWithNestedGeneric(){doTest(LanguageLevel.JDK_1_8, JavaSdkVersion.JDK_1_8, true);} } \ No newline at end of file