Prohibit using dynamic as an argument for reified type parameters

This commit is contained in:
Andrey Breslav
2014-11-25 14:25:30 +03:00
parent d5fdfcb797
commit 304d22553c
7 changed files with 43 additions and 10 deletions
@@ -385,7 +385,7 @@ public interface Errors {
DiagnosticFactory0<JetExpression> DANGLING_FUNCTION_LITERAL_ARGUMENT_SUSPECTED = DiagnosticFactory0.create(WARNING);
DiagnosticFactory1<PsiElement, TypeParameterDescriptor> TYPE_PARAMETER_AS_REIFIED = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, JetType> REIFIED_TYPE_NOTHING_SUBSTITUTION = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, JetType> REIFIED_TYPE_FORBIDDEN_SUBSTITUTION = DiagnosticFactory1.create(ERROR);
// Type inference
@@ -486,7 +486,7 @@ public class DefaultErrorMessages {
MAP.put(TYPE_PARAMETER_AS_REIFIED, "Cannot use ''{0}'' as reified type parameter. Use a class instead.", NAME);
MAP.put(REIFIED_TYPE_PARAMETER_NO_INLINE, "Only type parameters of inline functions can be reified");
MAP.put(REIFIED_TYPE_NOTHING_SUBSTITUTION, "Cannot use ''{0}'' as reified type parameter", RENDER_TYPE);
MAP.put(REIFIED_TYPE_FORBIDDEN_SUBSTITUTION, "Cannot use ''{0}'' as reified type parameter", RENDER_TYPE);
MAP.put(SUPERTYPES_FOR_ANNOTATION_CLASS, "Annotation class cannot have supertypes");
MAP.put(MISSING_VAL_ON_ANNOTATION_PARAMETER, "'val' keyword is missing on annotation parameter");
@@ -26,6 +26,7 @@ import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypesPackage;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.util.Map;
@@ -49,8 +50,8 @@ public class ReifiedTypeParameterSubstitutionCheck implements CallResolverExtens
Errors.TYPE_PARAMETER_AS_REIFIED.on(getCallElement(context), parameter)
);
}
else if (KotlinBuiltIns.getInstance().isNothingOrNullableNothing(argument)) {
context.trace.report(Errors.REIFIED_TYPE_NOTHING_SUBSTITUTION.on(getCallElement(context), argument));
else if (KotlinBuiltIns.getInstance().isNothingOrNullableNothing(argument) || TypesPackage.isDynamic(argument)) {
context.trace.report(Errors.REIFIED_TYPE_FORBIDDEN_SUBSTITUTION.on(getCallElement(context), argument));
}
}
}
@@ -0,0 +1,15 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// MODULE[js]: m1
// FILE: k.kt
fun <reified T> foo(t: T) {}
class C<reified T>(t: T)
fun test(d: dynamic) {
<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!><dynamic>(d)
<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!>(d)
<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>C<!><dynamic>(d)
<!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>C<!>(d)
}
@@ -0,0 +1,11 @@
package
internal fun </*0*/ reified T> foo(/*0*/ t: T): kotlin.Unit
internal fun test(/*0*/ d: dynamic): kotlin.Unit
internal final class C</*0*/ reified T> {
public constructor C</*0*/ reified T>(/*0*/ t: T)
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -3,12 +3,12 @@
inline fun<reified T> foo(block: () -> T): String = block().toString()
fun box() {
val a = <!REIFIED_TYPE_NOTHING_SUBSTITUTION!>array<!>(null!!)
val b = <!REIFIED_TYPE_NOTHING_SUBSTITUTION!>Array<!><Nothing?>(5) { null!! }
val c = <!REIFIED_TYPE_NOTHING_SUBSTITUTION!>foo<!>() { null!! }
val a = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>array<!>(null!!)
val b = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Array<!><Nothing?>(5) { null!! }
val c = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!>() { null!! }
val d = foo<Any> { null!! }
val e = <!REIFIED_TYPE_NOTHING_SUBSTITUTION!>foo<!> { "1" as Nothing }
val e1 = <!REIFIED_TYPE_NOTHING_SUBSTITUTION!>foo<!> { "1" as Nothing? }
val e = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!> { "1" as Nothing }
val e1 = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!> { "1" as Nothing? }
val f = <!REIFIED_TYPE_NOTHING_SUBSTITUTION!>javaClass<!><Nothing>()
val f = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>javaClass<!><Nothing>()
}
@@ -3836,6 +3836,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("reified.kt")
public void testReified() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/dynamicTypes/reified.kt");
doTest(fileName);
}
@TestMetadata("smartCast.kt")
public void testSmartCast() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/dynamicTypes/smartCast.kt");