From 304926005f5fe0b31a0eb5484dfd63ba6c7c714d Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 27 Nov 2012 21:09:17 +0400 Subject: [PATCH] Proper errors for cases like 'is Map' supported in 'when' --- .../jetbrains/jet/lang/resolve/TypeResolver.java | 10 +++++++++- .../tests/generics/RawTypeInIsPattern.kt | 13 +++++++++++++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 5 +++++ .../when/afterQualifiedArrayList.kt | 7 +++++++ .../addStarProjections/when/afterQualifiedList.kt | 7 +++++++ .../addStarProjections/when/afterQualifiedMap.kt | 7 +++++++ .../addStarProjections/when/afterUnqualifiedList.kt | 7 +++++++ .../addStarProjections/when/afterUnqualifiedMap.kt | 7 +++++++ .../when/afterUnqualifiedMapOneArg.kt | 9 +++++++++ .../when/beforeQualifiedArrayList.kt | 7 +++++++ .../addStarProjections/when/beforeQualifiedList.kt | 7 +++++++ .../addStarProjections/when/beforeQualifiedMap.kt | 7 +++++++ .../when/beforeUnqualifiedList.kt | 7 +++++++ .../addStarProjections/when/beforeUnqualifiedMap.kt | 7 +++++++ .../when/beforeUnqualifiedMapOneArg.kt | 9 +++++++++ 15 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/generics/RawTypeInIsPattern.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/afterQualifiedArrayList.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/afterQualifiedList.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/afterQualifiedMap.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/afterUnqualifiedList.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/afterUnqualifiedMap.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/afterUnqualifiedMapOneArg.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/beforeQualifiedArrayList.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/beforeQualifiedList.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/beforeQualifiedMap.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedList.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedMap.kt create mode 100644 idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedMapOneArg.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeResolver.java index 61a997290a2..b997404cfce 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/TypeResolver.java @@ -143,7 +143,7 @@ public class TypeResolver { else { if (actualArgumentCount != expectedArgumentCount) { if (actualArgumentCount == 0) { - if (rhsOfIsExpression(type)) { + if (rhsOfIsExpression(type) || rhsOfIsPattern(type)) { trace.report(NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION.on(type, expectedArgumentCount, allStarProjectionsString(typeConstructor))); } else { @@ -252,6 +252,14 @@ public class TypeResolver { return false; } + private static boolean rhsOfIsPattern(@NotNull JetUserType type) { + // Look for the is-pattern containing this type + JetWhenConditionIsPattern outerPattern = PsiTreeUtil.getParentOfType(type, JetWhenConditionIsPattern.class, false, JetExpression.class); + if (outerPattern == null) return false; + // We are interested only in the outermost type on the RHS + return type.getParent() == outerPattern.getTypeRef(); + } + private JetScope getScopeForTypeParameter(final TypeParameterDescriptor typeParameterDescriptor, boolean checkBounds) { if (checkBounds) { return typeParameterDescriptor.getUpperBoundsAsType().getMemberScope(); diff --git a/compiler/testData/diagnostics/tests/generics/RawTypeInIsPattern.kt b/compiler/testData/diagnostics/tests/generics/RawTypeInIsPattern.kt new file mode 100644 index 00000000000..bd67f5d6967 --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/RawTypeInIsPattern.kt @@ -0,0 +1,13 @@ +public fun foo(a: Any, b: Map) { + when (a) { + is Map -> {} + is Map -> {} + is Map -> {} + is Map<*, *> -> {} + is Map<> -> {} + is List<Map> -> {} + is List -> {} + is Int -> {} + else -> {} + } +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index ba5c319680f..9d41815b24b 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -1774,6 +1774,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/generics/RawTypeInIsExpression.kt"); } + @TestMetadata("RawTypeInIsPattern.kt") + public void testRawTypeInIsPattern() throws Exception { + doTest("compiler/testData/diagnostics/tests/generics/RawTypeInIsPattern.kt"); + } + @TestMetadata("RecursiveUpperBoundCheck.kt") public void testRecursiveUpperBoundCheck() throws Exception { doTest("compiler/testData/diagnostics/tests/generics/RecursiveUpperBoundCheck.kt"); diff --git a/idea/testData/quickfix/addStarProjections/when/afterQualifiedArrayList.kt b/idea/testData/quickfix/addStarProjections/when/afterQualifiedArrayList.kt new file mode 100644 index 00000000000..09f124cc65f --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/afterQualifiedArrayList.kt @@ -0,0 +1,7 @@ +// "Add '<*>'" "true" +public fun foo(a: Any) { + when (a) { + is java.util.ArrayList<*> -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/afterQualifiedList.kt b/idea/testData/quickfix/addStarProjections/when/afterQualifiedList.kt new file mode 100644 index 00000000000..4aad32f22b8 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/afterQualifiedList.kt @@ -0,0 +1,7 @@ +// "Add '<*>'" "true" +public fun foo(a: Any) { + when (a) { + is jet.List<*> -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/afterQualifiedMap.kt b/idea/testData/quickfix/addStarProjections/when/afterQualifiedMap.kt new file mode 100644 index 00000000000..ab79b4e4c1d --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/afterQualifiedMap.kt @@ -0,0 +1,7 @@ +// "Add '<*, *>'" "true" +public fun foo(a: Any) { + when (a) { + is jet.Map<*, *> -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedList.kt b/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedList.kt new file mode 100644 index 00000000000..10073649198 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedList.kt @@ -0,0 +1,7 @@ +// "Add '<*>'" "true" +public fun foo(a: Any) { + when (a) { + is List<*> -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedMap.kt b/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedMap.kt new file mode 100644 index 00000000000..e8620316570 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedMap.kt @@ -0,0 +1,7 @@ +// "Add '<*, *>'" "true" +public fun foo(a: Any) { + when (a) { + is Map<*, *> -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedMapOneArg.kt b/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedMapOneArg.kt new file mode 100644 index 00000000000..2dcb5c7632c --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/afterUnqualifiedMapOneArg.kt @@ -0,0 +1,9 @@ +// "Add '<*, *>'" "false" +// "Add '<*>'" "false" +// ERROR: 2 type arguments expected +public fun foo(a: Any) { + when (a) { + is Map -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/beforeQualifiedArrayList.kt b/idea/testData/quickfix/addStarProjections/when/beforeQualifiedArrayList.kt new file mode 100644 index 00000000000..825f8ec4eff --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/beforeQualifiedArrayList.kt @@ -0,0 +1,7 @@ +// "Add '<*>'" "true" +public fun foo(a: Any) { + when (a) { + is java.util.ArrayList -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/beforeQualifiedList.kt b/idea/testData/quickfix/addStarProjections/when/beforeQualifiedList.kt new file mode 100644 index 00000000000..41a032ada06 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/beforeQualifiedList.kt @@ -0,0 +1,7 @@ +// "Add '<*>'" "true" +public fun foo(a: Any) { + when (a) { + is jet.List -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/beforeQualifiedMap.kt b/idea/testData/quickfix/addStarProjections/when/beforeQualifiedMap.kt new file mode 100644 index 00000000000..5850ed4c5c8 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/beforeQualifiedMap.kt @@ -0,0 +1,7 @@ +// "Add '<*, *>'" "true" +public fun foo(a: Any) { + when (a) { + is jet.Map -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedList.kt b/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedList.kt new file mode 100644 index 00000000000..f19ca829ab6 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedList.kt @@ -0,0 +1,7 @@ +// "Add '<*>'" "true" +public fun foo(a: Any) { + when (a) { + is List -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedMap.kt b/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedMap.kt new file mode 100644 index 00000000000..ccef96ef221 --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedMap.kt @@ -0,0 +1,7 @@ +// "Add '<*, *>'" "true" +public fun foo(a: Any) { + when (a) { + is Map -> {} + else -> {} + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedMapOneArg.kt b/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedMapOneArg.kt new file mode 100644 index 00000000000..42b2681a5ff --- /dev/null +++ b/idea/testData/quickfix/addStarProjections/when/beforeUnqualifiedMapOneArg.kt @@ -0,0 +1,9 @@ +// "Add '<*, *>'" "false" +// "Add '<*>'" "false" +// ERROR: 2 type arguments expected +public fun foo(a: Any) { + when (a) { + is Map -> {} + else -> {} + } +} \ No newline at end of file