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

View File

@@ -418,6 +418,90 @@ classes.kt:
# 127| 1: [ExprStmt] <Expr>;
# 127| 0: [ClassInstanceExpr] new (...)
# 127| -3: [TypeAccess] Object
generic_anonymous.kt:
# 0| [CompilationUnit] generic_anonymous
# 0| 1: [Class] Generic_anonymousKt
# 11| 1: [Method] stringIdentity
#-----| 4: (Parameters)
# 11| 0: [Parameter] s
# 11| 5: [BlockStmt] { ... }
# 11| 0: [ReturnStmt] return ...
# 11| 0: [MethodAccess] get(...)
# 11| -1: [ClassInstanceExpr] new Generic<String>(...)
# 11| -3: [TypeAccess] Generic<String>
# 11| 0: [TypeAccess] String
# 11| 0: [VarAccess] s
# 13| 2: [Method] intIdentity
#-----| 4: (Parameters)
# 13| 0: [Parameter] i
# 13| 5: [BlockStmt] { ... }
# 13| 0: [ReturnStmt] return ...
# 13| 0: [MethodAccess] get(...)
# 13| -1: [ClassInstanceExpr] new Generic<Integer>(...)
# 13| -3: [TypeAccess] Generic<Integer>
# 13| 0: [TypeAccess] Integer
# 13| 0: [VarAccess] i
# 1| 2: [Class,GenericType,ParameterizedType] Generic
#-----| -2: (Generic Parameters)
# 1| 0: [TypeVariable] T
# 1| 1: [Constructor] Generic
#-----| 4: (Parameters)
# 1| 0: [Parameter] t
# 1| 5: [BlockStmt] { ... }
# 1| 0: [SuperConstructorInvocationStmt] super(...)
# 1| 1: [BlockStmt] { ... }
# 1| 0: [ExprStmt] <Expr>;
# 1| 0: [KtInitializerAssignExpr] ...=...
# 1| 0: [VarAccess] t
# 3| 1: [ExprStmt] <Expr>;
# 3| 0: [KtInitializerAssignExpr] ...=...
# 3| 0: [VarAccess] x
# 1| 2: [Method] getT
# 1| 5: [BlockStmt] { ... }
# 1| 0: [ReturnStmt] return ...
# 1| 0: [VarAccess] this.t
# 1| -1: [ThisAccess] this
# 1| 2: [FieldDeclaration] T t;
# 1| -1: [TypeAccess] T
# 1| 0: [VarAccess] t
# 3| 4: [FieldDeclaration] new Object(...) { ... } x;
# 3| -1: [TypeAccess] new Object(...) { ... }
# 3| 0: [TypeAccess] T
# 3| 0: [StmtExpr] <Stmt>
# 3| 0: [BlockStmt] { ... }
# 3| 0: [LocalTypeDeclStmt] class ...
# 3| 0: [AnonymousClass,LocalClass] new Object(...) { ... }
# 3| 1: [Constructor]
# 3| 5: [BlockStmt] { ... }
# 3| 0: [SuperConstructorInvocationStmt] super(...)
# 3| 1: [BlockStmt] { ... }
# 4| 0: [ExprStmt] <Expr>;
# 4| 0: [KtInitializerAssignExpr] ...=...
# 4| 0: [VarAccess] member
# 4| 2: [Method] getMember
# 4| 5: [BlockStmt] { ... }
# 4| 0: [ReturnStmt] return ...
# 4| 0: [VarAccess] this.member
# 4| -1: [ThisAccess] this
# 4| 2: [FieldDeclaration] T member;
# 4| -1: [TypeAccess] T
# 4| 0: [MethodAccess] getT(...)
# 4| -1: [ThisAccess] Generic.this
# 4| 0: [TypeAccess] Generic
# 3| 1: [ExprStmt] <Expr>;
# 3| 0: [ClassInstanceExpr] new (...)
# 3| -3: [TypeAccess] Object
# 3| 5: [Method] getX
# 3| 5: [BlockStmt] { ... }
# 3| 0: [ReturnStmt] return ...
# 3| 0: [VarAccess] this.x
# 3| -1: [ThisAccess] this
# 7| 6: [Method] get
# 7| 5: [BlockStmt] { ... }
# 7| 0: [ReturnStmt] return ...
# 7| 0: [MethodAccess] getMember(...)
# 7| -1: [MethodAccess] getX(...)
# 7| -1: [ThisAccess] this
local_anonymous.kt:
# 0| [CompilationUnit] local_anonymous
# 3| 1: [Class] Class1

View File

@@ -6,5 +6,6 @@
| classes.kt:85:16:85:25 | new Object(...) { ... } | classes.kt:85:16:85:25 | new (...) | | classes.kt:85:16:85:25 | Object | classes.kt:85:16:85:25 | class ... |
| classes.kt:89:16:89:44 | new Interface3<Integer>(...) { ... } | classes.kt:89:16:89:44 | new (...) | | classes.kt:89:16:89:44 | Interface3<Integer> | classes.kt:89:16:89:44 | class ... |
| classes.kt:127:16:134:9 | new Object(...) { ... } | classes.kt:127:16:134:9 | new (...) | | classes.kt:127:16:134:9 | Object | classes.kt:127:16:134:9 | class ... |
| generic_anonymous.kt:3:19:5:3 | new Object(...) { ... } | generic_anonymous.kt:3:19:5:3 | new (...) | | generic_anonymous.kt:3:19:5:3 | Object | generic_anonymous.kt:3:19:5:3 | class ... |
| local_anonymous.kt:5:16:7:9 | new Object(...) { ... } | local_anonymous.kt:5:16:7:9 | new (...) | | local_anonymous.kt:5:16:7:9 | Object | local_anonymous.kt:5:16:7:9 | class ... |
| local_anonymous.kt:29:31:35:5 | new Object(...) { ... } | local_anonymous.kt:29:31:35:5 | new (...) | | local_anonymous.kt:29:31:35:5 | Object | local_anonymous.kt:29:31:35:5 | class ... |

View File

@@ -34,6 +34,9 @@
| classes.kt:119:13:121:13 | Local2 | C1$Local2 | final, private |
| classes.kt:127:16:134:9 | new Object(...) { ... } | <anonymous class> | final, private |
| classes.kt:129:17:131:17 | Local3 | C1$$Local3 | final, private |
| generic_anonymous.kt:0:0:0:0 | Generic_anonymousKt | Generic_anonymousKt | |
| generic_anonymous.kt:1:1:9:1 | Generic | Generic | final, private |
| generic_anonymous.kt:3:19:5:3 | new Object(...) { ... } | <anonymous class> | final, private |
| local_anonymous.kt:3:1:36:1 | Class1 | LocalAnonymous.Class1 | final, public |
| local_anonymous.kt:5:16:7:9 | new Object(...) { ... } | <anonymous class> | final, private |
| local_anonymous.kt:11:9:11:24 | | Class1$ | final, private |

View File

@@ -34,6 +34,8 @@ superCall
| classes.kt:119:13:121:13 | super(...) |
| classes.kt:127:16:134:9 | super(...) |
| classes.kt:129:17:131:17 | super(...) |
| generic_anonymous.kt:1:1:9:1 | super(...) |
| generic_anonymous.kt:3:19:5:3 | super(...) |
| local_anonymous.kt:3:1:36:1 | super(...) |
| local_anonymous.kt:5:16:7:9 | super(...) |
| local_anonymous.kt:11:9:11:24 | super(...) |

View File

@@ -4,6 +4,10 @@
| classes.kt:119:13:121:13 | Local2<Integer> | 0 | file://<external>/Integer.class:0:0:0:0 | Integer |
| classes.kt:129:17:131:17 | Local3 | 0 | classes.kt:129:30:129:31 | T1 |
| classes.kt:129:17:131:17 | Local3<Integer> | 0 | file://<external>/Integer.class:0:0:0:0 | Integer |
| generic_anonymous.kt:1:1:9:1 | Generic | 0 | generic_anonymous.kt:1:23:1:23 | T |
| generic_anonymous.kt:1:1:9:1 | Generic<Integer> | 0 | file://<external>/Integer.class:0:0:0:0 | Integer |
| generic_anonymous.kt:1:1:9:1 | Generic<String> | 0 | file://<external>/String.class:0:0:0:0 | String |
| generic_anonymous.kt:1:1:9:1 | Generic<T> | 0 | generic_anonymous.kt:1:23:1:23 | T |
| superChain.kt:1:1:1:33 | SuperChain1 | 0 | superChain.kt:1:24:1:25 | T1 |
| superChain.kt:1:1:1:33 | SuperChain1 | 1 | superChain.kt:1:28:1:29 | T2 |
| superChain.kt:1:1:1:33 | SuperChain1<T3,String> | 0 | superChain.kt:2:24:2:25 | T3 |

View File

@@ -42,6 +42,11 @@
| classes.kt:127:16:134:9 | new Object(...) { ... } | file://<external>/Object.class:0:0:0:0 | Object |
| classes.kt:129:17:131:17 | Local3 | file://<external>/Object.class:0:0:0:0 | Object |
| classes.kt:129:17:131:17 | Local3<Integer> | file://<external>/Object.class:0:0:0:0 | Object |
| generic_anonymous.kt:1:1:9:1 | Generic | file://<external>/Object.class:0:0:0:0 | Object |
| generic_anonymous.kt:1:1:9:1 | Generic<Integer> | file://<external>/Object.class:0:0:0:0 | Object |
| generic_anonymous.kt:1:1:9:1 | Generic<String> | file://<external>/Object.class:0:0:0:0 | Object |
| generic_anonymous.kt:1:1:9:1 | Generic<T> | file://<external>/Object.class:0:0:0:0 | Object |
| generic_anonymous.kt:3:19:5:3 | new Object(...) { ... } | file://<external>/Object.class:0:0:0:0 | Object |
| local_anonymous.kt:3:1:36:1 | Class1 | file://<external>/Object.class:0:0:0:0 | Object |
| local_anonymous.kt:5:16:7:9 | new Object(...) { ... } | file://<external>/Object.class:0:0:0:0 | Object |
| local_anonymous.kt:11:9:11:24 | | file://<external>/Object.class:0:0:0:0 | Object |