JVM_IR KT-42758 don't use 'this' for object self-reference by name

This commit is contained in:
Dmitry Petrov
2020-11-19 12:51:03 +03:00
parent 118a7d4e34
commit e17158d961
11 changed files with 203 additions and 7 deletions
@@ -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");
@@ -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");
@@ -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 `<clinit>` 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
+23
View File
@@ -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 <reified T : Serializable> 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"
}
@@ -0,0 +1,72 @@
FILE fqName:<root> fileName:/objectByNameInsideObject.kt
CLASS CLASS name:Base modality:OPEN visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Base
CONSTRUCTOR visibility:public <> (f1:kotlin.Function0<kotlin.Any>) returnType:<root>.Base [primary]
VALUE_PARAMETER name:f1 index:0 type:kotlin.Function0<kotlin.Any>
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [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<kotlin.Any> visibility:private [final]
EXPRESSION_BODY
GET_VAR 'f1: kotlin.Function0<kotlin.Any> declared in <root>.Base.<init>' type=kotlin.Function0<kotlin.Any> origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-f1> visibility:public modality:FINAL <> ($this:<root>.Base) returnType:kotlin.Function0<kotlin.Any>
correspondingProperty: PROPERTY name:f1 visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Base
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-f1> (): kotlin.Function0<kotlin.Any> declared in <root>.Base'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:f1 type:kotlin.Function0<kotlin.Any> visibility:private [final]' type=kotlin.Function0<kotlin.Any> origin=null
receiver: GET_VAR '<this>: <root>.Base declared in <root>.Base.<get-f1>' type=<root>.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:<this> 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:<this> 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:<this> type:kotlin.Any
CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[<root>.Base]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Thing
CONSTRUCTOR visibility:private <> () returnType:<root>.Thing [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> (f1: kotlin.Function0<kotlin.Any>) [primary] declared in <root>.Base'
f1: FUN_EXPR type=kotlin.Function0<kotlin.Any> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Any
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Any declared in <root>.Thing.<init>'
GET_OBJECT 'CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[<root>.Base]' type=<root>.Thing
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[<root>.Base]'
FUN name:test1 visibility:public modality:FINAL <> ($this:<root>.Thing) returnType:<root>.Thing
$this: VALUE_PARAMETER name:<this> type:<root>.Thing
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test1 (): <root>.Thing declared in <root>.Thing'
GET_OBJECT 'CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[<root>.Base]' type=<root>.Thing
FUN name:test2 visibility:public modality:FINAL <> ($this:<root>.Thing) returnType:<root>.Thing
$this: VALUE_PARAMETER name:<this> type:<root>.Thing
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test2 (): <root>.Thing declared in <root>.Thing'
GET_VAR '<this>: <root>.Thing declared in <root>.Thing.test2' type=<root>.Thing origin=null
PROPERTY FAKE_OVERRIDE name:f1 visibility:public modality:FINAL [fake_override,val]
FUN FAKE_OVERRIDE name:<get-f1> visibility:public modality:FINAL <> ($this:<root>.Base) returnType:kotlin.Function0<kotlin.Any> [fake_override]
correspondingProperty: PROPERTY FAKE_OVERRIDE name:f1 visibility:public modality:FINAL [fake_override,val]
overridden:
public final fun <get-f1> (): kotlin.Function0<kotlin.Any> declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:<root>.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:<this> 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:<this> 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:<this> type:kotlin.Any
@@ -0,0 +1,6 @@
open class Base(val f1: () -> Any)
object Thing : Base({ Thing }) {
fun test1() = Thing
fun test2() = this
}
@@ -0,0 +1,72 @@
FILE fqName:<root> fileName:/objectByNameInsideObject.kt
CLASS CLASS name:Base modality:OPEN visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Base
CONSTRUCTOR visibility:public <> (f1:kotlin.Function0<kotlin.Any>) returnType:<root>.Base [primary]
VALUE_PARAMETER name:f1 index:0 type:kotlin.Function0<kotlin.Any>
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [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<kotlin.Any> visibility:private [final]
EXPRESSION_BODY
GET_VAR 'f1: kotlin.Function0<kotlin.Any> declared in <root>.Base.<init>' type=kotlin.Function0<kotlin.Any> origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-f1> visibility:public modality:FINAL <> ($this:<root>.Base) returnType:kotlin.Function0<kotlin.Any>
correspondingProperty: PROPERTY name:f1 visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Base
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-f1> (): kotlin.Function0<kotlin.Any> declared in <root>.Base'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:f1 type:kotlin.Function0<kotlin.Any> visibility:private [final]' type=kotlin.Function0<kotlin.Any> origin=null
receiver: GET_VAR '<this>: <root>.Base declared in <root>.Base.<get-f1>' type=<root>.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:<this> 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:<this> 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:<this> type:kotlin.Any
CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[<root>.Base]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Thing
CONSTRUCTOR visibility:private <> () returnType:<root>.Thing [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> (f1: kotlin.Function0<kotlin.Any>) [primary] declared in <root>.Base'
f1: FUN_EXPR type=kotlin.Function0<kotlin.Any> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Any
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Any declared in <root>.Thing.<init>'
GET_OBJECT 'CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[<root>.Base]' type=<root>.Thing
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[<root>.Base]'
FUN name:test1 visibility:public modality:FINAL <> ($this:<root>.Thing) returnType:<root>.Thing
$this: VALUE_PARAMETER name:<this> type:<root>.Thing
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test1 (): <root>.Thing declared in <root>.Thing'
GET_OBJECT 'CLASS OBJECT name:Thing modality:FINAL visibility:public superTypes:[<root>.Base]' type=<root>.Thing
FUN name:test2 visibility:public modality:FINAL <> ($this:<root>.Thing) returnType:<root>.Thing
$this: VALUE_PARAMETER name:<this> type:<root>.Thing
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test2 (): <root>.Thing declared in <root>.Thing'
GET_VAR '<this>: <root>.Thing declared in <root>.Thing.test2' type=<root>.Thing origin=null
PROPERTY FAKE_OVERRIDE name:f1 visibility:public modality:FINAL [fake_override,val]
FUN FAKE_OVERRIDE name:<get-f1> visibility:public modality:FINAL <> ($this:<root>.Base) returnType:kotlin.Function0<kotlin.Any> [fake_override]
correspondingProperty: PROPERTY FAKE_OVERRIDE name:f1 visibility:public modality:FINAL [fake_override,val]
overridden:
public final fun <get-f1> (): kotlin.Function0<kotlin.Any> declared in <root>.Base
$this: VALUE_PARAMETER name:<this> type:<root>.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 <root>.Base
$this: VALUE_PARAMETER name:<this> 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 <root>.Base
$this: VALUE_PARAMETER name:<this> 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 <root>.Base
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
@@ -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");
@@ -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");
@@ -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");
@@ -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");