Fir2Ir: more precise calculation of enum class modality

Use the same condition as in the already existing `createIrEnumEntry`
function (and as in psi2ir): enum class should be final unless there's
an enum entry with any declaration other than its constructor.

 #KT-57216
This commit is contained in:
Alexander Udalov
2023-03-15 00:11:36 +01:00
committed by Space Team
parent 2cc3b542fd
commit 68b94b07b8
17 changed files with 43 additions and 45 deletions
@@ -193,7 +193,9 @@ class Fir2IrClassifierStorage(
declarations.any { it is FirCallableDeclaration && it.modality == Modality.ABSTRACT } -> {
Modality.ABSTRACT
}
declarations.none { it is FirEnumEntry && it.initializer != null } -> {
declarations.none {
it is FirEnumEntry && isEnumEntryWhichRequiresSubclass(it)
} -> {
Modality.FINAL
}
hasAbstractMembersInScope() -> {
@@ -558,15 +560,14 @@ class Fir2IrClassifierStorage(
if (irParent != null) {
this.parent = irParent
}
val initializer = enumEntry.initializer
if (initializer is FirAnonymousObjectExpression) {
// An enum entry with its own members
if (initializer.anonymousObject.declarations.any { it !is FirConstructor }) {
val klass = getIrAnonymousObjectForEnumEntry(initializer.anonymousObject, enumEntry.name, irParent)
this.correspondingClass = klass
}
if (isEnumEntryWhichRequiresSubclass(enumEntry)) {
// An enum entry with its own members requires an anonymous object generated.
// Otherwise, this is a default-ish enum entry whose initializer would be a delegating constructor call,
// which will be translated via visitor later.
val klass = getIrAnonymousObjectForEnumEntry(
(enumEntry.initializer as FirAnonymousObjectExpression).anonymousObject, enumEntry.name, irParent
)
this.correspondingClass = klass
}
declarationStorage.leaveScope(this)
}
@@ -576,6 +577,11 @@ class Fir2IrClassifierStorage(
}
}
private fun isEnumEntryWhichRequiresSubclass(enumEntry: FirEnumEntry): Boolean {
val initializer = enumEntry.initializer
return initializer is FirAnonymousObjectExpression && initializer.anonymousObject.declarations.any { it !is FirConstructor }
}
fun getIrClassSymbol(firClassSymbol: FirClassSymbol<*>, forceTopLevelPrivate: Boolean = false): IrClassSymbol {
val firClass = firClassSymbol.fir
getCachedIrClass(firClass)?.let { return it.symbol }
@@ -1,6 +1,4 @@
// WITH_STDLIB
// IGNORE_BACKEND_K2: JVM_IR
// FIR status: KT-57216 K2: non-trivial enum declaration does not have ACC_FINAL in the bytecode
enum class Test {
@Deprecated("") ENTRY1,
-3
View File
@@ -1,6 +1,3 @@
// IGNORE_BACKEND_K2: JVM_IR
// FIR status: KT-57216 K2: non-trivial enum declaration does not have ACC_FINAL in the bytecode
enum class SimpleEnum {
A, B, C
}
@@ -3,9 +3,6 @@
// FULL_JDK
// WITH_STDLIB
// IGNORE_BACKEND_K2: JVM_IR
// FIR status: KT-57216 K2: non-trivial enum declaration does not have ACC_FINAL in the bytecode
enum class SimpleEnum {
A, B, C
}
@@ -1,6 +1,4 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND_K2: JVM_IR
// FIR status: KT-57216 K2: non-trivial enum declaration does not have ACC_FINAL in the bytecode
inline class Z(val x: Int)
+6 -6
View File
@@ -61,14 +61,14 @@ FILE fqName:<root> fileName:/enum.kt
overridden:
public final fun <get-ordinal> (): kotlin.Int declared in kotlin.Enum
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<E of kotlin.Enum>
CLASS ENUM_CLASS name:TestEnum2 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.TestEnum2>]
CLASS ENUM_CLASS name:TestEnum2 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.TestEnum2>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.TestEnum2
CONSTRUCTOR visibility:private <> (x:kotlin.Int) returnType:<root>.TestEnum2 [primary]
VALUE_PARAMETER name:x index:0 type:kotlin.Int
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.TestEnum2
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:TestEnum2 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.TestEnum2>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:TestEnum2 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.TestEnum2>]'
PROPERTY name:x visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:x type:kotlin.Int visibility:private [final]
EXPRESSION_BODY
@@ -470,7 +470,7 @@ FILE fqName:<root> fileName:/enum.kt
overridden:
public final fun <get-ordinal> (): kotlin.Int declared in kotlin.Enum
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<E of kotlin.Enum>
CLASS ENUM_CLASS name:TestEnum5 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.TestEnum5>]
CLASS ENUM_CLASS name:TestEnum5 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.TestEnum5>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.TestEnum5
CONSTRUCTOR visibility:private <> (x:kotlin.Int) returnType:<root>.TestEnum5 [primary]
VALUE_PARAMETER name:x index:0 type:kotlin.Int
@@ -479,7 +479,7 @@ FILE fqName:<root> fileName:/enum.kt
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.TestEnum5
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:TestEnum5 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.TestEnum5>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:TestEnum5 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.TestEnum5>]'
PROPERTY name:x visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:x type:kotlin.Int visibility:private [final]
EXPRESSION_BODY
@@ -554,7 +554,7 @@ FILE fqName:<root> fileName:/enum.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun f (): kotlin.Int declared in <root>'
CONST Int type=kotlin.Int value=1
CLASS ENUM_CLASS name:TestEnum6 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.TestEnum6>]
CLASS ENUM_CLASS name:TestEnum6 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.TestEnum6>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.TestEnum6
CONSTRUCTOR visibility:private <> (x:kotlin.Int, y:kotlin.Int) returnType:<root>.TestEnum6 [primary]
VALUE_PARAMETER name:x index:0 type:kotlin.Int
@@ -562,7 +562,7 @@ FILE fqName:<root> fileName:/enum.kt
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.TestEnum6
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:TestEnum6 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.TestEnum6>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:TestEnum6 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.TestEnum6>]'
PROPERTY name:x visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:x type:kotlin.Int visibility:private [final]
EXPRESSION_BODY
+3 -3
View File
@@ -18,7 +18,7 @@ enum class TestEnum1 : Enum<TestEnum1> {
}
open enum class TestEnum2 : Enum<TestEnum2> {
enum class TestEnum2 : Enum<TestEnum2> {
private constructor(x: Int) /* primary */ {
super/*Enum*/<TestEnum2>()
/* <init>() */
@@ -131,7 +131,7 @@ abstract enum class TestEnum4 : Enum<TestEnum4> {
}
open enum class TestEnum5 : Enum<TestEnum5> {
enum class TestEnum5 : Enum<TestEnum5> {
private constructor(x: Int = 0) /* primary */ {
super/*Enum*/<TestEnum5>()
/* <init>() */
@@ -161,7 +161,7 @@ fun f(): Int {
return 1
}
open enum class TestEnum6 : Enum<TestEnum6> {
enum class TestEnum6 : Enum<TestEnum6> {
private constructor(x: Int, y: Int) /* primary */ {
super/*Enum*/<TestEnum6>()
/* <init>() */
@@ -58,14 +58,14 @@ FILE fqName:<root> fileName:/enumClassModality.kt
overridden:
public final fun <get-ordinal> (): kotlin.Int declared in kotlin.Enum
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<E of kotlin.Enum>
CLASS ENUM_CLASS name:TestFinalEnum2 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.TestFinalEnum2>]
CLASS ENUM_CLASS name:TestFinalEnum2 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.TestFinalEnum2>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.TestFinalEnum2
CONSTRUCTOR visibility:private <> (x:kotlin.Int) returnType:<root>.TestFinalEnum2 [primary]
VALUE_PARAMETER name:x index:0 type:kotlin.Int
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.TestFinalEnum2
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:TestFinalEnum2 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.TestFinalEnum2>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:TestFinalEnum2 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.TestFinalEnum2>]'
PROPERTY name:x visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:x type:kotlin.Int visibility:private [final]
EXPRESSION_BODY
@@ -16,7 +16,7 @@ enum class TestFinalEnum1 : Enum<TestFinalEnum1> {
}
open enum class TestFinalEnum2 : Enum<TestFinalEnum2> {
enum class TestFinalEnum2 : Enum<TestFinalEnum2> {
private constructor(x: Int) /* primary */ {
super/*Enum*/<TestFinalEnum2>()
/* <init>() */
@@ -184,3 +184,4 @@ abstract enum class TestAbstractEnum2 : IFoo, Enum<TestAbstractEnum2> {
get(): EnumEntries<TestAbstractEnum2> /* Synthetic body for ENUM_ENTRIES */
}
@@ -1,12 +1,12 @@
FILE fqName:<root> fileName:/enumWithSecondaryCtor.kt
CLASS ENUM_CLASS name:Test0 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.Test0>]
CLASS ENUM_CLASS name:Test0 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.Test0>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Test0
CONSTRUCTOR visibility:private <> (x:kotlin.Int) returnType:<root>.Test0 [primary]
VALUE_PARAMETER name:x index:0 type:kotlin.Int
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.Test0
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:Test0 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.Test0>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:Test0 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.Test0>]'
PROPERTY name:x visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:x type:kotlin.Int visibility:private [final]
EXPRESSION_BODY
@@ -74,14 +74,14 @@ FILE fqName:<root> fileName:/enumWithSecondaryCtor.kt
overridden:
public final fun <get-ordinal> (): kotlin.Int declared in kotlin.Enum
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<E of kotlin.Enum>
CLASS ENUM_CLASS name:Test1 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.Test1>]
CLASS ENUM_CLASS name:Test1 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.Test1>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Test1
CONSTRUCTOR visibility:private <> (x:kotlin.Int) returnType:<root>.Test1 [primary]
VALUE_PARAMETER name:x index:0 type:kotlin.Int
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.Test1
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:Test1 modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.Test1>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:Test1 modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.Test1>]'
PROPERTY name:x visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:x type:kotlin.Int visibility:private [final]
EXPRESSION_BODY
@@ -1,4 +1,4 @@
open enum class Test0 : Enum<Test0> {
enum class Test0 : Enum<Test0> {
private constructor(x: Int) /* primary */ {
super/*Enum*/<Test0>()
/* <init>() */
@@ -24,7 +24,7 @@ open enum class Test0 : Enum<Test0> {
}
open enum class Test1 : Enum<Test1> {
enum class Test1 : Enum<Test1> {
private constructor(x: Int) /* primary */ {
super/*Enum*/<Test1>()
/* <init>() */
@@ -8,14 +8,14 @@ FILE fqName:<root> fileName:/temporaryInEnumEntryInitializer.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-n> (): kotlin.Any? declared in <root>'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:n type:kotlin.Any? visibility:private [final,static]' type=kotlin.Any? origin=null
CLASS ENUM_CLASS name:En modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.En>]
CLASS ENUM_CLASS name:En modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.En>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.En
CONSTRUCTOR visibility:private <> (x:kotlin.String?) returnType:<root>.En [primary]
VALUE_PARAMETER name:x index:0 type:kotlin.String?
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.En
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:En modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.En>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:En modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.En>]'
PROPERTY name:x visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:x type:kotlin.String? visibility:private [final]
EXPRESSION_BODY
@@ -2,7 +2,7 @@ val n: Any?
field = null
get
open enum class En : Enum<En> {
enum class En : Enum<En> {
private constructor(x: String?) /* primary */ {
super/*Enum*/<En>()
/* <init>() */
@@ -29,3 +29,4 @@ open enum class En : Enum<En> {
get(): EnumEntries<En> /* Synthetic body for ENUM_ENTRIES */
}
@@ -38,7 +38,7 @@ FILE fqName:<root> fileName:/lambdaInEnumEntryConstructorCall.kt
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS ENUM_CLASS name:ConfigurationParameter modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.ConfigurationParameter>]
CLASS ENUM_CLASS name:ConfigurationParameter modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.ConfigurationParameter>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.ConfigurationParameter
ENUM_ENTRY name:BASE_URL
init: EXPRESSION_BODY
@@ -65,7 +65,7 @@ FILE fqName:<root> fileName:/lambdaInEnumEntryConstructorCall.kt
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.ConfigurationParameter
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:ConfigurationParameter modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.ConfigurationParameter>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:ConfigurationParameter modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.ConfigurationParameter>]'
FUN ENUM_CLASS_SPECIAL_MEMBER name:values visibility:public modality:FINAL <> () returnType:kotlin.Array<<root>.ConfigurationParameter>
SYNTHETIC_BODY kind=ENUM_VALUES
FUN ENUM_CLASS_SPECIAL_MEMBER name:valueOf visibility:public modality:FINAL <> (value:kotlin.String) returnType:<root>.ConfigurationParameter
@@ -12,7 +12,7 @@ class Wrapper {
}
open enum class ConfigurationParameter : Enum<ConfigurationParameter> {
enum class ConfigurationParameter : Enum<ConfigurationParameter> {
BASE_URL = ConfigurationParameter(apply = local fun <anonymous>(value: String, nc: Wrapper) {
println(message = "Base url updated from config parameters ".plus(other = nc.<get-baseUrl>()).plus(other = " -> ").plus(other = value))
nc.<set-baseUrl>(<set-?> = value)
@@ -1,5 +1,5 @@
FILE fqName:<root> fileName:/thisInEnumConstructor.kt
CLASS ENUM_CLASS name:EE modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.EE>]
CLASS ENUM_CLASS name:EE modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.EE>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.EE
CONSTRUCTOR visibility:private <> (myName:kotlin.String) returnType:<root>.EE [primary]
VALUE_PARAMETER name:myName index:0 type:kotlin.String
@@ -10,7 +10,7 @@ FILE fqName:<root> fileName:/thisInEnumConstructor.kt
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.EE
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:EE modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.EE>]'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:EE modality:FINAL visibility:public superTypes:[kotlin.Enum<<root>.EE>]'
PROPERTY name:myName visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:myName type:kotlin.String visibility:private [final]
EXPRESSION_BODY
@@ -1,4 +1,4 @@
open enum class EE : Enum<EE> {
enum class EE : Enum<EE> {
private constructor(myName: String = Companion.toString().lowercase()) /* primary */ {
super/*Enum*/<EE>()
/* <init>() */