WHEN_ENUM_CAN_BE_NULL_IN_JAVA is no more reported after smart cast to not-null #KT-15201 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
b53ebd115b
commit
f64345634b
+10
-1
@@ -20,8 +20,10 @@ import org.jetbrains.kotlin.cfg.WhenChecker
|
||||
import org.jetbrains.kotlin.load.java.lazy.types.isMarkedNotNull
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtWhenExpression
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.AdditionalTypeChecker
|
||||
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
|
||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
@@ -33,8 +35,15 @@ class WhenByPlatformEnumChecker : AdditionalTypeChecker {
|
||||
override fun checkType(expression: KtExpression, expressionType: KotlinType, expressionTypeWithSmartCast: KotlinType, c: ResolutionContext<*>) {
|
||||
if (expression is KtWhenExpression && expression.elseExpression == null) {
|
||||
// Check for conditionally-exhaustive when on platform enums, see KT-6399
|
||||
val type = expression.subjectExpression?.let { c.trace.getType(it) } ?: return
|
||||
val subjectExpression = expression.subjectExpression ?: return
|
||||
val type = c.trace.getType(subjectExpression) ?: return
|
||||
if (type.isFlexible() && TypeUtils.isNullableType(type.asFlexibleType().upperBound) && !type.annotations.isMarkedNotNull()) {
|
||||
val dataFlowValue = DataFlowValueFactory.createDataFlowValue(subjectExpression, type, c)
|
||||
val dataFlowInfo = c.trace[BindingContext.EXPRESSION_TYPE_INFO, subjectExpression]?.dataFlowInfo
|
||||
if (dataFlowInfo != null && !dataFlowInfo.getStableNullability(dataFlowValue).canBeNull()) {
|
||||
return
|
||||
}
|
||||
|
||||
val enumClassDescriptor = WhenChecker.getClassDescriptorOfTypeIfEnum(type) ?: return
|
||||
val context = c.trace.bindingContext
|
||||
if (WhenChecker.getEnumMissingCases(expression, context, enumClassDescriptor).isEmpty()
|
||||
|
||||
@@ -27,6 +27,15 @@ fun platformType() = when (<!WHEN_ENUM_CAN_BE_NULL_IN_JAVA!>J.foo()<!>) {
|
||||
E.B -> 8
|
||||
}
|
||||
|
||||
fun platformTypeSmartCast(): Int {
|
||||
val e = J.foo()
|
||||
if (e == null) return -1
|
||||
return when (<!DEBUG_INFO_SMARTCAST!>e<!>) {
|
||||
E.A -> 1
|
||||
E.B -> 2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// FILE: J.java
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package
|
||||
|
||||
public fun nullableNothing(): kotlin.Nothing?
|
||||
public fun platformType(): kotlin.Int
|
||||
public fun platformTypeSmartCast(): kotlin.Int
|
||||
public fun test(/*0*/ e: E?): kotlin.Int
|
||||
public fun withNull(/*0*/ e: E?): kotlin.Int?
|
||||
public fun withNullableNothing(/*0*/ e: E?): kotlin.Int?
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliases
|
||||
import org.jetbrains.kotlin.utils.ifEmpty
|
||||
|
||||
class SpecifyTypeExplicitlyIntention :
|
||||
|
||||
Reference in New Issue
Block a user