[FIR] Support when exhaustiveness for java enum
The problem appear because for java enum its entries was represented by FirJavaField. To fix this FirEnumEntry was used #KT-39621 Fixed
This commit is contained in:
+3
-6
@@ -11,8 +11,7 @@ public/*package*/ open class A : R|kotlin/Any| {
|
||||
|
||||
}
|
||||
public final enum class Mixed : R|kotlin/Enum<ft<Mixed, Mixed?>!>| {
|
||||
public final static field NOT_ENTRY_EITHER: R|Mixed|
|
||||
|
||||
public final static enum entry NOT_ENTRY_EITHER: R|Mixed|
|
||||
public final static fun values(): R|kotlin/Array<Mixed>| {
|
||||
}
|
||||
|
||||
@@ -21,10 +20,8 @@ public final enum class Mixed : R|kotlin/Enum<ft<Mixed, Mixed?>!>| {
|
||||
|
||||
}
|
||||
public final enum class Signs : R|kotlin/Enum<ft<Signs, Signs?>!>| {
|
||||
public final static field HELLO: R|Signs|
|
||||
|
||||
public final static field WORLD: R|Signs|
|
||||
|
||||
public final static enum entry HELLO: R|Signs|
|
||||
public final static enum entry WORLD: R|Signs|
|
||||
public final static field X: R|ft<Signs, Signs?>!|
|
||||
|
||||
public final static fun values(): R|kotlin/Array<Signs>| {
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// FILE: JavaEnum.java
|
||||
public enum JavaEnum {
|
||||
A, B, C;
|
||||
|
||||
public int i = 0;
|
||||
}
|
||||
|
||||
// FILE: main.kt
|
||||
fun test_1(e: JavaEnum) {
|
||||
val a = when (e) {
|
||||
JavaEnum.A -> 1
|
||||
JavaEnum.B -> 2
|
||||
}.<!UNRESOLVED_REFERENCE!>plus<!>(0)
|
||||
|
||||
val b = when (e) {
|
||||
JavaEnum.A -> 1
|
||||
JavaEnum.B -> 2
|
||||
is String -> 3
|
||||
}.<!UNRESOLVED_REFERENCE!>plus<!>(0)
|
||||
|
||||
val c = when (e) {
|
||||
JavaEnum.A -> 1
|
||||
JavaEnum.B -> 2
|
||||
JavaEnum.C -> 3
|
||||
}.plus(0)
|
||||
|
||||
val d = when (e) {
|
||||
JavaEnum.A -> 1
|
||||
else -> 2
|
||||
}.plus(0)
|
||||
}
|
||||
|
||||
fun test_2(e: JavaEnum?) {
|
||||
val a = when (e) {
|
||||
JavaEnum.A -> 1
|
||||
JavaEnum.B -> 2
|
||||
JavaEnum.C -> 3
|
||||
}.<!UNRESOLVED_REFERENCE!>plus<!>(0)
|
||||
|
||||
val a = when (e) {
|
||||
JavaEnum.A -> 1
|
||||
JavaEnum.B -> 2
|
||||
JavaEnum.C -> 3
|
||||
null -> 4
|
||||
}.plus(0)
|
||||
|
||||
val a = when (e) {
|
||||
JavaEnum.A -> 1
|
||||
JavaEnum.B -> 2
|
||||
JavaEnum.C -> 3
|
||||
else -> 4
|
||||
}.plus(0)
|
||||
}
|
||||
|
||||
fun test_3(e: JavaEnum) {
|
||||
val a = when (e) {
|
||||
JavaEnum.A, JavaEnum.B -> 1
|
||||
JavaEnum.C -> 2
|
||||
}.plus(0)
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
FILE: main.kt
|
||||
public final fun test_1(e: R|JavaEnum|): R|kotlin/Unit| {
|
||||
lval a: <ERROR TYPE REF: Unresolved name: plus> = when (R|<local>/e|) {
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.A|) -> {
|
||||
Int(1)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.B|) -> {
|
||||
Int(2)
|
||||
}
|
||||
}
|
||||
.<Unresolved name: plus>#(Int(0))
|
||||
lval b: <ERROR TYPE REF: Unresolved name: plus> = when (R|<local>/e|) {
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.A|) -> {
|
||||
Int(1)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.B|) -> {
|
||||
Int(2)
|
||||
}
|
||||
($subj$ is R|kotlin/String|) -> {
|
||||
Int(3)
|
||||
}
|
||||
}
|
||||
.<Unresolved name: plus>#(Int(0))
|
||||
lval c: R|kotlin/Int| = when (R|<local>/e|) {
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.A|) -> {
|
||||
Int(1)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.B|) -> {
|
||||
Int(2)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.C|) -> {
|
||||
Int(3)
|
||||
}
|
||||
}
|
||||
.R|kotlin/Int.plus|(Int(0))
|
||||
lval d: R|kotlin/Int| = when (R|<local>/e|) {
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.A|) -> {
|
||||
Int(1)
|
||||
}
|
||||
else -> {
|
||||
Int(2)
|
||||
}
|
||||
}
|
||||
.R|kotlin/Int.plus|(Int(0))
|
||||
}
|
||||
public final fun test_2(e: R|JavaEnum?|): R|kotlin/Unit| {
|
||||
lval a: <ERROR TYPE REF: Unresolved name: plus> = when (R|<local>/e|) {
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.A|) -> {
|
||||
Int(1)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.B|) -> {
|
||||
Int(2)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.C|) -> {
|
||||
Int(3)
|
||||
}
|
||||
}
|
||||
.<Unresolved name: plus>#(Int(0))
|
||||
lval a: R|kotlin/Int| = when (R|<local>/e|) {
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.A|) -> {
|
||||
Int(1)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.B|) -> {
|
||||
Int(2)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.C|) -> {
|
||||
Int(3)
|
||||
}
|
||||
==($subj$, Null(null)) -> {
|
||||
Int(4)
|
||||
}
|
||||
}
|
||||
.R|kotlin/Int.plus|(Int(0))
|
||||
lval a: R|kotlin/Int| = when (R|<local>/e|) {
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.A|) -> {
|
||||
Int(1)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.B|) -> {
|
||||
Int(2)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.C|) -> {
|
||||
Int(3)
|
||||
}
|
||||
else -> {
|
||||
Int(4)
|
||||
}
|
||||
}
|
||||
.R|kotlin/Int.plus|(Int(0))
|
||||
}
|
||||
public final fun test_3(e: R|JavaEnum|): R|kotlin/Unit| {
|
||||
lval a: R|kotlin/Int| = when (R|<local>/e|) {
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.A|) || ==($subj$, Q|JavaEnum|.R|/JavaEnum.B|) -> {
|
||||
Int(1)
|
||||
}
|
||||
==($subj$, Q|JavaEnum|.R|/JavaEnum.C|) -> {
|
||||
Int(2)
|
||||
}
|
||||
}
|
||||
.R|kotlin/Int.plus|(Int(0))
|
||||
}
|
||||
Generated
+5
@@ -133,6 +133,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/exhaustiveness_enum.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("exhaustiveness_enumJava.kt")
|
||||
public void testExhaustiveness_enumJava() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/exhaustiveness_enumJava.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("exhaustiveness_sealedClass.kt")
|
||||
public void testExhaustiveness_sealedClass() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/exhaustiveness_sealedClass.kt");
|
||||
|
||||
+5
@@ -133,6 +133,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/exhaustiveness_enum.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("exhaustiveness_enumJava.kt")
|
||||
public void testExhaustiveness_enumJava() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/exhaustiveness_enumJava.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("exhaustiveness_sealedClass.kt")
|
||||
public void testExhaustiveness_sealedClass() throws Exception {
|
||||
runTest("compiler/fir/analysis-tests/testData/resolve/exhaustiveness_sealedClass.kt");
|
||||
|
||||
+16
-24
@@ -9,12 +9,14 @@ import org.jetbrains.kotlin.fir.FirRenderer
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.FirCallableMemberDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirEnumEntry
|
||||
import org.jetbrains.kotlin.fir.java.declarations.FirJavaClass
|
||||
import org.jetbrains.kotlin.fir.java.declarations.FirJavaConstructor
|
||||
import org.jetbrains.kotlin.fir.java.declarations.FirJavaField
|
||||
import org.jetbrains.kotlin.fir.java.declarations.FirJavaMethod
|
||||
import org.jetbrains.kotlin.fir.resolve.ScopeSession
|
||||
import org.jetbrains.kotlin.fir.resolve.buildUseSiteMemberScope
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
|
||||
|
||||
fun renderJavaClass(renderer: FirRenderer, javaClass: FirJavaClass, session: FirSession) {
|
||||
val memberScope = javaClass.buildUseSiteMemberScope(session, ScopeSession())
|
||||
@@ -28,6 +30,16 @@ fun renderJavaClass(renderer: FirRenderer, javaClass: FirJavaClass, session: Fir
|
||||
renderer.renderSupertypes(javaClass)
|
||||
renderer.renderInBraces {
|
||||
val renderedDeclarations = mutableListOf<FirDeclaration>()
|
||||
|
||||
fun renderAndCache(symbol: FirCallableSymbol<*>) {
|
||||
val enhanced = symbol.fir
|
||||
if (enhanced !in renderedDeclarations) {
|
||||
enhanced.accept(renderer, null)
|
||||
renderer.newLine()
|
||||
renderedDeclarations += enhanced
|
||||
}
|
||||
}
|
||||
|
||||
for (declaration in javaClass.declarations) {
|
||||
if (declaration in renderedDeclarations) continue
|
||||
|
||||
@@ -38,30 +50,10 @@ fun renderJavaClass(renderer: FirRenderer, javaClass: FirJavaClass, session: Fir
|
||||
memberScope
|
||||
|
||||
when (declaration) {
|
||||
is FirJavaConstructor -> scopeToUse!!.processDeclaredConstructors { symbol ->
|
||||
val enhanced = symbol.fir
|
||||
if (enhanced !in renderedDeclarations) {
|
||||
enhanced.accept(renderer, null)
|
||||
renderer.newLine()
|
||||
renderedDeclarations += enhanced
|
||||
}
|
||||
}
|
||||
is FirJavaMethod -> scopeToUse!!.processFunctionsByName(declaration.name) { symbol ->
|
||||
val enhanced = symbol.fir
|
||||
if (enhanced !in renderedDeclarations) {
|
||||
enhanced.accept(renderer, null)
|
||||
renderer.newLine()
|
||||
renderedDeclarations += enhanced
|
||||
}
|
||||
}
|
||||
is FirJavaField -> scopeToUse!!.processPropertiesByName(declaration.name) { symbol ->
|
||||
val enhanced = symbol.fir
|
||||
if (enhanced !in renderedDeclarations) {
|
||||
enhanced.accept(renderer, null)
|
||||
renderer.newLine()
|
||||
renderedDeclarations += enhanced
|
||||
}
|
||||
}
|
||||
is FirJavaConstructor -> scopeToUse!!.processDeclaredConstructors(::renderAndCache)
|
||||
is FirJavaMethod -> scopeToUse!!.processFunctionsByName(declaration.name, ::renderAndCache)
|
||||
is FirJavaField -> scopeToUse!!.processPropertiesByName(declaration.name, ::renderAndCache)
|
||||
is FirEnumEntry -> scopeToUse!!.processPropertiesByName(declaration.name, ::renderAndCache)
|
||||
else -> {
|
||||
declaration.accept(renderer, null)
|
||||
renderer.newLine()
|
||||
|
||||
@@ -11,16 +11,10 @@ import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.Visibility
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
|
||||
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
|
||||
import org.jetbrains.kotlin.fir.declarations.FirTypeParameter
|
||||
import org.jetbrains.kotlin.fir.declarations.addDefaultBoundIfNecessary
|
||||
import org.jetbrains.kotlin.fir.declarations.builder.FirTypeParameterBuilder
|
||||
import org.jetbrains.kotlin.fir.declarations.builder.buildConstructedClassTypeParameterRef
|
||||
import org.jetbrains.kotlin.fir.declarations.builder.buildOuterClassTypeParameterRef
|
||||
import org.jetbrains.kotlin.fir.generateValueOfFunction
|
||||
import org.jetbrains.kotlin.fir.generateValuesFunction
|
||||
import org.jetbrains.kotlin.fir.*
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.builder.*
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
|
||||
import org.jetbrains.kotlin.fir.java.declarations.*
|
||||
import org.jetbrains.kotlin.fir.resolve.constructType
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.AbstractFirSymbolProvider
|
||||
@@ -30,7 +24,6 @@ import org.jetbrains.kotlin.fir.scopes.impl.lazyNestedClassifierScope
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.nestedClassifierScope
|
||||
import org.jetbrains.kotlin.fir.symbols.CallableId
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.fir.toFirPsiSourceElement
|
||||
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl
|
||||
import org.jetbrains.kotlin.load.java.JavaClassFinder
|
||||
@@ -191,22 +184,39 @@ class JavaSymbolProvider(
|
||||
for (javaField in javaClass.fields) {
|
||||
val fieldName = javaField.name
|
||||
val fieldId = CallableId(classId.packageFqName, classId.relativeClassName, fieldName)
|
||||
val fieldSymbol = FirFieldSymbol(fieldId)
|
||||
val returnType = javaField.type
|
||||
val firJavaField = buildJavaField {
|
||||
source = (javaField as? JavaElementImpl<*>)?.psi?.toFirPsiSourceElement()
|
||||
session = this@JavaSymbolProvider.session
|
||||
symbol = fieldSymbol
|
||||
name = fieldName
|
||||
visibility = javaField.visibility
|
||||
modality = javaField.modality
|
||||
returnTypeRef = returnType.toFirJavaTypeRef(this@JavaSymbolProvider.session, javaTypeParameterStack)
|
||||
isVar = !javaField.isFinal
|
||||
isStatic = javaField.isStatic
|
||||
isEnumEntry = javaField.isEnumEntry
|
||||
addAnnotationsFrom(this@JavaSymbolProvider.session, javaField, javaTypeParameterStack)
|
||||
val firJavaDeclaration = when {
|
||||
javaField.isEnumEntry -> buildEnumEntry {
|
||||
source = (javaField as? JavaElementImpl<*>)?.psi?.toFirPsiSourceElement()
|
||||
session = this@JavaSymbolProvider.session
|
||||
symbol = FirVariableSymbol(fieldId)
|
||||
name = fieldName
|
||||
status = FirDeclarationStatusImpl(javaField.visibility, javaField.modality).apply {
|
||||
isStatic = javaField.isStatic
|
||||
isExpect = false
|
||||
isActual = false
|
||||
isOverride = false
|
||||
}
|
||||
returnTypeRef = returnType.toFirJavaTypeRef(this@JavaSymbolProvider.session, javaTypeParameterStack)
|
||||
resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES
|
||||
origin = FirDeclarationOrigin.Java
|
||||
addAnnotationsFrom(this@JavaSymbolProvider.session, javaField, javaTypeParameterStack)
|
||||
}
|
||||
else -> buildJavaField {
|
||||
source = (javaField as? JavaElementImpl<*>)?.psi?.toFirPsiSourceElement()
|
||||
session = this@JavaSymbolProvider.session
|
||||
symbol = FirFieldSymbol(fieldId)
|
||||
name = fieldName
|
||||
visibility = javaField.visibility
|
||||
modality = javaField.modality
|
||||
returnTypeRef = returnType.toFirJavaTypeRef(this@JavaSymbolProvider.session, javaTypeParameterStack)
|
||||
isVar = !javaField.isFinal
|
||||
isStatic = javaField.isStatic
|
||||
isEnumEntry = javaField.isEnumEntry
|
||||
addAnnotationsFrom(this@JavaSymbolProvider.session, javaField, javaTypeParameterStack)
|
||||
}
|
||||
}
|
||||
declarations += firJavaField
|
||||
declarations += firJavaDeclaration
|
||||
}
|
||||
for (javaMethod in javaClass.methods) {
|
||||
val methodName = javaMethod.name
|
||||
|
||||
+8
-7
@@ -69,21 +69,22 @@ class FirSignatureEnhancement(
|
||||
name: Name
|
||||
): FirVariableSymbol<*> {
|
||||
when (val firElement = original.fir) {
|
||||
is FirField -> {
|
||||
is FirEnumEntry -> {
|
||||
if (firElement.returnTypeRef !is FirJavaTypeRef) return original
|
||||
val memberContext = context.copyWithNewDefaultTypeQualifiers(typeQualifierResolver, jsr305State, firElement.annotations)
|
||||
val isEnumEntry = (firElement as? FirJavaField)?.isEnumEntry ?: false
|
||||
|
||||
val predefinedInfo = if (isEnumEntry) {
|
||||
val predefinedInfo =
|
||||
PredefinedFunctionEnhancementInfo(
|
||||
TypeEnhancementInfo(0 to JavaTypeQualifiers(NullabilityQualifier.NOT_NULL, null, false)),
|
||||
emptyList()
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
val newReturnTypeRef = enhanceReturnType(firElement, emptyList(), memberContext, predefinedInfo)
|
||||
return firElement.symbol.apply { this.fir.replaceReturnTypeRef(newReturnTypeRef) }
|
||||
}
|
||||
is FirField -> {
|
||||
if (firElement.returnTypeRef !is FirJavaTypeRef) return original
|
||||
val memberContext = context.copyWithNewDefaultTypeQualifiers(typeQualifierResolver, jsr305State, firElement.annotations)
|
||||
val newReturnTypeRef = enhanceReturnType(firElement, emptyList(), memberContext, null)
|
||||
|
||||
val symbol = FirFieldSymbol(original.callableId)
|
||||
buildJavaField {
|
||||
|
||||
@@ -342,7 +342,6 @@ class FirRenderer(builder: StringBuilder, private val mode: RenderMode = RenderM
|
||||
}
|
||||
|
||||
override fun visitEnumEntry(enumEntry: FirEnumEntry) {
|
||||
enumEntry.annotations.renderAnnotations()
|
||||
visitCallableDeclaration(enumEntry)
|
||||
enumEntry.initializer?.let {
|
||||
print(" = ")
|
||||
|
||||
Reference in New Issue
Block a user