[FIR2IR] Mark ownerIfBound function as @UnsafeDuringIrConstructionAPI

Usages of this function may lead to potentially error-prone behavior,
  so it should be used carefully
There is a probability that this function won't be needed after
  KT-62856 will be fixed
This commit is contained in:
Dmitriy Novozhilov
2023-11-07 11:46:54 +02:00
committed by Space Team
parent 3ae2a8387e
commit 5c7b586dbb
2 changed files with 23 additions and 8 deletions
@@ -288,6 +288,7 @@ class Fir2IrDeclarationStorage(
isLocal: Boolean = false, isLocal: Boolean = false,
fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null
): IrSimpleFunction { ): IrSimpleFunction {
@OptIn(UnsafeDuringIrConstructionAPI::class)
getCachedIrFunctionSymbol(function, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it } getCachedIrFunctionSymbol(function, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it }
/* /*
* Declaration storage doesn't know how to create lazy fake-overrides, it is responsibility of * Declaration storage doesn't know how to create lazy fake-overrides, it is responsibility of
@@ -298,6 +299,7 @@ class Fir2IrDeclarationStorage(
*/ */
if (fakeOverrideOwnerLookupTag != function.containingClassLookupTag()) { if (fakeOverrideOwnerLookupTag != function.containingClassLookupTag()) {
generateLazyFakeOverrides(function.nameOrSpecialName, fakeOverrideOwnerLookupTag) generateLazyFakeOverrides(function.nameOrSpecialName, fakeOverrideOwnerLookupTag)
@OptIn(UnsafeDuringIrConstructionAPI::class)
getCachedIrFunctionSymbol(function, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it } getCachedIrFunctionSymbol(function, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it }
} }
return createAndCacheIrFunction(function, irParent(), predefinedOrigin, isLocal, fakeOverrideOwnerLookupTag) return createAndCacheIrFunction(function, irParent(), predefinedOrigin, isLocal, fakeOverrideOwnerLookupTag)
@@ -392,6 +394,7 @@ class Fir2IrDeclarationStorage(
predefinedOrigin: IrDeclarationOrigin? = null, predefinedOrigin: IrDeclarationOrigin? = null,
isLocal: Boolean = false, isLocal: Boolean = false,
): IrConstructor { ): IrConstructor {
@OptIn(UnsafeDuringIrConstructionAPI::class)
getCachedIrConstructorSymbol(constructor)?.ownerIfBound()?.let { return it } getCachedIrConstructorSymbol(constructor)?.ownerIfBound()?.let { return it }
return createAndCacheIrConstructor(constructor, irParent, predefinedOrigin, isLocal) return createAndCacheIrConstructor(constructor, irParent, predefinedOrigin, isLocal)
} }
@@ -476,10 +479,12 @@ class Fir2IrDeclarationStorage(
): IrProperty { ): IrProperty {
@Suppress("NAME_SHADOWING") @Suppress("NAME_SHADOWING")
val property = prepareProperty(property) val property = prepareProperty(property)
@OptIn(UnsafeDuringIrConstructionAPI::class)
getCachedIrPropertySymbol(property, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it } getCachedIrPropertySymbol(property, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it }
// See comment in [getOrCreateIrFunction] // See comment in [getOrCreateIrFunction]
if (fakeOverrideOwnerLookupTag != property.containingClassLookupTag()) { if (fakeOverrideOwnerLookupTag != property.containingClassLookupTag()) {
generateLazyFakeOverrides(property.name, fakeOverrideOwnerLookupTag) generateLazyFakeOverrides(property.name, fakeOverrideOwnerLookupTag)
@OptIn(UnsafeDuringIrConstructionAPI::class)
getCachedIrPropertySymbol(property, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it } getCachedIrPropertySymbol(property, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it }
} }
return createAndCacheIrProperty(property, irParent(), predefinedOrigin, isLocal, fakeOverrideOwnerLookupTag) return createAndCacheIrProperty(property, irParent(), predefinedOrigin, isLocal, fakeOverrideOwnerLookupTag)
@@ -626,6 +631,7 @@ class Fir2IrDeclarationStorage(
val fir = firFieldSymbol.fir val fir = firFieldSymbol.fir
val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(fir, fakeOverrideOwnerLookupTag) val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(fir, fakeOverrideOwnerLookupTag)
if (staticFakeOverrideKey == null) { if (staticFakeOverrideKey == null) {
@OptIn(UnsafeDuringIrConstructionAPI::class)
fieldCache[fir]?.ownerIfBound()?.let { return it } fieldCache[fir]?.ownerIfBound()?.let { return it }
} else { } else {
generateLazyFakeOverrides(fir.name, fakeOverrideOwnerLookupTag) generateLazyFakeOverrides(fir.name, fakeOverrideOwnerLookupTag)
@@ -646,6 +652,7 @@ class Fir2IrDeclarationStorage(
// TODO: there is a mess with methods for fields // TODO: there is a mess with methods for fields
// we have three (!) different functions to getOrCreate field in different circumstances // we have three (!) different functions to getOrCreate field in different circumstances
fun getOrCreateIrField(field: FirField, irParent: IrDeclarationParent?): IrField { fun getOrCreateIrField(field: FirField, irParent: IrDeclarationParent?): IrField {
@OptIn(UnsafeDuringIrConstructionAPI::class)
getCachedIrFieldSymbol(field, irParent)?.ownerIfBound()?.let { return it } getCachedIrFieldSymbol(field, irParent)?.ownerIfBound()?.let { return it }
return createAndCacheIrField(field, irParent) return createAndCacheIrField(field, irParent)
} }
@@ -674,6 +681,7 @@ class Fir2IrDeclarationStorage(
if (fir.isLocal) { if (fir.isLocal) {
return localStorage.getDelegatedProperty(fir)?.delegate?.symbol ?: getIrVariableSymbol(fir) return localStorage.getDelegatedProperty(fir)?.delegate?.symbol ?: getIrVariableSymbol(fir)
} }
@OptIn(UnsafeDuringIrConstructionAPI::class)
propertyCache[fir]?.ownerIfBound()?.let { return it.backingField!!.symbol } propertyCache[fir]?.ownerIfBound()?.let { return it.backingField!!.symbol }
val irParent = findIrParent(fir, fakeOverrideOwnerLookupTag = null) val irParent = findIrParent(fir, fakeOverrideOwnerLookupTag = null)
val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED
@@ -1198,7 +1206,12 @@ annotation class LeakedDeclarationCaches
@RequiresOptIn @RequiresOptIn
annotation class GetOrCreateSensitiveAPI annotation class GetOrCreateSensitiveAPI
@OptIn(UnsafeDuringIrConstructionAPI::class) /**
* This function is introduced as preparation to publishing unbound symbols in fir2ir
* There is a probability that it won't be non needed in future, but for now it allows
* to easily track all places left when we need to extract owner from symbol
*/
@UnsafeDuringIrConstructionAPI
internal fun <D : IrDeclaration> IrBindableSymbol<*, D>.ownerIfBound(): D? { internal fun <D : IrDeclaration> IrBindableSymbol<*, D>.ownerIfBound(): D? {
return runIf(isBound) { owner } return runIf(isBound) { owner }
} }
@@ -272,13 +272,15 @@ class FakeOverrideGenerator(
// Now we avoid this problem by signature caching, // Now we avoid this problem by signature caching,
// so both FIR overrides correspond to one IR fake override // so both FIR overrides correspond to one IR fake override
signatureComposer.composeSignature(fakeOverrideFirDeclaration) signatureComposer.composeSignature(fakeOverrideFirDeclaration)
}?.takeIf { it.ownerIfBound()?.parent == irClass } }?.takeIf {
?: createIrDeclaration( @OptIn(UnsafeDuringIrConstructionAPI::class)
fakeOverrideFirDeclaration, it.ownerIfBound()?.parent == irClass
irClass, } ?: createIrDeclaration(
IrDeclarationOrigin.FAKE_OVERRIDE, fakeOverrideFirDeclaration,
isLocal irClass,
).symbol as IS IrDeclarationOrigin.FAKE_OVERRIDE,
isLocal
).symbol as IS
@OptIn(UnsafeDuringIrConstructionAPI::class) @OptIn(UnsafeDuringIrConstructionAPI::class)
val owner = irSymbol.owner val owner = irSymbol.owner