From 14992541bbe30af2c27ac2c43e18ae1bbc1681fe Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 12 Jul 2024 10:43:27 +0300 Subject: [PATCH] [FIR2IR] Add static fields to the list of declarations of lazy IR java class ^KT-69735 Fixed --- .../kotlin/fir/backend/Fir2IrFakeOverrideStrategy.kt | 6 ++++++ .../src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt | 8 +++++++- .../kotlin/ir/overrides/FakeOverrideBuilderStrategy.kt | 8 ++++---- .../irText/firProblems/FieldsFromJavaClass.__J.fir.ir.txt | 4 ++++ .../firProblems/FieldsFromJavaClass.__J1.fir.ir.txt | 4 ++++ .../irText/firProblems/FieldsFromJavaClass.__X.fir.ir.txt | 2 ++ .../firProblems/FieldsFromJavaClass.__X1.fir.ir.txt | 2 ++ 7 files changed, 29 insertions(+), 5 deletions(-) diff --git a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/backend/Fir2IrFakeOverrideStrategy.kt b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/backend/Fir2IrFakeOverrideStrategy.kt index 2be7c5cde5c15..b4d5a17a5638c 100644 --- a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/backend/Fir2IrFakeOverrideStrategy.kt +++ b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/backend/Fir2IrFakeOverrideStrategy.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.FirClass import org.jetbrains.kotlin.fir.dispatchReceiverClassLookupTagOrNull import org.jetbrains.kotlin.fir.isDelegated +import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyPropertyForPureField import org.jetbrains.kotlin.fir.resolve.ScopeSession import org.jetbrains.kotlin.fir.scopes.processAllFunctions import org.jetbrains.kotlin.fir.scopes.processAllProperties @@ -49,6 +50,11 @@ class Fir2IrFakeOverrideStrategy( ) : FakeOverrideBuilderStrategy.BindToPrivateSymbols(friendModules, delegatedMemberGenerationStrategy) { private val fieldOnlyProperties: MutableList = mutableListOf() + override fun fakeOverrideMember(superType: IrType, member: IrOverridableMember, clazz: IrClass): IrOverridableMember? { + if (member is Fir2IrLazyPropertyForPureField && member.backingField?.isStatic == true) return null + return super.fakeOverrideMember(superType, member, clazz) + } + override fun linkPropertyFakeOverride(property: IrPropertyWithLateBinding, manglerCompatibleMode: Boolean) { super.linkPropertyFakeOverride(property, manglerCompatibleMode) 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 08be042528ff6..1e96d2be4839f 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 @@ -199,7 +199,7 @@ class Fir2IrLazyClass( when { !shouldBuildStub(symbol.fir) -> {} symbol is FirFieldSymbol -> { - if (!symbol.isStatic) { + if (shouldBuildIrField(symbol)) { // Lazy declarations are created together with their symbol, so it's safe to take the owner here @OptIn(UnsafeDuringIrConstructionAPI::class) result += declarationStorage.getIrSymbolForField( @@ -250,6 +250,12 @@ class Fir2IrLazyClass( } } + private fun shouldBuildIrField(fieldSymbol: FirFieldSymbol): Boolean { + if (!fieldSymbol.isStatic) return true + // we need to create IR for static fields only if they are not fake-overrides + return fir.isJava && !fieldSymbol.fir.isFakeOverride(fir) + } + override var metadata: MetadataSource? get() = null set(_) = error("We should never need to store metadata of external declarations.") diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/overrides/FakeOverrideBuilderStrategy.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/overrides/FakeOverrideBuilderStrategy.kt index 21b12c83c95cc..72bc072c59600 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/overrides/FakeOverrideBuilderStrategy.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/overrides/FakeOverrideBuilderStrategy.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.util.hasAnnotation import org.jetbrains.kotlin.ir.util.render import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.Variance +import org.jetbrains.kotlin.utils.addToStdlib.runIf /** * This class is a customization point for IrFakeOverrideBuilder. @@ -64,11 +65,10 @@ abstract class FakeOverrideBuilderStrategy( * Creates a fake override for [member] from [superType] to be added to the class [clazz] or returns null, * if no fake override should be created for this member */ - fun fakeOverrideMember(superType: IrType, member: IrOverridableMember, clazz: IrClass): IrOverridableMember? { - return if (isVisibleForOverrideInClass(member, clazz)) + open fun fakeOverrideMember(superType: IrType, member: IrOverridableMember, clazz: IrClass): IrOverridableMember? { + return runIf(isVisibleForOverrideInClass(member, clazz)) { buildFakeOverrideMember(superType, member, clazz, unimplementedOverridesStrategy) - else - null + } } /** diff --git a/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__J.fir.ir.txt b/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__J.fir.ir.txt index e9df3a1d9ea3b..ff86e6baed0c2 100644 --- a/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__J.fir.ir.txt +++ b/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__J.fir.ir.txt @@ -4,6 +4,10 @@ CLASS IR_EXTERNAL_JAVA_DECLARATION_STUB CLASS name:J modality:OPEN visibility:pu FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:f type:kotlin.Int visibility:public PROPERTY IR_EXTERNAL_JAVA_DECLARATION_STUB name:f2 visibility:public modality:FINAL [var] FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:f2 type:kotlin.Int visibility:public + PROPERTY IR_EXTERNAL_JAVA_DECLARATION_STUB name:s visibility:public modality:FINAL [var] + FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:s type:kotlin.Int visibility:public [static] + PROPERTY IR_EXTERNAL_JAVA_DECLARATION_STUB name:s2 visibility:public modality:FINAL [var] + FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:s2 type:kotlin.Int visibility:public [static] CONSTRUCTOR IR_EXTERNAL_JAVA_DECLARATION_STUB visibility:public <> () returnType:.J [primary] FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: diff --git a/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__J1.fir.ir.txt b/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__J1.fir.ir.txt index 2a25177aea3a0..1f7ee572df76a 100644 --- a/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__J1.fir.ir.txt +++ b/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__J1.fir.ir.txt @@ -5,6 +5,10 @@ CLASS IR_EXTERNAL_JAVA_DECLARATION_STUB CLASS name:J1 modality:OPEN visibility:p FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:f type:@[FlexibleNullability] T of .J1? visibility:public PROPERTY IR_EXTERNAL_JAVA_DECLARATION_STUB name:f2 visibility:public modality:FINAL [var] FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:f2 type:@[FlexibleNullability] T of .J1? visibility:public + PROPERTY IR_EXTERNAL_JAVA_DECLARATION_STUB name:s visibility:public modality:FINAL [var] + FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:s type:@[FlexibleNullability] T of .J1? visibility:public [static] + PROPERTY IR_EXTERNAL_JAVA_DECLARATION_STUB name:s2 visibility:public modality:FINAL [var] + FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:s2 type:@[FlexibleNullability] T of .J1? visibility:public [static] CONSTRUCTOR IR_EXTERNAL_JAVA_DECLARATION_STUB visibility:public <> () returnType:.J1.J1> [primary] FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: diff --git a/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__X.fir.ir.txt b/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__X.fir.ir.txt index 6241b53c01113..404a31d3d7e38 100644 --- a/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__X.fir.ir.txt +++ b/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__X.fir.ir.txt @@ -6,6 +6,8 @@ CLASS IR_EXTERNAL_JAVA_DECLARATION_STUB CLASS name:X modality:OPEN visibility:pu overridden: public final f: kotlin.Int [var] declared in .J FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:f type:kotlin.Int visibility:public + PROPERTY IR_EXTERNAL_JAVA_DECLARATION_STUB name:s2 visibility:public modality:FINAL [var] + FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:s2 type:kotlin.Int visibility:public [static] CONSTRUCTOR IR_EXTERNAL_JAVA_DECLARATION_STUB visibility:public <> () returnType:.X [primary] FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: diff --git a/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__X1.fir.ir.txt b/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__X1.fir.ir.txt index 7fbe354bd75b3..04399deaf9cc8 100644 --- a/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__X1.fir.ir.txt +++ b/compiler/testData/ir/irText/firProblems/FieldsFromJavaClass.__X1.fir.ir.txt @@ -7,6 +7,8 @@ CLASS IR_EXTERNAL_JAVA_DECLARATION_STUB CLASS name:X1 modality:OPEN visibility:p overridden: public final f: @[FlexibleNullability] T of .J1? [var] declared in .J1 FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:f type:@[FlexibleNullability] kotlin.String? visibility:public + PROPERTY IR_EXTERNAL_JAVA_DECLARATION_STUB name:s2 visibility:public modality:FINAL [var] + FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:s2 type:@[FlexibleNullability] kotlin.String? visibility:public [static] CONSTRUCTOR IR_EXTERNAL_JAVA_DECLARATION_STUB visibility:public/*package*/ <> () returnType:.X1.X1> [primary] FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: