[FIR] Fix creating scopes for enum entry initializers

Before we analyzed initializers of enum entry with scope for constructor
  in implict type mode, so scope was untouchable. Now we analyze them
  in body resolve phase, so previously we add value parameters to
  constructors scope, which mess up scope of enum entry initializer
This commit is contained in:
Dmitriy Novozhilov
2021-07-04 13:27:12 +03:00
parent 1593c4859d
commit 6e1fce6b8b
2 changed files with 15 additions and 1 deletions
@@ -152,6 +152,7 @@ enum class FirTowerDataMode {
NESTED_CLASS,
COMPANION_OBJECT,
CONSTRUCTOR_HEADER,
ENUM_ENTRY,
SPECIAL,
}
@@ -160,6 +161,7 @@ class FirTowerDataContextsForClassParts(
forNestedClasses: FirTowerDataContext? = null,
forCompanionObject: FirTowerDataContext? = null,
forConstructorHeaders: FirTowerDataContext? = null,
forEnumEntries: FirTowerDataContext? = null,
val primaryConstructorPureParametersScope: FirLocalScope? = null,
val primaryConstructorAllParametersScope: FirLocalScope? = null,
) {
@@ -170,6 +172,7 @@ class FirTowerDataContextsForClassParts(
modeMap[NESTED_CLASS] = forNestedClasses
modeMap[COMPANION_OBJECT] = forCompanionObject
modeMap[CONSTRUCTOR_HEADER] = forConstructorHeaders
modeMap[ENUM_ENTRY] = forEnumEntries
}
var mode: FirTowerDataMode = MEMBER_DECLARATION
@@ -418,6 +418,16 @@ class BodyResolveContext(
val scopeForConstructorHeader =
staticsAndCompanion.addNonLocalScopeIfNotNull(typeParameterScope)
/*
* Scope for enum entries is equal to initial scope for constructor header
*
* The only difference that we add value parameters to local scope for constructors
* and should not do this for enum entries
*/
@Suppress("UnnecessaryVariable")
val scopeForEnumEntries = scopeForConstructorHeader
val newTowerDataContextForStaticNestedClasses =
if ((owner as? FirRegularClass)?.classKind?.isSingleton == true)
forMembersResolution
@@ -437,6 +447,7 @@ class BodyResolveContext(
newTowerDataContextForStaticNestedClasses,
statics,
scopeForConstructorHeader,
scopeForEnumEntries,
primaryConstructorPureParametersScope,
primaryConstructorAllParametersScope
)
@@ -569,7 +580,7 @@ class BodyResolveContext(
@OptIn(PrivateForInline::class)
inline fun <T> forEnumEntry(
f: () -> T
): T = withTowerDataMode(FirTowerDataMode.CONSTRUCTOR_HEADER, f)
): T = withTowerDataMode(FirTowerDataMode.ENUM_ENTRY, f)
@OptIn(PrivateForInline::class)
inline fun <T> withAnonymousInitializer(