Kotlin: Don't convert back and forth between ClassId and FqName

This showed up as a bug in Kotlin 2 mode:

We were starting with the Class Id "java/util/Map.Entry", which we then
converted to the FqName "java.util.Map.Entry", and then back to a
Class Id with ClassId.topLevel. This gave us a Class Id that
referenceClass wasn't able to resolve.

Now we just stick with the Class Id that we started with, and the class
can be resolved by Kotlin 2.
This commit is contained in:
Ian Lynagh
2023-10-17 16:58:41 +01:00
parent ed9502fd0b
commit ab891465a4
3 changed files with 10 additions and 1 deletions

View File

@@ -139,7 +139,7 @@ open class KotlinUsesExtractor(
}
fun getJavaEquivalentClass(c: IrClass) =
getJavaEquivalentClassId(c)?.let { getClassByFqName(pluginContext, it.asSingleFqName()) }?.owner
getJavaEquivalentClassId(c)?.let { getClassByClassId(pluginContext, it) }?.owner
/**
* Gets a KotlinFileExtractor based on this one, except it attributes locations to the file that declares the given class.

View File

@@ -2,6 +2,7 @@ package com.github.codeql.utils
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
@@ -9,6 +10,10 @@ fun getClassByFqName(pluginContext: IrPluginContext, fqName: FqName): IrClassSym
return pluginContext.referenceClass(fqName)
}
fun getClassByClassId(pluginContext: IrPluginContext, id: ClassId): IrClassSymbol? {
return getClassByFqName(pluginContext, id.asSingleFqName())
}
fun getFunctionsByFqName(pluginContext: IrPluginContext, pkgName: FqName, name: Name): Collection<IrSimpleFunctionSymbol> {
val fqName = pkgName.child(name)
return pluginContext.referenceFunctions(fqName)

View File

@@ -10,6 +10,10 @@ import org.jetbrains.kotlin.name.Name
fun getClassByFqName(pluginContext: IrPluginContext, fqName: FqName): IrClassSymbol? {
val id = ClassId.topLevel(fqName)
return getClassByClassId(pluginContext, id)
}
fun getClassByClassId(pluginContext: IrPluginContext, id: ClassId): IrClassSymbol? {
return pluginContext.referenceClass(id)
}