[NI] Don't avoid Nothing-like constraints if Nothing was in initial type

#KT-32081 Fixed
 #KT-32951 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2019-06-20 12:58:32 +03:00
parent 4b2175c8f8
commit f3e4c9cdb3
9 changed files with 119 additions and 2 deletions
@@ -158,7 +158,10 @@ class ConstraintIncorporator(
) {
if (targetVariable in getNestedTypeVariables(newConstraint)) return
if (!containsConstrainingTypeWithoutProjection(newConstraint, otherConstraint)) return
if (trivialConstraintTypeInferenceOracle.isGeneratedConstraintTrivial(otherConstraint, newConstraint, isSubtype)) return
if (trivialConstraintTypeInferenceOracle.isGeneratedConstraintTrivial(
baseConstraint, otherConstraint, newConstraint, isSubtype
)
) return
val derivedFrom = (baseConstraint.derivedFrom + otherConstraint.derivedFrom).toMutableSet()
if (otherVariable in derivedFrom) return
@@ -35,6 +35,7 @@ class TrivialConstraintTypeInferenceOracle(context: TypeSystemInferenceExtension
// but can change result of the constraint system.
// Therefore, here we avoid adding such trivial constraints to have stable constraint system
fun isGeneratedConstraintTrivial(
baseConstraint: Constraint,
otherConstraint: Constraint,
generatedConstraintType: KotlinTypeMarker,
isSubtype: Boolean
@@ -42,8 +43,9 @@ class TrivialConstraintTypeInferenceOracle(context: TypeSystemInferenceExtension
if (isSubtype && generatedConstraintType.isNothing()) return true
if (!isSubtype && generatedConstraintType.isNullableAny()) return true
// If type that will be used to generate new constraint already contains `Nothing(?)`,
// If types from constraints that will be used to generate new constraint already contains `Nothing(?)`,
// then we can't decide that resulting constraint will be useless
if (baseConstraint.type.contains { it.isNothingOrNullableNothing() }) return false
if (otherConstraint.type.contains { it.isNothingOrNullableNothing() }) return false
// It's important to preserve constraints with nullable Nothing: `Nothing? <: T` (see implicitNothingConstraintFromReturn.kt test)