Type parameter is considered capable of taking any value for the purpose of cast possibility check #KT-6611 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-06-07 19:14:16 +03:00
parent 99acbf16b2
commit 4b6b35dadf
5 changed files with 40 additions and 1 deletions
@@ -40,6 +40,11 @@ object CastDiagnosticsUtil {
// This is an oversimplification (which does not render the method incomplete):
// we consider any type parameter capable of taking any value, which may be made more precise if we considered bounds
if (TypeUtils.isTypeParameter(lhsType) || TypeUtils.isTypeParameter(rhsType)) return true
if (lhsType.constructor == rhsType.constructor) {
if (lhsType.arguments.all { TypeUtils.isTypeParameter(it.type) }) return true
if (rhsType.arguments.all { TypeUtils.isTypeParameter(it.type) }) return true
}
if (isFinal(lhsType) || isFinal(rhsType)) return false
if (isTrait(lhsType) || isTrait(rhsType)) return true
return false
@@ -0,0 +1,14 @@
// See also: KT-6611 (cast can never succeed: Class<T> -> Class<Any>)
class Class<T>(val name: String, val instance: T)
fun <T> test(clazz: Class<T>) {
println((<!UNCHECKED_CAST!>clazz as Class<Any><!>).name)
}
fun use() {
test(Class("String", ""))
}
fun println(s: String) = s
@@ -0,0 +1,14 @@
package
public fun println(/*0*/ s: kotlin.String): kotlin.String
public fun </*0*/ T> test(/*0*/ clazz: Class<T>): kotlin.Unit
public fun use(): kotlin.Unit
public final class Class</*0*/ T> {
public constructor Class</*0*/ T>(/*0*/ name: kotlin.String, /*1*/ instance: T)
public final val instance: T
public final val name: kotlin.String
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
}
@@ -1,5 +1,5 @@
// !LANGUAGE: -BoundCallableReferences
// !DIAGNOSTICS: -CAST_NEVER_SUCCEEDS
// !DIAGNOSTICS: -UNCHECKED_CAST
import kotlin.reflect.KClass
@@ -2328,6 +2328,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("AsWithOtherParameter.kt")
public void testAsWithOtherParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/AsWithOtherParameter.kt");
doTest(fileName);
}
@TestMetadata("constants.kt")
public void testConstants() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/constants.kt");