11package com.github.codeql.utils
22
33import com.github.codeql.KotlinUsesExtractor
4+ import com.github.codeql.getJavaEquivalentClassId
45import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
56import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor
67import org.jetbrains.kotlin.descriptors.ClassKind
@@ -19,6 +20,7 @@ import org.jetbrains.kotlin.ir.types.*
1920import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
2021import org.jetbrains.kotlin.ir.types.impl.IrStarProjectionImpl
2122import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
23+ import org.jetbrains.kotlin.ir.util.classId
2224import org.jetbrains.kotlin.ir.util.constructedClassType
2325import org.jetbrains.kotlin.ir.util.constructors
2426import org.jetbrains.kotlin.ir.util.parentAsClass
@@ -195,13 +197,25 @@ fun IrTypeArgument.withQuestionMark(b: Boolean): IrTypeArgument =
195197
196198typealias TypeSubstitution = (IrType , KotlinUsesExtractor .TypeContext , IrPluginContext ) -> IrType
197199
200+ fun matchingTypeParameters (l : IrTypeParameter ? , r : IrTypeParameter ): Boolean {
201+ if (l == = r)
202+ return true
203+ if (l == null )
204+ return false
205+ // Special case: match List's E and MutableList's E, for example, because in the JVM lowering they will map to the same thing.
206+ val lParent = l.parent as ? IrClass ? : return false
207+ val rParent = r.parent as ? IrClass ? : return false
208+ val lJavaId = getJavaEquivalentClassId(lParent) ? : lParent.classId
209+ return (getJavaEquivalentClassId(rParent) ? : rParent.classId) == lJavaId && l.name == r.name
210+ }
211+
198212// Returns true if type is C<T1, T2, ...> where C is declared `class C<T1, T2, ...> { ... }`
199213fun isUnspecialised (paramsContainer : IrTypeParametersContainer , args : List <IrTypeArgument >): Boolean {
200214 val unspecialisedHere = paramsContainer.typeParameters.zip(args).all { paramAndArg ->
201215 (paramAndArg.second as ? IrTypeProjection )?.let {
202216 // Type arg refers to the class' own type parameter?
203217 it.variance == Variance .INVARIANT &&
204- it.type.classifierOrNull?.owner == = paramAndArg.first
218+ matchingTypeParameters( it.type.classifierOrNull?.owner as ? IrTypeParameter , paramAndArg.first)
205219 } ? : false
206220 }
207221 val remainingArgs = args.drop(paramsContainer.typeParameters.size)
0 commit comments