diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt index 45ac823d09b..3cb2dd7d3bb 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt @@ -123,12 +123,6 @@ internal class ClassMemberGenerator( } body.statements += irDelegatingConstructorCall } - if (delegatedConstructor?.isThis == false) { - val instanceInitializerCall = IrInstanceInitializerCallImpl( - startOffset, endOffset, irClass.symbol, irFunction.constructedClassType - ) - body.statements += instanceInitializerCall - } if (containingClass is FirRegularClass && containingClass.contextReceivers.isNotEmpty()) { val contextReceiverFields = @@ -158,6 +152,13 @@ internal class ClassMemberGenerator( } } + if (delegatedConstructor?.isThis == false) { + val instanceInitializerCall = IrInstanceInitializerCallImpl( + startOffset, endOffset, irClass.symbol, irFunction.constructedClassType + ) + body.statements += instanceInitializerCall + } + val regularBody = firFunction.body?.let { visitor.convertToIrBlockBody(it) } if (regularBody != null) { body.statements += regularBody.statements diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 17d7e550f65..878f2ac4279 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -16788,6 +16788,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/unaryOperators.kt"); } + @Test + @TestMetadata("useContextReceiverInPropertyInitializer.kt") + public void testUseContextReceiverInPropertyInitializer() throws Exception { + runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/useContextReceiverInPropertyInitializer.kt"); + } + @Test @TestMetadata("useFromAnotherModule.kt") public void testUseFromAnotherModule() throws Exception { diff --git a/compiler/testData/codegen/box/extensionClasses/contextualPrimaryConstructorWithParams.kt b/compiler/testData/codegen/box/extensionClasses/contextualPrimaryConstructorWithParams.kt index 306a4814b91..931d6e7c0ad 100644 --- a/compiler/testData/codegen/box/extensionClasses/contextualPrimaryConstructorWithParams.kt +++ b/compiler/testData/codegen/box/extensionClasses/contextualPrimaryConstructorWithParams.kt @@ -1,6 +1,5 @@ // !LANGUAGE: +ContextReceivers // TARGET_BACKEND: JVM_IR -// IGNORE_BACKEND_FIR: JVM_IR // FIR status: context receivers aren't yet supported class O(val o: String) @@ -15,4 +14,4 @@ fun box(): String { val ok = OK("K") ok.result } -} \ No newline at end of file +} diff --git a/compiler/testData/codegen/box/extensionClasses/typealiasForContextualClass.kt b/compiler/testData/codegen/box/extensionClasses/typealiasForContextualClass.kt index ccd17ccb94f..edd0dcd4f22 100644 --- a/compiler/testData/codegen/box/extensionClasses/typealiasForContextualClass.kt +++ b/compiler/testData/codegen/box/extensionClasses/typealiasForContextualClass.kt @@ -1,6 +1,5 @@ // !LANGUAGE: +ContextReceivers // TARGET_BACKEND: JVM_IR -// IGNORE_BACKEND_FIR: JVM_IR // FIR status: context receivers aren't yet supported class A { @@ -15,4 +14,4 @@ class B { typealias C = B fun box(): String = - with(A()) { C().result } \ No newline at end of file + with(A()) { C().result } diff --git a/compiler/testData/codegen/box/extensionClasses/useFromAnotherModule.kt b/compiler/testData/codegen/box/extensionClasses/useFromAnotherModule.kt index 259fd0ab21b..d788b7d4606 100644 --- a/compiler/testData/codegen/box/extensionClasses/useFromAnotherModule.kt +++ b/compiler/testData/codegen/box/extensionClasses/useFromAnotherModule.kt @@ -1,6 +1,5 @@ // !LANGUAGE: +ContextReceivers // TARGET_BACKEND: JVM_IR -// IGNORE_BACKEND_FIR: JVM_IR // FIR status: context receivers aren't yet supported // MODULE: lib diff --git a/compiler/testData/codegen/box/extensionFunctions/contextReceivers/useContextReceiverInPropertyInitializer.kt b/compiler/testData/codegen/box/extensionFunctions/contextReceivers/useContextReceiverInPropertyInitializer.kt new file mode 100644 index 00000000000..33e9e422003 --- /dev/null +++ b/compiler/testData/codegen/box/extensionFunctions/contextReceivers/useContextReceiverInPropertyInitializer.kt @@ -0,0 +1,19 @@ +// !LANGUAGE: +ContextReceivers +// TARGET_BACKEND: JVM_IR + +class Components(val x: String) + +context(Components) +abstract class A(val y: String) { + val w: String = x + fun foo(): String = w + y +} + +context(Components) +class B(y: String) : A(y) + +fun box(): String { + return with(Components("O")) { + B("K").foo() + } +} diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/class.fir.ir.txt b/compiler/testData/ir/irText/declarations/contextReceivers/class.fir.ir.txt index 02712a95634..26252740552 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/class.fir.ir.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/class.fir.ir.txt @@ -37,10 +37,10 @@ FILE fqName: fileName:/class.kt VALUE_PARAMETER name:arg index:1 type:kotlin.Any BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Inner modality:FINAL visibility:public superTypes:[kotlin.Any]' SET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:.Outer visibility:private [final]' type=kotlin.Unit origin=null receiver: GET_VAR ': .Inner declared in .Inner' type=.Inner origin=null value: GET_VAR '_context_receiver_0: .Outer declared in .Inner.' type=.Outer origin=null + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Inner modality:FINAL visibility:public superTypes:[kotlin.Any]' FUN name:bar visibility:public modality:FINAL <> ($this:.Inner) returnType:kotlin.Int $this: VALUE_PARAMETER name: type:.Inner BLOCK_BODY diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/class.fir.kt.txt b/compiler/testData/ir/irText/declarations/contextReceivers/class.fir.kt.txt index be531043e1b..af6f865267a 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/class.fir.kt.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/class.fir.kt.txt @@ -15,9 +15,9 @@ class Inner { private /* final field */ val contextReceiverField0: Outer constructor(_context_receiver_0: Outer, arg: Any) /* primary */ { super/*Any*/() + .#contextReceiverField0 = _context_receiver_0 /* () */ - .#contextReceiverField0 = _context_receiver_0 } fun bar(): Int { diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.fir.ir.txt b/compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.fir.ir.txt index 10b399aa5ea..29077396e4f 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.fir.ir.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.fir.ir.txt @@ -38,10 +38,10 @@ FILE fqName: fileName:/contextualPrimaryConstructorWithParams.kt VALUE_PARAMETER name:k index:1 type:kotlin.String BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:OK modality:FINAL visibility:public superTypes:[kotlin.Any]' SET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:.O visibility:private [final]' type=kotlin.Unit origin=null receiver: GET_VAR ': .OK declared in .OK' type=.OK origin=null value: GET_VAR '_context_receiver_0: .O declared in .OK.' type=.O origin=null + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:OK modality:FINAL visibility:public superTypes:[kotlin.Any]' PROPERTY name:k visibility:public modality:FINAL [val] FIELD PROPERTY_BACKING_FIELD name:k type:kotlin.String visibility:private [final] EXPRESSION_BODY diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.fir.kt.txt b/compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.fir.kt.txt index 179265d91a6..fe771d02ea0 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.fir.kt.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/contextualPrimaryConstructorWithParams.fir.kt.txt @@ -15,9 +15,9 @@ class OK { private /* final field */ val contextReceiverField0: O constructor(_context_receiver_0: O, k: String) /* primary */ { super/*Any*/() + .#contextReceiverField0 = _context_receiver_0 /* () */ - .#contextReceiverField0 = _context_receiver_0 } val k: String diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/delegatedPropertiesOperators.fir.ir.txt b/compiler/testData/ir/irText/declarations/contextReceivers/delegatedPropertiesOperators.fir.ir.txt index 1ac12413763..9626931a760 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/delegatedPropertiesOperators.fir.ir.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/delegatedPropertiesOperators.fir.ir.txt @@ -88,10 +88,10 @@ FILE fqName: fileName:/delegatedPropertiesOperators.kt VALUE_PARAMETER name:_context_receiver_0 index:0 type:kotlin.Int BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Result modality:FINAL visibility:public superTypes:[kotlin.Any]' SET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:kotlin.Int visibility:private [final]' type=kotlin.Unit origin=null receiver: GET_VAR ': .Result declared in .Result' type=.Result origin=null value: GET_VAR '_context_receiver_0: kotlin.Int declared in .Result.' type=kotlin.Int origin=null + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Result modality:FINAL visibility:public superTypes:[kotlin.Any]' PROPERTY name:s visibility:public modality:FINAL [delegated,var] FIELD PROPERTY_DELEGATE name:s$delegate type:.Delegate visibility:private [final] EXPRESSION_BODY diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/delegatedPropertiesOperators.fir.kt.txt b/compiler/testData/ir/irText/declarations/contextReceivers/delegatedPropertiesOperators.fir.kt.txt index 25cb28c5e4c..151b5e14fe0 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/delegatedPropertiesOperators.fir.kt.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/delegatedPropertiesOperators.fir.kt.txt @@ -31,9 +31,9 @@ class Result { private /* final field */ val contextReceiverField0: Int constructor(_context_receiver_0: Int) /* primary */ { super/*Any*/() + .#contextReceiverField0 = _context_receiver_0 /* () */ - .#contextReceiverField0 = _context_receiver_0 } var s: String /* by */ diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/genericOuterClass.fir.ir.txt b/compiler/testData/ir/irText/declarations/contextReceivers/genericOuterClass.fir.ir.txt index 5e9b06f4332..d219678c4ab 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/genericOuterClass.fir.ir.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/genericOuterClass.fir.ir.txt @@ -7,10 +7,10 @@ FILE fqName: fileName:/genericOuterClass.kt VALUE_PARAMETER name:_context_receiver_0 index:0 type:T of .A BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' SET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:T of .A visibility:private [final]' type=kotlin.Unit origin=null receiver: GET_VAR ': .A.A> declared in .A' type=.A.A> origin=null value: GET_VAR '_context_receiver_0: T of .A declared in .A.' type=T of .A origin=null + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any @@ -32,10 +32,10 @@ FILE fqName: fileName:/genericOuterClass.kt VALUE_PARAMETER name:_context_receiver_0 index:0 type:kotlin.collections.Collection

.B> BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:B modality:FINAL visibility:public superTypes:[kotlin.Any]' SET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:kotlin.collections.Collection

.B> visibility:private [final]' type=kotlin.Unit origin=null receiver: GET_VAR ': .B

.B> declared in .B' type=.B

.B> origin=null value: GET_VAR '_context_receiver_0: kotlin.collections.Collection

.B> declared in .B.' type=kotlin.collections.Collection

.B> origin=null + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:B modality:FINAL visibility:public superTypes:[kotlin.Any]' FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/genericOuterClass.fir.kt.txt b/compiler/testData/ir/irText/declarations/contextReceivers/genericOuterClass.fir.kt.txt index 37477d3faee..b248966aae9 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/genericOuterClass.fir.kt.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/genericOuterClass.fir.kt.txt @@ -2,9 +2,9 @@ class A { private /* final field */ val contextReceiverField0: T constructor(_context_receiver_0: T) /* primary */ { super/*Any*/() + .#contextReceiverField0 = _context_receiver_0 /* () */ - .#contextReceiverField0 = _context_receiver_0 } } @@ -13,9 +13,9 @@ class B

{ private /* final field */ val contextReceiverField0: Collection

constructor(_context_receiver_0: Collection

) /* primary */ { super/*Any*/() + .#contextReceiverField0 = _context_receiver_0 /* () */ - .#contextReceiverField0 = _context_receiver_0 } } diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/iteratorOperator.fir.ir.txt b/compiler/testData/ir/irText/declarations/contextReceivers/iteratorOperator.fir.ir.txt index 9048269ea03..198f37aa162 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/iteratorOperator.fir.ir.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/iteratorOperator.fir.ir.txt @@ -197,10 +197,10 @@ FILE fqName: fileName:/iteratorOperator.kt VALUE_PARAMETER name:counter index:1 type:.Counter BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:CounterIterator modality:FINAL visibility:public superTypes:[kotlin.collections.Iterator]' SET_FIELD 'FIELD FIELD_FOR_CLASS_CONTEXT_RECEIVER name:contextReceiverField0 type:.CounterConfig visibility:private [final]' type=kotlin.Unit origin=null receiver: GET_VAR ': .CounterIterator declared in .CounterIterator' type=.CounterIterator origin=null value: GET_VAR '_context_receiver_0: .CounterConfig declared in .CounterIterator.' type=.CounterConfig origin=null + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:CounterIterator modality:FINAL visibility:public superTypes:[kotlin.collections.Iterator]' PROPERTY name:counter visibility:private modality:FINAL [val] FIELD PROPERTY_BACKING_FIELD name:counter type:.Counter visibility:private [final] EXPRESSION_BODY diff --git a/compiler/testData/ir/irText/declarations/contextReceivers/iteratorOperator.fir.kt.txt b/compiler/testData/ir/irText/declarations/contextReceivers/iteratorOperator.fir.kt.txt index ffa4d46e02e..7c22d442f0e 100644 --- a/compiler/testData/ir/irText/declarations/contextReceivers/iteratorOperator.fir.kt.txt +++ b/compiler/testData/ir/irText/declarations/contextReceivers/iteratorOperator.fir.kt.txt @@ -89,9 +89,9 @@ class CounterIterator : Iterator { private /* final field */ val contextReceiverField0: CounterConfig constructor(_context_receiver_0: CounterConfig, counter: Counter) /* primary */ { super/*Any*/() + .#contextReceiverField0 = _context_receiver_0 /* () */ - .#contextReceiverField0 = _context_receiver_0 } private val counter: Counter diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 361bd412aec..26618ee19b8 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -16788,6 +16788,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/unaryOperators.kt"); } + @Test + @TestMetadata("useContextReceiverInPropertyInitializer.kt") + public void testUseContextReceiverInPropertyInitializer() throws Exception { + runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/useContextReceiverInPropertyInitializer.kt"); + } + @Test @TestMetadata("useFromAnotherModule.kt") public void testUseFromAnotherModule() throws Exception {