An option to allow out-of-scope type parameters in IrManglerComputer

In the lowered IR there are often references to type parameters whose
containers are not in the current scope. This is incorrect semantically,
but it works in practice due to erasure, so when the mangler is used on
the lowered IR, we don't want to crash the compiler.
This commit is contained in:
Sergej Jaskiewicz
2023-03-03 15:00:40 +01:00
committed by Space Team
parent e3a4d6fa56
commit f40278c036
5 changed files with 51 additions and 11 deletions
@@ -19,10 +19,13 @@ import org.jetbrains.kotlin.types.model.*
* @param Session An additional context used for type computations.
* @property builder A string builder to write the mangled name into.
* @property mode The mangle mode.
* @property allowOutOfScopeTypeParameters Whether to throw an exception if the container of a referenced type parameter is not found
* in the current scope. This often happens in the lowered IR and should never happen in the IR emitted by the frontend.
*/
abstract class BaseKotlinMangleComputer<Declaration, Type, TypeParameter, ValueParameter, TypeParameterContainer, FunctionDeclaration, Session>(
protected val builder: StringBuilder,
protected val mode: MangleMode
protected val mode: MangleMode,
protected val allowOutOfScopeTypeParameters: Boolean = false,
) : KotlinMangleComputer<Declaration>
where Declaration : Any,
Type : KotlinTypeMarker,
@@ -120,7 +123,7 @@ abstract class BaseKotlinMangleComputer<Declaration, Type, TypeParameter, ValueP
protected fun StringBuilder.mangleTypeParameterReference(typeParameter: TypeParameter) {
val parent = getEffectiveParent(typeParameter)
val containerIndex = typeParameterContainers.indexOf(parent)
require(containerIndex >= 0) {
require(allowOutOfScopeTypeParameters || containerIndex >= 0) {
"No container found for type parameter '${getTypeParameterName(typeParameter)}' of '${renderDeclaration(parent)}'"
}
appendSignature(containerIndex)
@@ -22,7 +22,8 @@ import org.jetbrains.kotlin.ir.visitors.acceptVoid
open class IrMangleComputer(
builder: StringBuilder,
mode: MangleMode,
protected val compatibleMode: Boolean
protected val compatibleMode: Boolean,
allowOutOfScopeTypeParameters: Boolean = false,
) : BaseKotlinMangleComputer<
/*Declaration=*/IrDeclaration,
/*Type=*/IrType,
@@ -31,7 +32,7 @@ open class IrMangleComputer(
/*TypeParameterContainer=*/IrDeclaration,
/*FunctionDeclaration=*/IrFunction,
/*Session=*/Nothing?,
>(builder, mode) {
>(builder, mode, allowOutOfScopeTypeParameters) {
final override fun getTypeSystemContext(session: Nothing?) = object : IrTypeSystemContext {
override val irBuiltIns: IrBuiltIns