Kotlin: Fix isUnspecialised to handle generic classes inside generic methods

This commit is contained in:
Tamas Vajk
2022-09-09 11:13:48 +02:00
parent 3267d7c96e
commit b8b0fd8a74
8 changed files with 21 additions and 22 deletions

View File

@@ -1450,7 +1450,7 @@ open class KotlinFileExtractor(
extractNewExprForLocalFunction(ids, id, locId, enclosingCallable, enclosingStmt)
} else {
val methodId =
if (extractClassTypeArguments && drType is IrSimpleType && !isUnspecialised(drType)) {
if (extractClassTypeArguments && drType is IrSimpleType && !isUnspecialised(drType, logger)) {
val extractionMethod = if (isFunctionInvoke) {
// For `kotlin.FunctionX` and `kotlin.reflect.KFunctionX` interfaces, we're making sure that we

View File

@@ -220,7 +220,7 @@ open class KotlinUsesExtractor(
val extractedTypeArgs = when {
extractClass.symbol.isKFunction() && typeArgs != null && typeArgs.isNotEmpty() -> listOf(typeArgs.last())
extractClass.fqNameWhenAvailable == FqName("kotlin.jvm.functions.FunctionN") && typeArgs != null && typeArgs.isNotEmpty() -> listOf(typeArgs.last())
typeArgs != null && isUnspecialised(c, typeArgs) -> listOf()
typeArgs != null && isUnspecialised(c, typeArgs, logger) -> listOf()
else -> typeArgs
}
@@ -479,7 +479,7 @@ open class KotlinUsesExtractor(
fun useSimpleTypeClass(c: IrClass, args: List<IrTypeArgument>?, hasQuestionMark: Boolean): TypeResults {
if (c.isAnonymousObject) {
args?.let {
if (it.isNotEmpty() && !isUnspecialised(c, it)) {
if (it.isNotEmpty() && !isUnspecialised(c, it, logger)) {
logger.error("Unexpected specialised instance of generic anonymous class")
}
}

View File

@@ -1,6 +1,7 @@
package com.github.codeql.utils
import com.github.codeql.KotlinUsesExtractor
import com.github.codeql.Logger
import com.github.codeql.getJavaEquivalentClassId
import com.github.codeql.utils.versions.codeQlWithHasQuestionMark
import com.github.codeql.utils.versions.createImplicitParameterDeclarationWithWrappedDescriptor
@@ -25,6 +26,7 @@ import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
import org.jetbrains.kotlin.ir.util.classId
import org.jetbrains.kotlin.ir.util.constructedClassType
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.kotlinFqName
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
@@ -215,27 +217,34 @@ private fun matchingTypeParameters(l: IrTypeParameter?, r: IrTypeParameter): Boo
}
// Returns true if type is C<T1, T2, ...> where C is declared `class C<T1, T2, ...> { ... }`
fun isUnspecialised(paramsContainer: IrTypeParametersContainer, args: List<IrTypeArgument>): Boolean {
fun isUnspecialised(paramsContainer: IrTypeParametersContainer, args: List<IrTypeArgument>, logger: Logger): Boolean {
return isUnspecialised(paramsContainer, args, logger, paramsContainer)
}
private fun isUnspecialised(paramsContainer: IrTypeParametersContainer, args: List<IrTypeArgument>, logger: Logger, origParamsContainer: IrTypeParametersContainer): Boolean {
val unspecialisedHere = paramsContainer.typeParameters.zip(args).all { paramAndArg ->
(paramAndArg.second as? IrTypeProjection)?.let {
// Type arg refers to the class' own type parameter?
it.variance == Variance.INVARIANT &&
matchingTypeParameters(it.type.classifierOrNull?.owner as? IrTypeParameter, paramAndArg.first)
matchingTypeParameters(it.type.classifierOrNull?.owner as? IrTypeParameter, paramAndArg.first)
} ?: false
}
val remainingArgs = args.drop(paramsContainer.typeParameters.size)
val parentClass = paramsContainer.parents.firstIsInstanceOrNull<IrClass>()
val parentTypeContainer = paramsContainer.parents.firstIsInstanceOrNull<IrTypeParametersContainer>()
val parentUnspecialised = when {
remainingArgs.isEmpty() -> true
parentClass == null -> false
else -> isUnspecialised(parentClass, remainingArgs)
parentTypeContainer == null -> {
logger.error("Found more type arguments than parameters: ${origParamsContainer.kotlinFqName.asString()}")
false
}
else -> isUnspecialised(parentTypeContainer, remainingArgs, logger, origParamsContainer)
}
return unspecialisedHere && parentUnspecialised
}
// 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)
fun isUnspecialised(type: IrSimpleType, logger: Logger) = (type.classifier.owner as? IrClass)?.let {
isUnspecialised(it, type.arguments, logger)
} ?: false

View File

@@ -1 +0,0 @@
| Type Cl1 uses out-of-scope type variable U2. Note the Java extractor is known to sometimes do this; the Kotlin extractor should not. |

View File

@@ -533,8 +533,8 @@ classes.kt:
# 142| 5: [BlockStmt] { ... }
# 143| 0: [LocalVariableDeclStmt] var ...;
# 143| 1: [LocalVariableDeclExpr] x
# 143| 0: [ClassInstanceExpr] new Cl1<U3>(...)
# 143| -3: [TypeAccess] Cl1<U3>
# 143| 0: [ClassInstanceExpr] new Cl1(...)
# 143| -3: [TypeAccess] Cl1
# 143| 0: [TypeAccess] U3
# 150| 26: [Class,GenericType,ParameterizedType] Cl00
#-----| -2: (Generic Parameters)

View File

@@ -1,5 +0,0 @@
| file://:0:0:0:0 | Unexpected specialised instance of generic anonymous class | CodeQL Kotlin extractor | 5 | | Unexpected specialised instance of generic anonymous class | 2022-09-09 11:08:33 Unexpected specialised instance of generic anonymous class ...while extracting a expression (<no name>) at generic_anonymous.kt:26:13:26:37\n ...while extracting a type operator call (<no name>) at generic_anonymous.kt:26:13:26:37\n ...while extracting a expression (<no name>) at generic_anonymous.kt:26:13:26:37\n ...while extracting a statement (<no name>) at generic_anonymous.kt:26:13:26:37\n ...while extracting a block body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a function (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a generated class (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a statement (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a block body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a function (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a declaration (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a class source (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a declaration (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a file (generic_anonymous.kt) at generic_anonymous.kt:1:1:34:0\n |
| file://:0:0:0:0 | Unexpected specialised instance of generic anonymous class | CodeQL Kotlin extractor | 5 | | Unexpected specialised instance of generic anonymous class | 2022-09-09 11:08:33 Unexpected specialised instance of generic anonymous class ...while extracting a expression (<no name>) at generic_anonymous.kt:27:13:27:37\n ...while extracting a type operator call (<no name>) at generic_anonymous.kt:27:13:27:37\n ...while extracting a expression (<no name>) at generic_anonymous.kt:27:13:27:37\n ...while extracting a statement (<no name>) at generic_anonymous.kt:27:13:27:37\n ...while extracting a block body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a function (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a generated class (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a statement (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a block body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a function (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a declaration (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a class source (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a declaration (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a file (generic_anonymous.kt) at generic_anonymous.kt:1:1:34:0\n |
| file://:0:0:0:0 | Unexpected specialised instance of generic anonymous class | CodeQL Kotlin extractor | 5 | | Unexpected specialised instance of generic anonymous class | 2022-09-09 11:08:33 Unexpected specialised instance of generic anonymous class ...while extracting a expression (<no name>) at generic_anonymous.kt:28:13:28:41\n ...while extracting a type operator call (<no name>) at generic_anonymous.kt:28:13:28:41\n ...while extracting a expression (<no name>) at generic_anonymous.kt:28:13:28:41\n ...while extracting a statement (<no name>) at generic_anonymous.kt:28:13:28:41\n ...while extracting a block body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a function (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a generated class (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a statement (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a block body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a function (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a declaration (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a class source (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a declaration (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a file (generic_anonymous.kt) at generic_anonymous.kt:1:1:34:0\n |
| file://:0:0:0:0 | Unexpected specialised instance of generic anonymous class | CodeQL Kotlin extractor | 5 | | Unexpected specialised instance of generic anonymous class | 2022-09-09 11:08:33 Unexpected specialised instance of generic anonymous class ...while extracting a expression (<no name>) at generic_anonymous.kt:29:13:29:29\n ...while extracting a type operator call (<no name>) at generic_anonymous.kt:29:13:29:29\n ...while extracting a expression (<no name>) at generic_anonymous.kt:29:13:29:29\n ...while extracting a statement (<no name>) at generic_anonymous.kt:29:13:29:29\n ...while extracting a block body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a function (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a generated class (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a statement (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a block body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a function (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a declaration (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a class source (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a declaration (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a file (generic_anonymous.kt) at generic_anonymous.kt:1:1:34:0\n |
| file://:0:0:0:0 | Unexpected specialised instance of generic anonymous class | CodeQL Kotlin extractor | 5 | | Unexpected specialised instance of generic anonymous class | 2022-09-09 11:08:33 Unexpected specialised instance of generic anonymous class ...while extracting a expression (<no name>) at generic_anonymous.kt:30:13:30:33\n ...while extracting a type operator call (<no name>) at generic_anonymous.kt:30:13:30:33\n ...while extracting a expression (<no name>) at generic_anonymous.kt:30:13:30:33\n ...while extracting a statement (<no name>) at generic_anonymous.kt:30:13:30:33\n ...while extracting a block body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a body (<no name>) at generic_anonymous.kt:25:26:31:9\n ...while extracting a function (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a generated class (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a statement (func2) at generic_anonymous.kt:25:9:31:9\n ...while extracting a block body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a body (<no name>) at generic_anonymous.kt:24:22:32:5\n ...while extracting a function (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a declaration (func1) at generic_anonymous.kt:24:5:32:5\n ...while extracting a class source (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a declaration (Outer) at generic_anonymous.kt:15:1:33:1\n ...while extracting a file (generic_anonymous.kt) at generic_anonymous.kt:1:1:34:0\n |

View File

@@ -8,8 +8,6 @@
| file://<external>/C1$<no name provided>$Local3.class:0:0:0:0 | Local3<Integer> | 0 | file://<external>/Integer.class:0:0:0:0 | Integer |
| file://<external>/C1$Local1.class:0:0:0:0 | Local1<Integer> | 0 | file://<external>/Integer.class:0:0:0:0 | Integer |
| file://<external>/C1$Local2.class:0:0:0:0 | Local2<Integer> | 0 | file://<external>/Integer.class:0:0:0:0 | Integer |
| file://<external>/Cl0$Cl1.class:0:0:0:0 | Cl1<U3> | 0 | classes.kt:141:23:141:24 | U3 |
| file://<external>/Cl0.class:0:0:0:0 | Cl0<U0> | 0 | classes.kt:140:14:140:15 | U2 |
| file://<external>/Generic.class:0:0:0:0 | Generic<Integer> | 0 | file://<external>/Integer.class:0:0:0:0 | Integer |
| file://<external>/Generic.class:0:0:0:0 | Generic<String> | 0 | file://<external>/String.class:0:0:0:0 | String |
| file://<external>/SuperChain1.class:0:0:0:0 | SuperChain1<T3,String> | 0 | superChain.kt:2:24:2:25 | T3 |

View File

@@ -47,8 +47,6 @@
| file://<external>/C1$<no name provided>$Local3.class:0:0:0:0 | Local3<Integer> | file://<external>/Object.class:0:0:0:0 | Object |
| file://<external>/C1$Local1.class:0:0:0:0 | Local1<Integer> | file://<external>/Object.class:0:0:0:0 | Object |
| file://<external>/C1$Local2.class:0:0:0:0 | Local2<Integer> | file://<external>/Object.class:0:0:0:0 | Object |
| file://<external>/Cl0$Cl1.class:0:0:0:0 | Cl1<U3> | file://<external>/Object.class:0:0:0:0 | Object |
| file://<external>/Cl0.class:0:0:0:0 | Cl0<U0> | file://<external>/Object.class:0:0:0:0 | Object |
| file://<external>/Generic.class:0:0:0:0 | Generic<Integer> | file://<external>/Object.class:0:0:0:0 | Object |
| file://<external>/Generic.class:0:0:0:0 | Generic<String> | file://<external>/Object.class:0:0:0:0 | Object |
| file://<external>/SuperChain1.class:0:0:0:0 | SuperChain1<T3,String> | file://<external>/Object.class:0:0:0:0 | Object |