[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"); 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 @Test
@TestMetadata("WhenTypeDisjunctions.kt") @TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception { public void testWhenTypeDisjunctions() throws Exception {
@@ -35846,6 +35846,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt"); 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 @Test
@TestMetadata("WhenTypeDisjunctions.kt") @TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception { public void testWhenTypeDisjunctions() throws Exception {
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
// FILE: JavaEnum.java // FILE: JavaEnum.java
public enum JavaEnum { public enum JavaEnum {
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
// FILE: JavaEnum.java // FILE: JavaEnum.java
public enum JavaEnum { public enum JavaEnum {
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
enum class Outer { enum class Outer {
FIRST, SECOND; FIRST, SECOND;
} }
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
enum class Some { enum class Some {
FIRST, FIRST,
SECOND; SECOND;
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
// FILE: first.kt // FILE: first.kt
package first package first
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
enum class Some { enum class Some {
FIRST, FIRST,
SECOND; SECOND;
@@ -1,3 +1,4 @@
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
package test package test
enum class Sample { enum class Sample {
@@ -35846,6 +35846,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt"); 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 @Test
@TestMetadata("WhenTypeDisjunctions.kt") @TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception { public void testWhenTypeDisjunctions() throws Exception {
@@ -35942,6 +35942,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/when/whenOnNothing.kt"); 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 @Test
@TestMetadata("WhenTypeDisjunctions.kt") @TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception { public void testWhenTypeDisjunctions() throws Exception {
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve 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.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.PrivateForInline 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.declarations.utils.isInner
import org.jetbrains.kotlin.fir.expressions.FirCallableReferenceAccess import org.jetbrains.kotlin.fir.expressions.FirCallableReferenceAccess
import org.jetbrains.kotlin.fir.expressions.FirWhenExpression 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.*
import org.jetbrains.kotlin.fir.resolve.calls.ImplicitExtensionReceiverValue import org.jetbrains.kotlin.fir.resolve.calls.ImplicitExtensionReceiverValue
import org.jetbrains.kotlin.fir.resolve.calls.ImplicitReceiverValue import org.jetbrains.kotlin.fir.resolve.calls.ImplicitReceiverValue
@@ -534,19 +536,28 @@ class BodyResolveContext(
inline fun <T> withWhenSubjectType( inline fun <T> withWhenSubjectType(
subjectType: ConeKotlinType?, subjectType: ConeKotlinType?,
sessionHolder: SessionHolder, sessionHolder: SessionHolder,
f: () -> T f: () -> T,
): T { ): T {
val session = sessionHolder.session val session = sessionHolder.session
val subjectClassSymbol = (subjectType as? ConeClassLikeType)
?.lookupTag?.toFirRegularClassSymbol(session)?.takeIf { it.fir.classKind == ClassKind.ENUM_CLASS } val withContextSensitiveResolution =
val whenSubjectImportingScope = subjectClassSymbol?.let { session.languageVersionSettings.supportsFeature(LanguageFeature.ContextSensitiveEnumResolutionInWhen)
FirWhenSubjectImportingScope(it.classId, session, sessionHolder.scopeSession)
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 { return try {
f() f()
} finally { } finally {
whenSubjectImportingScopes.removeLast() if (withContextSensitiveResolution) {
whenSubjectImportingScopes.removeLast()
}
} }
} }
+1
View File
@@ -1,6 +1,7 @@
// TARGET_BACKEND: JVM_IR // TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR // IGNORE_BACKEND_K1: JVM_IR
// WITH_STDLIB // WITH_STDLIB
// LANGUAGE: +ContextSensitiveEnumResolutionInWhen
enum class Rainbow { enum class Rainbow {
RED, 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"); 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 @Test
@TestMetadata("WhenTypeDisjunctions.kt") @TestMetadata("WhenTypeDisjunctions.kt")
public void testWhenTypeDisjunctions() throws Exception { public void testWhenTypeDisjunctions() throws Exception {
@@ -348,6 +348,7 @@ enum class LanguageFeature(
ForbidInferringTypeVariablesIntoEmptyIntersection(sinceVersion = null, kind = BUG_FIX), // KT-51221 ForbidInferringTypeVariablesIntoEmptyIntersection(sinceVersion = null, kind = BUG_FIX), // KT-51221
IntrinsicConstEvaluation(sinceVersion = null, kind = UNSTABLE_FEATURE), // KT-49303 IntrinsicConstEvaluation(sinceVersion = null, kind = UNSTABLE_FEATURE), // KT-49303
DisableCheckingChangedProgressionsResolve(sinceVersion = null, kind = OTHER), // KT-49276 DisableCheckingChangedProgressionsResolve(sinceVersion = null, kind = OTHER), // KT-49276
ContextSensitiveEnumResolutionInWhen(sinceVersion = null, kind = UNSTABLE_FEATURE), // KT-52774
; ;
init { init {