IR: keep local scope with counter maps across LDL invocations
Since LocalDeclarationsLowering is a BodyLoweringPass, local functions inside one declaration are handled independently of local functions in the other declaration. This can lead to name clashes, in case a local function with the same name and signature is declared in overloads in the same container, which results in a signature clash error in JVM IR. The issue became more common with the introduction of adapted function references, where psi2ir generates a local adapter-function with a predefined name, which can easily clash with another reference to the same target in an overload. This led to a compilation error when bootstrapping Kotlin with JVM IR, for example in GradleIRBuilder.kt where there are a lot of references to the same function.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.backend.common.ir
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
import org.jetbrains.kotlin.backend.common.lower.LocalDeclarationsLowering
|
||||
import org.jetbrains.kotlin.builtins.*
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
||||
@@ -34,6 +35,8 @@ abstract class Ir<out T : CommonBackendContext>(val context: T, val irModule: Ir
|
||||
|
||||
val defaultParameterDeclarationsCache = mutableMapOf<IrFunction, IrFunction>()
|
||||
|
||||
internal val localScopeWithCounterMap = LocalDeclarationsLowering.LocalScopeWithCounterMap()
|
||||
|
||||
// If irType is an inline class type, return the underlying type according to the
|
||||
// unfolding rules of the current backend. Otherwise, returns null.
|
||||
open fun unfoldInlineClassType(irType: IrType): IrType? = null
|
||||
|
||||
+24
-23
@@ -39,12 +39,15 @@ import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrTypeAbbreviationImpl
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType
|
||||
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.ir.util.constructedClass
|
||||
import org.jetbrains.kotlin.ir.util.file
|
||||
import org.jetbrains.kotlin.ir.util.parentAsClass
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
import java.util.*
|
||||
|
||||
interface LocalNameProvider {
|
||||
@@ -117,19 +120,22 @@ class LocalDeclarationsLowering(
|
||||
LocalDeclarationsTransformer(irElement, container, classesToLower).lowerLocalDeclarations()
|
||||
}
|
||||
|
||||
private class ScopeWithCounter(scope: Scope, irElement: IrElement) : ScopeWithIr(scope, irElement) {
|
||||
internal class ScopeWithCounter(scope: Scope, irElement: IrElement) : ScopeWithIr(scope, irElement) {
|
||||
// Continuous numbering across all declarations in the container.
|
||||
var counter: Int = 0
|
||||
val usedLocalFunctionNames: MutableSet<Name> = hashSetOf()
|
||||
}
|
||||
|
||||
internal class LocalScopeWithCounterMap {
|
||||
val scopeMap: MutableMap<IrSymbolOwner, ScopeWithCounter> = hashMapOf()
|
||||
}
|
||||
|
||||
private val scopeMap: MutableMap<IrSymbolOwner, ScopeWithCounter> = mutableMapOf()
|
||||
// Need to keep LocalFunctionContext.index
|
||||
private val IrSymbolOwner.scopeWithCounter: ScopeWithCounter
|
||||
get() = scopeMap.getOrPut(this) {
|
||||
get() = context.ir.localScopeWithCounterMap.scopeMap.getOrPut(this) {
|
||||
ScopeWithCounter(Scope(symbol), this)
|
||||
}
|
||||
|
||||
|
||||
private abstract class LocalContext {
|
||||
val capturedTypeParameterToTypeParameter: MutableMap<IrTypeParameter, IrTypeParameter> = mutableMapOf()
|
||||
|
||||
@@ -229,7 +235,6 @@ class LocalDeclarationsLowering(
|
||||
val localFunctions: MutableMap<IrFunction, LocalFunctionContext> = LinkedHashMap()
|
||||
val localClasses: MutableMap<IrClass, LocalClassContext> = LinkedHashMap()
|
||||
val localClassConstructors: MutableMap<IrConstructor, LocalClassConstructorContext> = LinkedHashMap()
|
||||
val usedLocalFunctionNames: MutableSet<Name> = mutableSetOf()
|
||||
|
||||
val transformedDeclarations = mutableMapOf<IrSymbolOwner, IrDeclaration>()
|
||||
|
||||
@@ -832,19 +837,17 @@ class LocalDeclarationsLowering(
|
||||
}
|
||||
|
||||
private fun collectLocalDeclarations() {
|
||||
val enclosingFileScope = container.file.scopeWithCounter
|
||||
|
||||
val enclosingClassScope = run {
|
||||
val enclosingFile = container.file
|
||||
val enclosingClass = run {
|
||||
var currentParent = container as? IrClass ?: container.parent
|
||||
while (currentParent is IrDeclaration && currentParent !is IrClass) {
|
||||
currentParent = currentParent.parent
|
||||
}
|
||||
|
||||
currentParent as? IrClass
|
||||
}?.scopeWithCounter
|
||||
}
|
||||
|
||||
irElement.acceptVoid(object : IrElementVisitorVoidWithContext() {
|
||||
|
||||
override fun visitElement(element: IrElement) {
|
||||
element.acceptChildrenVoid(this)
|
||||
}
|
||||
@@ -866,18 +869,16 @@ class LocalDeclarationsLowering(
|
||||
super.visitSimpleFunction(declaration)
|
||||
|
||||
if (declaration.visibility == Visibilities.LOCAL) {
|
||||
val scopeWithIr =
|
||||
(currentClass ?: enclosingClassScope ?: enclosingFileScope /*file is required for K/N cause file declarations are not split by classes*/
|
||||
?: error("No scope for ${declaration.dump()}"))
|
||||
val enclosingScope =
|
||||
// File is required for K/N because file declarations are not split by classes.
|
||||
currentClass ?: enclosingClass?.scopeWithCounter ?: enclosingFile.scopeWithCounter
|
||||
val index =
|
||||
if (enclosingScope is ScopeWithCounter &&
|
||||
(declaration.name.isSpecial || declaration.name in enclosingScope.usedLocalFunctionNames)
|
||||
) enclosingScope.counter++ else -1
|
||||
localFunctions[declaration] =
|
||||
LocalFunctionContext(
|
||||
declaration,
|
||||
if (declaration.name.isSpecial || declaration.name in usedLocalFunctionNames)
|
||||
(scopeWithIr as ScopeWithCounter).counter++
|
||||
else -1,
|
||||
scopeWithIr.irElement as IrDeclarationContainer
|
||||
)
|
||||
usedLocalFunctionNames.add(declaration.name)
|
||||
LocalFunctionContext(declaration, index, enclosingScope.irElement as IrDeclarationContainer)
|
||||
(enclosingScope as? ScopeWithCounter)?.usedLocalFunctionNames?.add(declaration.name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -909,4 +910,4 @@ class LocalDeclarationsLowering(
|
||||
}
|
||||
|
||||
// Local inner classes capture anything through outer
|
||||
internal fun IrClass.isLocalNotInner(): Boolean = visibility == Visibilities.LOCAL && !isInner
|
||||
internal fun IrClass.isLocalNotInner(): Boolean = visibility == Visibilities.LOCAL && !isInner
|
||||
|
||||
Reference in New Issue
Block a user