Proper errors for cases like 'is Map' supported in 'when'

This commit is contained in:
Andrey Breslav
2012-11-27 21:09:17 +04:00
parent 85f7b4069f
commit 304926005f
15 changed files with 115 additions and 1 deletions
@@ -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();
@@ -0,0 +1,13 @@
public fun foo(a: Any, <!UNUSED_PARAMETER!>b<!>: <!WRONG_NUMBER_OF_TYPE_ARGUMENTS!>Map<!>) {
when (a) {
is Map<!WRONG_NUMBER_OF_TYPE_ARGUMENTS!><Int><!> -> {}
is <!NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION!>Map<!> -> {}
is Map<out Any?, Any?> -> {}
is Map<*, *> -> {}
is Map<<!SYNTAX!><!>> -> {}
is <!CANNOT_CHECK_FOR_ERASED!>List<<!WRONG_NUMBER_OF_TYPE_ARGUMENTS!>Map<!>><!> -> {}
is <!NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION!>List<!> -> {}
is Int -> {}
else -> {}
}
}
@@ -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");
@@ -0,0 +1,7 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
when (a) {
is java.util.ArrayList<*> -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
when (a) {
is jet.List<*> -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*, *>'" "true"
public fun foo(a: Any) {
when (a) {
is jet.Map<*, *> -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
when (a) {
is List<*> -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*, *>'" "true"
public fun foo(a: Any) {
when (a) {
is Map<*, *> -> {}
else -> {}
}
}
@@ -0,0 +1,9 @@
// "Add '<*, *>'" "false"
// "Add '<*>'" "false"
// ERROR: 2 type arguments expected
public fun foo(a: Any) {
when (a) {
is Map<Int> -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
when (a) {
is java.util.Array<caret>List -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
when (a) {
is jet.List<caret> -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*, *>'" "true"
public fun foo(a: Any) {
when (a) {
is jet.Map<caret> -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
when (a) {
is List<caret> -> {}
else -> {}
}
}
@@ -0,0 +1,7 @@
// "Add '<*, *>'" "true"
public fun foo(a: Any) {
when (a) {
is Map<caret> -> {}
else -> {}
}
}
@@ -0,0 +1,9 @@
// "Add '<*, *>'" "false"
// "Add '<*>'" "false"
// ERROR: 2 type arguments expected
public fun foo(a: Any) {
when (a) {
is <caret>Map<Int> -> {}
else -> {}
}
}