[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
This commit is contained in:
Dmitriy Novozhilov
2023-09-12 15:15:50 +03:00
committed by Space Team
parent e7b95d1288
commit 3778a69bf6
3 changed files with 8 additions and 10 deletions
@@ -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)
@@ -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
@@ -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")
}