[FIR] Report CANNOT_CHECK_FOR_ERASED independent of reified parameters

This changes the logic so that Foo<T> is reported no matter if T is
reified or not. Even for Array<reified T> to align K2 with K1 logic.

#KT-55903 Fixed
This commit is contained in:
Kirill Rakhman
2023-04-28 16:44:24 +02:00
committed by Space Team
parent bbee881b5b
commit 3bfb0866cb
4 changed files with 49 additions and 17 deletions
@@ -141,12 +141,11 @@ fun isCastErased(supertype: ConeKotlinType, subtype: ConeKotlinType, context: Ch
// downcasting to a non-reified type parameter is always erased
if (isNonReifiedTypeParameter) return true
// downcasting to a reified type parameter is never erased
else if (subtype is ConeTypeParameterType) return false
// Check that we are actually casting to a generic type
// NOTE: this does not account for 'as Array<List<T>>'
if (subtype.allParameterReified()) return false
val staticallyKnownSubtype = findStaticallyKnownSubtype(supertype, subtype, context)
val regularClassSymbol = subtype.toRegularClassSymbol(context.session) ?: return true
val staticallyKnownSubtype = findStaticallyKnownSubtype(supertype, regularClassSymbol, context)
// If the substitution failed, it means that the result is an impossible type, e.g. something like Out<in Foo>
// In this case, we can't guarantee anything, so the cast is considered to be erased
@@ -156,10 +155,6 @@ fun isCastErased(supertype: ConeKotlinType, subtype: ConeKotlinType, context: Ch
return !AbstractTypeChecker.isSubtypeOf(context.session.typeContext, staticallyKnownSubtype, subtype, stubTypesEqualToAnything = false)
}
private fun ConeKotlinType.allParameterReified(): Boolean {
return typeArguments.all { (it.type as? ConeTypeParameterType)?.lookupTag?.typeParameterSymbol?.isReified == true }
}
/**
* Remember that we are trying to cast something of type `supertype` to `subtype`.
@@ -178,7 +173,7 @@ private fun ConeKotlinType.allParameterReified(): Boolean {
*/
fun findStaticallyKnownSubtype(
supertype: ConeKotlinType,
subtype: ConeKotlinType,
subTypeClassSymbol: FirRegularClassSymbol,
context: CheckerContext
): ConeKotlinType {
assert(!supertype.isMarkedNullable) { "This method only makes sense for non-nullable types" }
@@ -188,8 +183,7 @@ fun findStaticallyKnownSubtype(
// Assume we are casting an expression of type Collection<Foo> to List<Bar>
// First, let's make List<T>, where T is a type variable
val subtypeWithVariables = subtype.toRegularClassSymbol(session)!!
val subtypeWithVariablesType = subtypeWithVariables.defaultType()
val subtypeWithVariablesType = subTypeClassSymbol.defaultType()
// Now, let's find a supertype of List<T> that is a Collection of something,
// in this case it will be Collection<T>
@@ -214,7 +208,7 @@ fun findStaticallyKnownSubtype(
normalizedType.typeConstructor(typeContext)
).firstOrNull()
val variables: List<FirTypeParameterSymbol> = subtypeWithVariables.typeParameterSymbols
val variables: List<FirTypeParameterSymbol> = subTypeClassSymbol.typeParameterSymbols
val substitution = if (supertypeWithVariables != null) {
// Now, let's try to unify Collection<T> and Collection<Foo> solution is a map from T to Foo
@@ -15,10 +15,25 @@ fun <T, S : T> test(x: T?, y: S, z: T) {
null <!UNCHECKED_CAST!>as S<!>
}
inline fun <reified T> test(x: T?) {
class Box<T>
inline fun <reified T> test(x: T?, a: Any) {
x is T
null as T
null as T?
a is T
a as T
a is <!CANNOT_CHECK_FOR_ERASED!>Box<T><!>
a is <!CANNOT_CHECK_FOR_ERASED!>Array<T><!>
a <!UNCHECKED_CAST!>as Box<T><!>
a <!UNCHECKED_CAST!>as Array<T><!>
a is <!CANNOT_CHECK_FOR_ERASED!>Box<List<T>><!>
a is <!CANNOT_CHECK_FOR_ERASED!>Array<List<T>><!>
a <!UNCHECKED_CAST!>as Box<List<T>><!>
a <!UNCHECKED_CAST!>as Array<List<T>><!>
}
fun <T> foo(x: List<T>, y: List<T>?) {
@@ -15,13 +15,28 @@ fun <T, S : T> test(x: T?, y: S, z: T) {
null <!UNCHECKED_CAST!>as S<!>
}
inline fun <reified T> test(x: T?) {
class Box<T>
inline fun <reified T> test(x: T?, a: Any) {
x is T
null as T
null <!USELESS_CAST!>as T?<!>
a is T
a as T
a is <!CANNOT_CHECK_FOR_ERASED!>Box<T><!>
a is <!CANNOT_CHECK_FOR_ERASED!>Array<T><!>
a <!UNCHECKED_CAST!>as Box<T><!>
a <!UNCHECKED_CAST!>as Array<T><!>
a is <!CANNOT_CHECK_FOR_ERASED!>Box<List<T>><!>
a is <!CANNOT_CHECK_FOR_ERASED!>Array<List<T>><!>
a <!UNCHECKED_CAST!>as Box<List<T>><!>
a <!UNCHECKED_CAST!>as Array<List<T>><!>
}
fun <T> foo(x: List<T>, y: List<T>?) {
<!USELESS_IS_CHECK!>x is List<T><!>
y is List<T>
}
}
@@ -1,5 +1,13 @@
package
public fun </*0*/ T> foo(/*0*/ x: kotlin.collections.List<T>, /*1*/ y: kotlin.collections.List<T>?): kotlin.Unit
public inline fun </*0*/ reified T> test(/*0*/ x: T?): kotlin.Unit
public fun </*0*/ T, /*1*/ S : T> test(/*0*/ x: T?, /*1*/ y: S, /*2*/ z: T): kotlin.Unit
public inline fun </*0*/ reified T> test(/*0*/ x: T?, /*1*/ a: kotlin.Any): kotlin.Unit
public final class Box</*0*/ T> {
public constructor Box</*0*/ 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
}