Support definitely not null stub types and proper subtyping on them

This commit is contained in:
Victor Petukhov
2021-05-21 14:42:29 +03:00
parent 0f317b01b4
commit 0c427555cf
10 changed files with 86 additions and 49 deletions
@@ -387,6 +387,10 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
return this is ConeStubType // TODO: distinguish stub types for builder inference and for subtyping
}
override fun SimpleTypeMarker.isStubTypeForBuilderInference(): Boolean {
return this is ConeStubType // TODO: distinguish stub types for builder inference and for subtyping
}
override fun intersectTypes(types: List<SimpleTypeMarker>): SimpleTypeMarker {
@Suppress("UNCHECKED_CAST")
return ConeTypeIntersector.intersectTypes(this as ConeInferenceContext, types as List<ConeKotlinType>) as SimpleTypeMarker
@@ -53,6 +53,8 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun SimpleTypeMarker.isStubTypeForVariableInSubtyping() = false
override fun SimpleTypeMarker.isStubTypeForBuilderInference() = false
override fun FlexibleTypeMarker.asDynamicType() = this as? IrDynamicType
override fun FlexibleTypeMarker.asRawType(): RawTypeMarker? = null
@@ -97,6 +97,10 @@ object NewCommonSuperTypeCalculator {
commonSuperType
}
private fun TypeSystemCommonSuperTypesContext.isCapturedStubTypeForVariableInSubtyping(type: SimpleTypeMarker) =
type.asCapturedType()?.typeConstructor()?.projection()?.takeUnless { it.isStarProjection() }
?.getType()?.asSimpleType()?.isStubTypeForVariableInSubtyping() == true
private fun TypeSystemCommonSuperTypesContext.refineNullabilityForUndefinedNullability(
types: List<SimpleTypeMarker>,
commonSuperType: SimpleTypeMarker
@@ -164,20 +168,27 @@ object NewCommonSuperTypeCalculator {
): SimpleTypeMarker {
if (types.size == 1) return types.single()
val nonTypeVariables = types.filter { !isTypeVariable(it) }
val nonTypeVariables = types.filter { !it.isStubTypeForVariableInSubtyping() && !isCapturedStubTypeForVariableInSubtyping(it) }
if (nonTypeVariables.size == 1) return nonTypeVariables.single()
assert(nonTypeVariables.isNotEmpty()) {
"There should be at least one non-stub type to compute common supertype but there are: $types"
}
val stubTypeVariables = types.filter { isStubTypeVariable(it) }
val nonStubTypeVariables = types.filter { !isStubTypeVariable(it) }
val nonStubTypeVariables = types.filter { !it.isStubType() }
val areAllNonStubTypesNothing = nonStubTypeVariables.isNotEmpty() && nonStubTypeVariables.all { it.isNothing() }
if (nonStubTypeVariables.size == 1 && !areAllNonStubTypesNothing) return nonStubTypeVariables.single()
if (nonStubTypeVariables.isEmpty() || areAllNonStubTypesNothing) {
return uniquify(stubTypeVariables, contextStubTypesNotEqual).singleOrNull() ?: nullableAnyType()
val stubTypeVariables = types.filter { it.isStubType() }
val uniqueStubTypes =
stubTypeVariables.distinctBy { it.asDefinitelyNotNullType()?.original()?.typeConstructor() ?: it.typeConstructor() }
if (uniqueStubTypes.size > 1) return nullableAnyType()
if (stubTypeVariables.none { it.isDefinitelyNotNullType() }) {
return uniquify(stubTypeVariables.ifEmpty { types }, contextStubTypesNotEqual).singleOrNull() ?: return nullableAnyType()
}
}
val uniqueTypes = uniquify(nonTypeVariables, contextStubTypesNotEqual)
@@ -192,16 +203,6 @@ object NewCommonSuperTypeCalculator {
return findSuperTypeConstructorsAndIntersectResult(explicitSupertypes, depth, contextStubTypesEqualToAnything)
}
private fun TypeSystemCommonSuperTypesContext.isStubTypeVariable(type: SimpleTypeMarker): Boolean {
return type.isStubType() || isStubCapturedTypeVariable(type)
}
private fun TypeSystemCommonSuperTypesContext.isStubCapturedTypeVariable(type: SimpleTypeMarker): Boolean {
val projectedType =
type.asCapturedType()?.typeConstructor()?.projection()?.takeUnless { it.isStarProjection() }?.getType() ?: return false
return projectedType.asSimpleType()?.isStubType() == true
}
private fun TypeSystemCommonSuperTypesContext.isTypeVariable(type: SimpleTypeMarker): Boolean {
return type.isStubTypeForVariableInSubtyping() || isCapturedTypeVariable(type)
}
@@ -34,16 +34,16 @@ fun <E> select4(x: E?, y: In<E>): E = x!!
fun test() {
val ret = build {
emit("1")
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)?")!>select1(get(), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)?")!>select1(get(), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)?")!>select1(Test.foo(get()), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)?")!>select1(Test.foo(get()), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select4(get(), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select4(get(), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select4(Test.foo(get()), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select4(Test.foo(get()), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select1(get(), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select1(get(), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select1(Test.foo(get()), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select1(Test.foo(get()), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>select4(get(), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>select4(get(), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>select4(Test.foo(get()), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>select4(Test.foo(get()), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select4(id(Test.foo(get())), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>select4(id(Test.foo(get())), getIn())<!>
build2 {
emit(1)
@@ -55,30 +55,30 @@ fun test() {
select2(get(), <!TYPE_MISMATCH("TypeVariable(R1); TypeVariable(R2)")!>Test.foo(this@build.getIn())<!>)
select2(Test.foo(this@build.get()), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)!")!>Test.foo(getIn())<!>)
select2(Test.foo(get()), <!TYPE_MISMATCH("TypeVariable(R1); TypeVariable(R2)!")!>this@build.getIn()<!>)
select3(this@build.get(), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)")!>getIn()<!>)
select3(get(), <!TYPE_MISMATCH("TypeVariable(R1); TypeVariable(R2)")!>Test.foo(this@build.getIn())<!>)
select3(this@build.get(), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)!!")!>getIn()<!>)
select3(get(), <!TYPE_MISMATCH("TypeVariable(R1); TypeVariable(R2)!!")!>Test.foo(this@build.getIn())<!>)
select3(Test.foo(this@build.get()), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)")!>Test.foo(getIn())<!>)
select3(Test.foo(get()), <!TYPE_MISMATCH("TypeVariable(R1); TypeVariable(R2)")!>this@build.getIn()<!>)
select4(this@build.get(), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)")!>getIn()<!>)
select4(get(), <!TYPE_MISMATCH("TypeVariable(R1); TypeVariable(R2)")!>Test.foo(this@build.getIn())<!>)
select4(this@build.get(), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)!!")!>getIn()<!>)
select4(get(), <!TYPE_MISMATCH("TypeVariable(R1); TypeVariable(R2)!!")!>Test.foo(this@build.getIn())<!>)
select4(Test.foo(this@build.get()), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)")!>Test.foo(getIn())<!>)
select4(Test.foo(get()), <!TYPE_MISMATCH("TypeVariable(R1); TypeVariable(R2)")!>this@build.getIn()<!>)
select4(id(Test.foo(this@build.get())), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)")!>getIn()<!>)
select4(id(Test.foo(this@build.get())), <!TYPE_MISMATCH("TypeVariable(R2); TypeVariable(R1)!!")!>getIn()<!>)
""
}
""
}
val ret2 = build {
emit(if (true) "" else null)
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select2(get(), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select2(get(), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("(TypeVariable(R1)..TypeVariable(R1)?)")!>select2(Test.foo(get()), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("(TypeVariable(R1)..TypeVariable(R1)?)")!>select2(Test.foo(get()), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select3(get(), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select3(get(), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select3(Test.foo(get()), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("TypeVariable(R1)")!>select3(Test.foo(get()), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select2(get(), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select2(get(), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select2(Test.foo(get()), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select2(Test.foo(get()), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>select3(get(), getIn())<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String")!>select3(get(), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select3(Test.foo(get()), Test.foo(getIn()))<!>
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.String?")!>select3(Test.foo(get()), getIn())<!>
""
}
}