From 09acea5548cf5bf32d41d03e8bca6c735a2cfb28 Mon Sep 17 00:00:00 2001 From: Juan Chen Date: Tue, 6 Oct 2020 03:21:06 +0000 Subject: [PATCH] [FIR]: Set proper classId to enum entries FirVisibilityChecker::isVisible checked if a private declaration can be accessed at a use site by matching class ids of the private declaration's owner with the use site's containing class declarations. When the private declaration is defined in an enum entry and used in the same entry, its owner class id has the enum entry name, but the use site is in an FirAnonymousObject, which has "anonymous" as the class id. This causes visibility check to fail. This PR fixes owner class ids of declarations defined in enum entries to be the same as normal anonymous objects. --- .../converter/DeclarationsConverter.kt | 7 ++++++- .../kotlin/fir/builder/RawFirBuilder.kt | 19 +++++++++++-------- .../codegen/box/enum/enumEntryMembers.kt | 6 ++++++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index bf545b309f2..4515acc454a 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -643,7 +643,12 @@ class DeclarationsConverter( ) superTypeRefs += enumClassWrapper.delegatedSuperTypeRef convertPrimaryConstructor(null, null, enumClassWrapper, null)?.let { declarations += it.firConstructor } - classBodyNode?.also { declarations += convertClassBody(it, enumClassWrapper) } + classBodyNode?.also { + // Use ANONYMOUS_OBJECT_NAME for the owner class id of enum entry declarations + withChildClassName(ANONYMOUS_OBJECT_NAME, isLocal = true) { + declarations += convertClassBody(it, enumClassWrapper) + } + } } } } diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index 7fd8158136e..fb3a17049bb 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -761,14 +761,17 @@ class RawFirBuilder( owner = ktEnumEntry, typeParameters ) - for (declaration in ktEnumEntry.declarations) { - declarations += declaration.toFirDeclaration( - correctedEnumSelfTypeRef, - delegatedSelfType = delegatedEntrySelfType, - ktEnumEntry, - ownerClassBuilder = this, - ownerTypeParameters = emptyList() - ) + // Use ANONYMOUS_OBJECT_NAME for the owner class id for enum entry declarations (see KT-42351) + withChildClassName(ANONYMOUS_OBJECT_NAME, isLocal = true) { + for (declaration in ktEnumEntry.declarations) { + declarations += declaration.toFirDeclaration( + correctedEnumSelfTypeRef, + delegatedSelfType = delegatedEntrySelfType, + ktEnumEntry, + ownerClassBuilder = this, + ownerTypeParameters = emptyList() + ) + } } } } diff --git a/compiler/testData/codegen/box/enum/enumEntryMembers.kt b/compiler/testData/codegen/box/enum/enumEntryMembers.kt index 919960c4463..0747281c05e 100644 --- a/compiler/testData/codegen/box/enum/enumEntryMembers.kt +++ b/compiler/testData/codegen/box/enum/enumEntryMembers.kt @@ -3,6 +3,10 @@ // FILE: lib.kt enum class Foo { FOO() { + // Test for KT-42351 + private fun privateBar() = "bar" + override fun bar(): String = privateBar() + override fun foo() = "foo" override var xxx: String @@ -12,6 +16,7 @@ enum class Foo { }; abstract fun foo(): String + abstract fun bar(): String abstract var xxx: String } @@ -25,6 +30,7 @@ fun box(): String { assertEquals(Foo.FOO.xxx, "xxx") assertEquals(Foo.FOO.toString(), "FOO") assertEquals(Foo.valueOf("FOO").toString(), "FOO") + assertEquals(Foo.FOO.bar(), "bar") return "OK" }