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
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.name.Name
// TODO: do not serialize descriptors of non-exported declarations.
object KonanBinaryInterface {
private val mangler = object : AbstractKonanIrMangler(true) {}
private val mangler = object : AbstractKonanIrMangler(withReturnType = true, allowOutOfScopeTypeParameters = true) {}
private val exportChecker = mangler.getExportChecker(compatibleMode = true)
@@ -15,10 +15,14 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.hasAnnotation
abstract class AbstractKonanIrMangler(private val withReturnType: Boolean) : IrBasedKotlinManglerImpl() {
abstract class AbstractKonanIrMangler(
private val withReturnType: Boolean,
private val allowOutOfScopeTypeParameters: Boolean = false
) : IrBasedKotlinManglerImpl() {
override fun getExportChecker(compatibleMode: Boolean): IrExportCheckerVisitor = KonanIrExportChecker(compatibleMode)
override fun getMangleComputer(mode: MangleMode, compatibleMode: Boolean): IrMangleComputer = KonanIrManglerComputer(StringBuilder(256), mode, compatibleMode, withReturnType)
override fun getMangleComputer(mode: MangleMode, compatibleMode: Boolean): IrMangleComputer =
KonanIrManglerComputer(StringBuilder(256), mode, compatibleMode, withReturnType, allowOutOfScopeTypeParameters)
override fun IrDeclaration.isPlatformSpecificExport(): Boolean {
if (this is IrSimpleFunction) if (isFakeOverride) return false
@@ -51,8 +55,15 @@ abstract class AbstractKonanIrMangler(private val withReturnType: Boolean) : IrB
override fun IrDeclaration.isPlatformSpecificExported(): Boolean = isPlatformSpecificExport()
}
private class KonanIrManglerComputer(builder: StringBuilder, mode: MangleMode, compatibleMode: Boolean, private val withReturnType: Boolean) : IrMangleComputer(builder, mode, compatibleMode) {
override fun copy(newMode: MangleMode): IrMangleComputer = KonanIrManglerComputer(builder, newMode, compatibleMode, withReturnType)
private class KonanIrManglerComputer(
builder: StringBuilder,
mode: MangleMode,
compatibleMode: Boolean,
private val withReturnType: Boolean,
allowOutOfScopeTypeParameters: Boolean,
) : IrMangleComputer(builder, mode, compatibleMode, allowOutOfScopeTypeParameters) {
override fun copy(newMode: MangleMode): IrMangleComputer =
KonanIrManglerComputer(builder, newMode, compatibleMode, withReturnType, allowOutOfScopeTypeParameters)
override fun addReturnType(): Boolean = withReturnType
@@ -166,4 +177,4 @@ abstract class AbstractKonanDescriptorMangler : DescriptorBasedKotlinManglerImpl
}
}
object KonanManglerDesc : AbstractKonanDescriptorMangler()
object KonanManglerDesc : AbstractKonanDescriptorMangler()