Fix inner generic type extraction

- Don't attribute type parameters that belong to the outer class to the inner
- Don't extract constructor generic parameters as if they were parameters of the type being instantiated
This commit is contained in:
Chris Smowton
2022-01-07 14:22:27 +00:00
committed by Ian Lynagh
parent aa0ddeb29a
commit 67e3374a23
2 changed files with 20 additions and 3 deletions

View File

@@ -1264,7 +1264,12 @@ open class KotlinFileExtractor(
val typeAccessId = extractTypeAccess(typeAccessType, callable, id, -3, e, enclosingStmt)
if (e.typeArgumentsCount > 0) {
if (e is IrConstructorCall) {
// Only extract type arguments relating to the constructed type, not the constructor itself:
e.getClassTypeArguments().forEachIndexed({ argIdx, argType ->
extractTypeAccess(argType!!, callable, typeAccessId, argIdx, e, enclosingStmt, TypeContext.GENERIC_ARGUMENT)
})
} else {
extractTypeArguments(e, typeAccessId, callable, enclosingStmt)
}
}

View File

@@ -91,9 +91,21 @@ open class KotlinUsesExtractor(
return KotlinSourceFileExtractor(newLogger, newTrapWriter, clsFile, externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
}
private fun removeOuterClassTypeArgs(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?): List<IrTypeArgument>? {
return argsIncludingOuterClasses?.let {
if (it.size > c.typeParameters.size)
it.take(c.typeParameters.size)
else
null
} ?: argsIncludingOuterClasses
}
// `typeArgs` can be null to describe a raw generic type.
// For non-generic types it will be zero-length list.
fun useClassInstance(c: IrClass, typeArgs: List<IrTypeArgument>?, inReceiverContext: Boolean = false): UseClassInstanceResult {
fun useClassInstance(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?, inReceiverContext: Boolean = false): UseClassInstanceResult {
// For all purposes ignore type arguments relating to outer classes.
val typeArgs = removeOuterClassTypeArgs(c, argsIncludingOuterClasses)
if (c.isAnonymousObject) {
logger.warn(Severity.ErrorSevere, "Unexpected access to anonymous class instance")
}
@@ -207,7 +219,7 @@ open class KotlinUsesExtractor(
// `args` can be null to describe a raw generic type.
// For non-generic types it will be zero-length list.
fun useSimpleTypeClass(c: IrClass, args: List<IrTypeArgument>?, hasQuestionMark: Boolean): TypeResults {
fun useSimpleTypeClass(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?, hasQuestionMark: Boolean): TypeResults {
if (c.isAnonymousObject) {
if (args?.isNotEmpty() == true) {
logger.warn(Severity.ErrorHigh, "Anonymous class with unexpected type arguments")