[FIR] Introduce a feature flag for context-sensitive enum resolution

The feature was previously enabled unconditionally in K2 which
triggered a bug when an enum has an entry with the same name as itself.

#KT-58897 Fixed
#KT-52774
This commit is contained in:
Kirill Rakhman
2023-05-26 16:55:35 +02:00
committed by Space Team
parent 4b643480d6
commit cf2ef443f4
16 changed files with 69 additions and 7 deletions
@@ -35846,6 +35846,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("whenOverEnumWithSameNameAsEntry.kt")
public void testWhenOverEnumWithSameNameAsEntry() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOverEnumWithSameNameAsEntry.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
@@ -35846,6 +35846,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("whenOverEnumWithSameNameAsEntry.kt")
public void testWhenOverEnumWithSameNameAsEntry() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOverEnumWithSameNameAsEntry.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
// FILE: JavaEnum.java
public enum JavaEnum {
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
// FILE: JavaEnum.java
public enum JavaEnum {
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
enum class Outer {
FIRST, SECOND;
}
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
enum class Some {
FIRST,
SECOND;
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
// FILE: first.kt
package first
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
enum class Some {
FIRST,
SECOND;
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
package test
enum class Sample {
@@ -35846,6 +35846,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("whenOverEnumWithSameNameAsEntry.kt")
public void testWhenOverEnumWithSameNameAsEntry() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOverEnumWithSameNameAsEntry.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
@@ -35942,6 +35942,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("whenOverEnumWithSameNameAsEntry.kt")
public void testWhenOverEnumWithSameNameAsEntry() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOverEnumWithSameNameAsEntry.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.PrivateForInline
@@ -15,6 +16,7 @@ import org.jetbrains.kotlin.fir.declarations.utils.isCompanion
import org.jetbrains.kotlin.fir.declarations.utils.isInner
import org.jetbrains.kotlin.fir.expressions.FirCallableReferenceAccess
import org.jetbrains.kotlin.fir.expressions.FirWhenExpression
import org.jetbrains.kotlin.fir.languageVersionSettings
import org.jetbrains.kotlin.fir.resolve.*
import org.jetbrains.kotlin.fir.resolve.calls.ImplicitExtensionReceiverValue
import org.jetbrains.kotlin.fir.resolve.calls.ImplicitReceiverValue
@@ -534,19 +536,28 @@ class BodyResolveContext(
inline fun <T> withWhenSubjectType(
subjectType: ConeKotlinType?,
sessionHolder: SessionHolder,
f: () -> T
f: () -> T,
): T {
val session = sessionHolder.session
val subjectClassSymbol = (subjectType as? ConeClassLikeType)
?.lookupTag?.toFirRegularClassSymbol(session)?.takeIf { it.fir.classKind == ClassKind.ENUM_CLASS }
val whenSubjectImportingScope = subjectClassSymbol?.let {
FirWhenSubjectImportingScope(it.classId, session, sessionHolder.scopeSession)
val withContextSensitiveResolution =
session.languageVersionSettings.supportsFeature(LanguageFeature.ContextSensitiveEnumResolutionInWhen)
if (withContextSensitiveResolution) {
val subjectClassSymbol = (subjectType as? ConeClassLikeType)
?.lookupTag?.toFirRegularClassSymbol(session)?.takeIf { it.fir.classKind == ClassKind.ENUM_CLASS }
val whenSubjectImportingScope = subjectClassSymbol?.let {
FirWhenSubjectImportingScope(it.classId, session, sessionHolder.scopeSession)
}
whenSubjectImportingScopes.add(whenSubjectImportingScope)
}
whenSubjectImportingScopes.add(whenSubjectImportingScope)
return try {
f()
} finally {
whenSubjectImportingScopes.removeLast()
if (withContextSensitiveResolution) {
whenSubjectImportingScopes.removeLast()
}
}
}
+1
View File
@@ -1,6 +1,7 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// WITH_STDLIB
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
enum class Rainbow {
RED,
@@ -0,0 +1,12 @@
// FIR_IDENTICAL
// KT-58897
enum class A {
A,
B,
}
fun test2(a: A) = when (a) {
A.A -> "A"
A.B -> "B"
}
@@ -36744,6 +36744,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt");
}
@Test
@TestMetadata("whenOverEnumWithSameNameAsEntry.kt")
public void testWhenOverEnumWithSameNameAsEntry() throws Exception {
runTest("compiler/testData/diagnostics/tests/when/whenOverEnumWithSameNameAsEntry.kt");
}
@Test
@TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception {
@@ -348,6 +348,7 @@ enum class LanguageFeature(
ForbidInferringTypeVariablesIntoEmptyIntersection(sinceVersion = null, kind = BUG_FIX), // KT-51221
IntrinsicConstEvaluation(sinceVersion = null, kind = UNSTABLE_FEATURE), // KT-49303
DisableCheckingChangedProgressionsResolve(sinceVersion = null, kind = OTHER), // KT-49276
ContextSensitiveEnumResolutionInWhen(sinceVersion = null, kind = UNSTABLE_FEATURE), // KT-52774
;
init {