diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 6e7c644d7de..c510346ab76 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -19483,6 +19483,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/objects/kt4086.kt"); } + @TestMetadata("kt42758.kt") + public void testKt42758() throws Exception { + runTest("compiler/testData/codegen/box/objects/kt42758.kt"); + } + @TestMetadata("kt535.kt") public void testKt535() throws Exception { runTest("compiler/testData/codegen/box/objects/kt535.kt"); diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java index a058e3a353a..478ed5273ab 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/fir/Fir2IrTextTestGenerated.java @@ -1192,6 +1192,11 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest { runTest("compiler/testData/ir/irText/expressions/objectAsCallable.kt"); } + @TestMetadata("objectByNameInsideObject.kt") + public void testObjectByNameInsideObject() throws Exception { + runTest("compiler/testData/ir/irText/expressions/objectByNameInsideObject.kt"); + } + @TestMetadata("objectClassReference.kt") public void testObjectClassReference() throws Exception { runTest("compiler/testData/ir/irText/expressions/objectClassReference.kt"); diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt index e1a3e5186c4..155eaa07db6 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt @@ -52,13 +52,6 @@ private class SingletonReferencesLowering(val context: JvmBackendContext) : File } override fun visitGetObjectValue(expression: IrGetObjectValue): IrExpression { - // When in an instance method of the object, use the dispatch receiver. Do not use the dispatch - // receiver in the constructor, as in the case of an `object` the only way it can be used is - // in a super constructor call argument (=> it has JVM type "uninitialized this" and is not usable) - // while for a `companion object` all initializers are in `` and have no receiver at all. - thisOfClass(expression.symbol.owner, false)?.let { - return IrGetValueImpl(expression.startOffset, expression.endOffset, it.symbol) - } val instanceField = if (allScopes.any { it.irElement == expression.symbol.owner }) context.cachedDeclarations.getPrivateFieldForObjectInstance(expression.symbol.owner) // Constructor or static method. else diff --git a/compiler/testData/codegen/box/objects/kt42758.kt b/compiler/testData/codegen/box/objects/kt42758.kt new file mode 100644 index 00000000000..54e3680f3a4 --- /dev/null +++ b/compiler/testData/codegen/box/objects/kt42758.kt @@ -0,0 +1,23 @@ +// TARGET_BACKEND: JVM +// FULL_JDK +// WITH_RUNTIME + +import java.io.* + +object Thing : Serializable { + private fun readResolve(): Any = Thing +} + +private inline fun roundTrip(value: T): T { + val outputStream = ByteArrayOutputStream() + ObjectOutputStream(outputStream).use { it.writeObject(value) } + val inputStream = ByteArrayInputStream(outputStream.toByteArray()) + return ObjectInputStream(inputStream).use { it.readObject() } as T +} + +fun box(): String { + if (Thing !== roundTrip(Thing)) + throw Exception("Thing !== roundTrip(Thing)") + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/objectByNameInsideObject.fir.txt b/compiler/testData/ir/irText/expressions/objectByNameInsideObject.fir.txt new file mode 100644 index 00000000000..aae5f871fee --- /dev/null +++ b/compiler/testData/ir/irText/expressions/objectByNameInsideObject.fir.txt @@ -0,0 +1,72 @@ +FILE fqName: fileName:/objectByNameInsideObject.kt + CLASS CLASS name:Base modality:OPEN visibility:public superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Base + CONSTRUCTOR visibility:public <> (f1:kotlin.Function0) returnType:.Base [primary] + VALUE_PARAMETER name:f1 index:0 type:kotlin.Function0 + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Base modality:OPEN visibility:public superTypes:[kotlin.Any]' + PROPERTY name:f1 visibility:public modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:f1 type:kotlin.Function0 visibility:private [final] + EXPRESSION_BODY + GET_VAR 'f1: kotlin.Function0 declared in .Base.' type=kotlin.Function0 origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Base) returnType:kotlin.Function0 + correspondingProperty: PROPERTY name:f1 visibility:public modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.Base + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): kotlin.Function0 declared in .Base' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:f1 type:kotlin.Function0 visibility:private [final]' type=kotlin.Function0 origin=null + receiver: GET_VAR ': .Base declared in .Base.' type=.Base origin=null + 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 + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[.Base] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Thing + CONSTRUCTOR visibility:private <> () returnType:.Thing [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor (f1: kotlin.Function0) [primary] declared in .Base' + f1: FUN_EXPR type=kotlin.Function0 origin=LAMBDA + FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> () returnType:kotlin.Any + BLOCK_BODY + RETURN type=kotlin.Nothing from='local final fun (): kotlin.Any declared in .Thing.' + GET_OBJECT 'CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[.Base]' type=.Thing + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[.Base]' + FUN name:test1 visibility:public modality:FINAL <> ($this:.Thing) returnType:.Thing + $this: VALUE_PARAMETER name: type:.Thing + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun test1 (): .Thing declared in .Thing' + GET_OBJECT 'CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[.Base]' type=.Thing + FUN name:test2 visibility:public modality:FINAL <> ($this:.Thing) returnType:.Thing + $this: VALUE_PARAMETER name: type:.Thing + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun test2 (): .Thing declared in .Thing' + GET_VAR ': .Thing declared in .Thing.test2' type=.Thing origin=null + PROPERTY FAKE_OVERRIDE name:f1 visibility:public modality:FINAL [fake_override,val] + FUN FAKE_OVERRIDE name: visibility:public modality:FINAL <> ($this:.Base) returnType:kotlin.Function0 [fake_override] + correspondingProperty: PROPERTY FAKE_OVERRIDE name:f1 visibility:public modality:FINAL [fake_override,val] + overridden: + public final fun (): kotlin.Function0 declared in .Base + $this: VALUE_PARAMETER name: type:.Base + 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 + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any diff --git a/compiler/testData/ir/irText/expressions/objectByNameInsideObject.kt b/compiler/testData/ir/irText/expressions/objectByNameInsideObject.kt new file mode 100644 index 00000000000..945c11ceea5 --- /dev/null +++ b/compiler/testData/ir/irText/expressions/objectByNameInsideObject.kt @@ -0,0 +1,6 @@ +open class Base(val f1: () -> Any) + +object Thing : Base({ Thing }) { + fun test1() = Thing + fun test2() = this +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/objectByNameInsideObject.txt b/compiler/testData/ir/irText/expressions/objectByNameInsideObject.txt new file mode 100644 index 00000000000..87b401c2bcd --- /dev/null +++ b/compiler/testData/ir/irText/expressions/objectByNameInsideObject.txt @@ -0,0 +1,72 @@ +FILE fqName: fileName:/objectByNameInsideObject.kt + CLASS CLASS name:Base modality:OPEN visibility:public superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Base + CONSTRUCTOR visibility:public <> (f1:kotlin.Function0) returnType:.Base [primary] + VALUE_PARAMETER name:f1 index:0 type:kotlin.Function0 + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Base modality:OPEN visibility:public superTypes:[kotlin.Any]' + PROPERTY name:f1 visibility:public modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:f1 type:kotlin.Function0 visibility:private [final] + EXPRESSION_BODY + GET_VAR 'f1: kotlin.Function0 declared in .Base.' type=kotlin.Function0 origin=INITIALIZE_PROPERTY_FROM_PARAMETER + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Base) returnType:kotlin.Function0 + correspondingProperty: PROPERTY name:f1 visibility:public modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.Base + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): kotlin.Function0 declared in .Base' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:f1 type:kotlin.Function0 visibility:private [final]' type=kotlin.Function0 origin=null + receiver: GET_VAR ': .Base declared in .Base.' type=.Base origin=null + 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 + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String declared in kotlin.Any + $this: VALUE_PARAMETER name: type:kotlin.Any + CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[.Base] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Thing + CONSTRUCTOR visibility:private <> () returnType:.Thing [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor (f1: kotlin.Function0) [primary] declared in .Base' + f1: FUN_EXPR type=kotlin.Function0 origin=LAMBDA + FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> () returnType:kotlin.Any + BLOCK_BODY + RETURN type=kotlin.Nothing from='local final fun (): kotlin.Any declared in .Thing.' + GET_OBJECT 'CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[.Base]' type=.Thing + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[.Base]' + FUN name:test1 visibility:public modality:FINAL <> ($this:.Thing) returnType:.Thing + $this: VALUE_PARAMETER name: type:.Thing + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun test1 (): .Thing declared in .Thing' + GET_OBJECT 'CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[.Base]' type=.Thing + FUN name:test2 visibility:public modality:FINAL <> ($this:.Thing) returnType:.Thing + $this: VALUE_PARAMETER name: type:.Thing + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun test2 (): .Thing declared in .Thing' + GET_VAR ': .Thing declared in .Thing.test2' type=.Thing origin=null + PROPERTY FAKE_OVERRIDE name:f1 visibility:public modality:FINAL [fake_override,val] + FUN FAKE_OVERRIDE name: visibility:public modality:FINAL <> ($this:.Base) returnType:kotlin.Function0 [fake_override] + correspondingProperty: PROPERTY FAKE_OVERRIDE name:f1 visibility:public modality:FINAL [fake_override,val] + overridden: + public final fun (): kotlin.Function0 declared in .Base + $this: VALUE_PARAMETER name: type:.Base + 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 [fake_override,operator] declared in .Base + $this: VALUE_PARAMETER name: type:kotlin.Any + VALUE_PARAMETER name:other index:0 type:kotlin.Any? + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] + overridden: + public open fun hashCode (): kotlin.Int [fake_override] declared in .Base + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] + overridden: + public open fun toString (): kotlin.String [fake_override] declared in .Base + $this: VALUE_PARAMETER name: type:kotlin.Any diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index a7830f03dd5..5ef49e33862 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -20883,6 +20883,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/objects/kt4086.kt"); } + @TestMetadata("kt42758.kt") + public void testKt42758() throws Exception { + runTest("compiler/testData/codegen/box/objects/kt42758.kt"); + } + @TestMetadata("kt535.kt") public void testKt535() throws Exception { runTest("compiler/testData/codegen/box/objects/kt535.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index ac04cff9c25..c09f7381aa5 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -20883,6 +20883,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/objects/kt4086.kt"); } + @TestMetadata("kt42758.kt") + public void testKt42758() throws Exception { + runTest("compiler/testData/codegen/box/objects/kt42758.kt"); + } + @TestMetadata("kt535.kt") public void testKt535() throws Exception { runTest("compiler/testData/codegen/box/objects/kt535.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 5df032e166f..47e76b02666 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -19483,6 +19483,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/objects/kt4086.kt"); } + @TestMetadata("kt42758.kt") + public void testKt42758() throws Exception { + runTest("compiler/testData/codegen/box/objects/kt42758.kt"); + } + @TestMetadata("kt535.kt") public void testKt535() throws Exception { runTest("compiler/testData/codegen/box/objects/kt535.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java index f276c5a2813..3d038845229 100644 --- a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -1191,6 +1191,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { runTest("compiler/testData/ir/irText/expressions/objectAsCallable.kt"); } + @TestMetadata("objectByNameInsideObject.kt") + public void testObjectByNameInsideObject() throws Exception { + runTest("compiler/testData/ir/irText/expressions/objectByNameInsideObject.kt"); + } + @TestMetadata("objectClassReference.kt") public void testObjectClassReference() throws Exception { runTest("compiler/testData/ir/irText/expressions/objectClassReference.kt");