From 3778a69bf6b49925f2e2e139b176d27899640cc6 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 12 Sep 2023 15:15:50 +0300 Subject: [PATCH] [FIR2IR] Replace `getIrFieldSymbol` with `getOrCreateIrField` in Fir2IrDeclarationStorage Effectively, `getIrFieldSymbol` always created `IrField` if it was needed and returned its symbol. So to avoid potentially unsafe access of `IrFieldSymbol.owner` it's more convenient to return directly `IrField` ^KT-60924 --- .../jetbrains/kotlin/fir/backend/ConversionUtils.kt | 2 +- .../kotlin/fir/backend/Fir2IrDeclarationStorage.kt | 12 ++++++------ .../jetbrains/kotlin/fir/backend/FirIrProvider.kt | 4 +--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt index 2d3ef9b301e..94e42601ae6 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt @@ -277,7 +277,7 @@ fun FirCallableSymbol<*>.toSymbolForCall( is FirFunctionSymbol<*> -> declarationStorage.getIrFunctionSymbol(this, fakeOverrideOwnerLookupTag) is FirPropertySymbol -> declarationStorage.getIrPropertySymbol(this, fakeOverrideOwnerLookupTag) - is FirFieldSymbol -> declarationStorage.getIrFieldSymbol(this, fakeOverrideOwnerLookupTag) + is FirFieldSymbol -> declarationStorage.getOrCreateIrField(this, fakeOverrideOwnerLookupTag).symbol is FirBackingFieldSymbol -> declarationStorage.getIrBackingFieldSymbol(this) is FirDelegateFieldSymbol -> declarationStorage.getIrDelegateFieldSymbol(this) is FirVariableSymbol<*> -> declarationStorage.getIrValueSymbol(this) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index df429dc6d1d..5ee4e3c4542 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -641,27 +641,27 @@ class Fir2IrDeclarationStorage( // ------------------------------------ fields ------------------------------------ - fun getIrFieldSymbol( + fun getOrCreateIrField( firFieldSymbol: FirFieldSymbol, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null - ): IrFieldSymbol { + ): IrField { val fir = firFieldSymbol.fir val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(fir, fakeOverrideOwnerLookupTag) if (staticFakeOverrideKey == null) { - fieldCache[fir]?.let { return it.symbol } + fieldCache[fir]?.let { return it } } else { generateLazyFakeOverrides(fir.name, fakeOverrideOwnerLookupTag) // Lazy static fake override should always exist - return fieldStaticOverrideCache[staticFakeOverrideKey]!!.symbol + return fieldStaticOverrideCache[staticFakeOverrideKey]!! } // In case of type parameters from the parent as the field's return type, find the parent ahead to cache type parameters. val irParent = findIrParent(fir, fakeOverrideOwnerLookupTag) val unwrapped = fir.unwrapFakeOverrides() if (unwrapped !== fir) { - return getIrFieldSymbol(unwrapped.symbol) + return getOrCreateIrField(unwrapped.symbol) } - return createAndCacheIrField(fir, irParent).symbol + return createAndCacheIrField(fir, irParent) } // TODO: there is a mess with methods for fields diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirIrProvider.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirIrProvider.kt index aa6b4d9f24f..b193998e3e3 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirIrProvider.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirIrProvider.kt @@ -183,9 +183,7 @@ class FirIrProvider(val components: Fir2IrComponents) : IrProvider { } SymbolKind.FIELD_SYMBOL -> { val firField = firDeclaration as FirField - // TODO: effectively call getIrClassSymbol should be replaced with getOrCreateField (KT-61348) - @OptIn(IrSymbolInternals::class) - declarationStorage.getIrFieldSymbol(firField.symbol).owner + declarationStorage.getOrCreateIrField(firField.symbol) } else -> error("Don't know how to deal with this symbol kind: $kind") }