FIR: Fix inference for case of non-nullable RHS of elvis

Some existing tests start failing after previous commits adding @Exact
attribute to `?:`

They have a form:
var x: String? = nullable()

if (x == null) {
   x = nullable() ?: "" // considering @Exact the whole elvis is inferred to nullable from expect type
}

x.length // should be smart cast
This commit is contained in:
Denis.Zharkov
2021-11-15 16:16:19 +03:00
committed by teamcityserver
parent 883b18a0c6
commit 3ec7866ead
2 changed files with 15 additions and 1 deletions
@@ -14,7 +14,7 @@ FILE: ifElvisReturn.kt
R|<local>/b|
}
else -> {
R|/materialize|<R|kotlin/Nothing|>() ?: ^foo Unit
R|/materialize|<R|B|>() ?: ^foo Unit
}
}
@@ -243,6 +243,20 @@ class FirControlFlowStatementsResolveTransformer(transformer: FirBodyResolveTran
}
}
session.typeContext.run {
if (result.typeRef.coneTypeSafe<ConeKotlinType>()?.isNullableType() == true
&& result.rhs.typeRef.coneTypeSafe<ConeKotlinType>()?.isNullableType() == false
) {
// Sometimes return type for special call for elvis operator might be nullable,
// but result is not nullable if the right type is not nullable
result.replaceTypeRef(
result.typeRef.withReplacedConeType(result.typeRef.coneType.makeConeTypeDefinitelyNotNullOrNotNull(session.typeContext))
)
}
}
dataFlowAnalyzer.exitElvis(elvisExpression)
return result
}