From e340286606b52bbec252863b75a062e90cb3dae0 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 21 Feb 2024 10:25:20 +0200 Subject: [PATCH] [FIR2IR] Part 3. Don't create type parameter symbols with signature ^KT-64990 --- .../fir/backend/Fir2IrClassifierStorage.kt | 21 ++++++------------- .../Fir2IrCallableDeclarationsGenerator.kt | 8 +++---- .../generators/Fir2IrClassifiersGenerator.kt | 6 +++--- .../kotlin/fir/lazy/Fir2IrLazyClass.kt | 2 +- .../kotlin/fir/lazy/Fir2IrLazyConstructor.kt | 2 +- .../kotlin/fir/lazy/Fir2IrLazyProperty.kt | 2 +- .../fir/lazy/Fir2IrLazySimpleFunction.kt | 2 +- .../kotlin/fir/lazy/Fir2IrLazyTypeAlias.kt | 2 +- .../fir/lazy/Fir2IrTypeParametersContainer.kt | 2 +- 9 files changed, 19 insertions(+), 28 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt index 39e4fb02dd2..669de38eafb 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt @@ -78,15 +78,15 @@ class Fir2IrClassifierStorage( // ------------------------------------ type parameters ------------------------------------ // Note: declareTypeParameters should be called before! - internal fun preCacheTypeParameters(owner: FirTypeParameterRefsOwner, irOwnerSymbol: IrSymbol) { + internal fun preCacheTypeParameters(owner: FirTypeParameterRefsOwner) { for ((index, typeParameter) in owner.typeParameters.withIndex()) { val original = typeParameter.symbol.fir getCachedIrTypeParameter(original) - ?: createAndCacheIrTypeParameter(original, index, irOwnerSymbol) + ?: createAndCacheIrTypeParameter(original, index) if (owner is FirProperty && owner.isVar) { val context = ConversionTypeOrigin.SETTER getCachedIrTypeParameter(original, context) - ?: createAndCacheIrTypeParameter(original, index, irOwnerSymbol, context) + ?: createAndCacheIrTypeParameter(original, index, context) } } } @@ -94,11 +94,10 @@ class Fir2IrClassifierStorage( internal fun getIrTypeParameter( typeParameter: FirTypeParameter, index: Int, - ownerSymbol: IrSymbol, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT ): IrTypeParameter { getCachedIrTypeParameter(typeParameter, typeOrigin)?.let { return it } - val irTypeParameter = createAndCacheIrTypeParameter(typeParameter, index, ownerSymbol, typeOrigin) + val irTypeParameter = createAndCacheIrTypeParameter(typeParameter, index, typeOrigin) classifiersGenerator.initializeTypeParameterBounds(typeParameter, irTypeParameter) return irTypeParameter } @@ -106,10 +105,9 @@ class Fir2IrClassifierStorage( private fun createAndCacheIrTypeParameter( typeParameter: FirTypeParameter, index: Int, - ownerSymbol: IrSymbol, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT, ): IrTypeParameter { - val symbol = createTypeParameterSymbol(ownerSymbol, index) + val symbol = IrTypeParameterSymbolImpl() val irTypeParameter = classifiersGenerator.createIrTypeParameterWithoutBounds(typeParameter, index, symbol) // Cache the type parameter BEFORE processing its bounds/supertypes, to properly handle recursive type bounds. if (typeOrigin.forSetter) { @@ -120,13 +118,6 @@ class Fir2IrClassifierStorage( return irTypeParameter } - private fun createTypeParameterSymbol(ownerSymbol: IrSymbol, index: Int): IrTypeParameterSymbol { - if (ownerSymbol !is IrClassifierSymbol) return IrTypeParameterSymbolImpl() - val signature = signatureComposer.composeTypeParameterSignature(index, ownerSymbol.signature) - ?: return IrTypeParameterSymbolImpl() - return symbolTable.referenceTypeParameter(signature) - } - internal fun getCachedIrTypeParameter( typeParameter: FirTypeParameter, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT @@ -165,7 +156,7 @@ class Fir2IrClassifierStorage( val isSetter = firTypeParameterOwner is FirPropertyAccessor && firTypeParameterOwner.isSetter val conversionTypeOrigin = if (isSetter) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT - return createAndCacheIrTypeParameter(firTypeParameter, index, IrTypeParameterSymbolImpl(), conversionTypeOrigin).also { + return createAndCacheIrTypeParameter(firTypeParameter, index, conversionTypeOrigin).also { classifiersGenerator.initializeTypeParameterBounds(firTypeParameter, it) }.symbol } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt index 1dd4a612855..e0a10a2de14 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt @@ -111,7 +111,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi if (isLambda) ((function as FirAnonymousFunction).typeRef as? FirResolvedTypeRef)?.type?.isSuspendOrKSuspendFunctionType(session) == true else function.isSuspend val created = function.convertWithOffsets { startOffset, endOffset -> - classifierStorage.preCacheTypeParameters(function, symbol) + classifierStorage.preCacheTypeParameters(function) irFactory.createSimpleFunction( startOffset = if (updatedOrigin == IrDeclarationOrigin.DELEGATED_MEMBER) SYNTHETIC_OFFSET else startOffset, endOffset = if (updatedOrigin == IrDeclarationOrigin.DELEGATED_MEMBER) SYNTHETIC_OFFSET else endOffset, @@ -186,7 +186,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi } val visibility = if (irParent.isAnonymousObject) Visibilities.Public else constructor.visibility return constructor.convertWithOffsets { startOffset, endOffset -> - classifierStorage.preCacheTypeParameters(constructor, symbol) + classifierStorage.preCacheTypeParameters(constructor) irFactory.createConstructor( startOffset = startOffset, endOffset = endOffset, @@ -245,7 +245,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi return lazyDeclarationsGenerator.createIrLazyProperty(property, irParent!!, symbols, origin) } return property.convertWithOffsets { startOffset, endOffset -> - classifierStorage.preCacheTypeParameters(property, symbols.propertySymbol) + classifierStorage.preCacheTypeParameters(property) irFactory.createProperty( startOffset = startOffset, endOffset = endOffset, @@ -275,7 +275,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi if (delegate != null || property.hasBackingField) { val backingField = if (delegate != null) { ((delegate as? FirQualifiedAccessExpression)?.calleeReference?.toResolvedBaseSymbol()?.fir as? FirTypeParameterRefsOwner)?.let { - classifierStorage.preCacheTypeParameters(it, symbol) + classifierStorage.preCacheTypeParameters(it) } createBackingField( this, diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrClassifiersGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrClassifiersGenerator.kt index 1340a5a185a..4b89bb7d708 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrClassifiersGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrClassifiersGenerator.kt @@ -121,7 +121,7 @@ class Fir2IrClassifiersGenerator(val components: Fir2IrComponents) : Fir2IrCompo // `irClass` is a source class and definitely is not a lazy class @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrClass.declareTypeParameters(klass: FirClass) { - classifierStorage.preCacheTypeParameters(klass, symbol) + classifierStorage.preCacheTypeParameters(klass) setTypeParameters(this, klass) if (klass is FirRegularClass) { val fieldsForContextReceiversOfCurrentClass = classifierStorage.getFieldsWithContextReceiversForClass(this, klass) @@ -285,7 +285,7 @@ class Fir2IrClassifiersGenerator(val components: Fir2IrComponents) : Fir2IrCompo parent: IrDeclarationParent, symbol: IrTypeAliasSymbol, ): IrTypeAlias = typeAlias.convertWithOffsets { startOffset, endOffset -> - classifierStorage.preCacheTypeParameters(typeAlias, symbol) + classifierStorage.preCacheTypeParameters(typeAlias) irFactory.createTypeAlias( startOffset = startOffset, endOffset = endOffset, @@ -389,7 +389,7 @@ class Fir2IrClassifiersGenerator(val components: Fir2IrComponents) : Fir2IrCompo ) { irOwner.typeParameters = owner.typeParameters.mapIndexedNotNull { index, typeParameter -> if (typeParameter !is FirTypeParameter) return@mapIndexedNotNull null - classifierStorage.getIrTypeParameter(typeParameter, index, irOwner.symbol, typeOrigin).apply { + classifierStorage.getIrTypeParameter(typeParameter, index, typeOrigin).apply { parent = irOwner if (superTypes.isEmpty()) { superTypes = typeParameter.bounds.map { it.toIrType(typeOrigin) } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt index 54cecae5572..3f608751922 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt @@ -44,7 +44,7 @@ class Fir2IrLazyClass( IrMaybeDeserializedClass, DeserializableClass, Fir2IrComponents by components { init { symbol.bind(this) - classifierStorage.preCacheTypeParameters(fir, symbol) + classifierStorage.preCacheTypeParameters(fir) } override var annotations: List by createLazyAnnotations() diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyConstructor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyConstructor.kt index 9c3e0d23f19..5cf16ab50b0 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyConstructor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyConstructor.kt @@ -41,7 +41,7 @@ class Fir2IrLazyConstructor( Fir2IrComponents by components { init { symbol.bind(this) - classifierStorage.preCacheTypeParameters(fir, symbol) + classifierStorage.preCacheTypeParameters(fir) } override var annotations: List by createLazyAnnotations() diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyProperty.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyProperty.kt index 6cafe1b04c4..d6eaf6b7559 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyProperty.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyProperty.kt @@ -47,7 +47,7 @@ class Fir2IrLazyProperty( init { symbol.bind(this) - classifierStorage.preCacheTypeParameters(fir, symbol) + classifierStorage.preCacheTypeParameters(fir) } override var annotations: List by createLazyAnnotations() diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazySimpleFunction.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazySimpleFunction.kt index a5acfe98063..ffd02a7377d 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazySimpleFunction.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazySimpleFunction.kt @@ -40,7 +40,7 @@ class Fir2IrLazySimpleFunction( ) : AbstractFir2IrLazyFunction(components, startOffset, endOffset, origin, symbol, parent, isFakeOverride) { init { symbol.bind(this) - classifierStorage.preCacheTypeParameters(fir, symbol) + classifierStorage.preCacheTypeParameters(fir) } override var annotations: List by createLazyAnnotations() diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyTypeAlias.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyTypeAlias.kt index 28bae62c592..cadc080a679 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyTypeAlias.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyTypeAlias.kt @@ -34,7 +34,7 @@ class Fir2IrLazyTypeAlias( ) : IrTypeAlias(), AbstractFir2IrLazyDeclaration, Fir2IrTypeParametersContainer, Fir2IrComponents by components { init { symbol.bind(this) - classifierStorage.preCacheTypeParameters(fir, symbol) + classifierStorage.preCacheTypeParameters(fir) } override var annotations: List by createLazyAnnotations() diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrTypeParametersContainer.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrTypeParametersContainer.kt index 821c4cfa0cd..c8328dae0b4 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrTypeParametersContainer.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrTypeParametersContainer.kt @@ -17,7 +17,7 @@ interface Fir2IrTypeParametersContainer : IrTypeParametersContainer, Fir2IrCompo fun prepareTypeParameters() { typeParameters = fir.typeParameters.mapIndexedNotNull { index, typeParameter -> if (typeParameter !is FirTypeParameter) return@mapIndexedNotNull null - classifierStorage.getIrTypeParameter(typeParameter, index, symbol).apply { + classifierStorage.getIrTypeParameter(typeParameter, index).apply { parent = this@Fir2IrTypeParametersContainer if (superTypes.isEmpty()) { superTypes = typeParameter.bounds.map { it.toIrType(typeConverter) }