Accept and amend check for anonymous types with type parameters

This commit is contained in:
Chris Smowton
2022-03-17 18:54:44 +00:00
committed by Ian Lynagh
parent c0f3988aaa
commit 96908d153d
9 changed files with 119 additions and 14 deletions

View File

@@ -1126,17 +1126,6 @@ open class KotlinFileExtractor(
extractNewExprForLocalFunction(ids, id, locId, enclosingCallable, enclosingStmt)
} else {
// Returns true if type is C<T1, T2, ...> where C is declared `class C<T1, T2, ...> { ... }`
fun isUnspecialised(type: IrSimpleType) =
type.classifier.owner is IrClass &&
(type.classifier.owner as IrClass).typeParameters.zip(type.arguments).all { paramAndArg ->
(paramAndArg.second as? IrTypeProjection)?.let {
// Type arg refers to the class' own type parameter?
it.variance == Variance.INVARIANT &&
it.type.classifierOrNull?.owner === paramAndArg.first
} ?: false
}
val methodId =
if (drType != null && extractClassTypeArguments && drType is IrSimpleType && !isUnspecialised(drType)) {
if (isBigArityFunctionInvoke) {

View File

@@ -388,8 +388,10 @@ open class KotlinUsesExtractor(
// For non-generic types it will be zero-length list.
fun useSimpleTypeClass(c: IrClass, args: List<IrTypeArgument>?, hasQuestionMark: Boolean): TypeResults {
if (c.isAnonymousObject) {
if (args?.isNotEmpty() == true) {
logger.error("Anonymous class with unexpected type arguments")
args?.let {
if (it.isNotEmpty() && !isUnspecialised(c, it)) {
logger.error("Unexpected specialised instance of generic anonymous class")
}
}
return useAnonymousClass(c)

View File

@@ -191,4 +191,19 @@ fun IrTypeArgument.withQuestionMark(b: Boolean): IrTypeArgument =
else -> this
}
typealias TypeSubstitution = (IrType, KotlinUsesExtractor.TypeContext, IrPluginContext) -> IrType
typealias TypeSubstitution = (IrType, KotlinUsesExtractor.TypeContext, IrPluginContext) -> IrType
// Returns true if type is C<T1, T2, ...> where C is declared `class C<T1, T2, ...> { ... }`
fun isUnspecialised(classType: IrClass, args: List<IrTypeArgument>) =
classType.typeParameters.zip(args).all { paramAndArg ->
(paramAndArg.second as? IrTypeProjection)?.let {
// Type arg refers to the class' own type parameter?
it.variance == Variance.INVARIANT &&
it.type.classifierOrNull?.owner === paramAndArg.first
} ?: false
}
// Returns true if type is C<T1, T2, ...> where C is declared `class C<T1, T2, ...> { ... }`
fun isUnspecialised(type: IrSimpleType) = (type.classifier.owner as? IrClass)?.let {
isUnspecialised(it, type.arguments)
} ?: false