[NI] Discriminate Nothing? result type from nullability constraints

Nullability constraints should not be chosen when proper
argument constraints for variables in contravariant position present.

^KT-32106 Fixed
^KT-33166 Fixed
This commit is contained in:
Pavel Kirpichenkov
2019-12-24 14:51:39 +03:00
parent a1ad680edb
commit 7ee71450bc
23 changed files with 154 additions and 35 deletions
@@ -10604,6 +10604,16 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
public void testNontrivialCallExpression() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/nontrivialCallExpression.kt");
}
@TestMetadata("preferArgumentToNullability.kt")
public void testPreferArgumentToNullability() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/preferArgumentToNullability.kt");
}
@TestMetadata("preferNothingToBound.kt")
public void testPreferNothingToBound() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/preferNothingToBound.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/inference/nothingType")
@@ -175,7 +175,10 @@ class ConstraintIncorporator(
val inputTypePosition = if (baseConstraint.position.from is OnlyInputTypeConstraintPosition)
baseConstraint.position else null
addNewIncorporatedConstraint(targetVariable, newConstraint, ConstraintContext(kind, derivedFrom, inputTypePosition))
val isNullabilityConstraint = isUsefulForNullabilityConstraint && newConstraint.isNullableNothing()
val constraintContext = ConstraintContext(kind, derivedFrom, inputTypePosition, isNullabilityConstraint)
addNewIncorporatedConstraint(targetVariable, newConstraint, constraintContext)
}
fun Context.containsConstrainingTypeWithoutProjection(
@@ -178,7 +178,7 @@ class ConstraintInjector(
val typeVariable = c.allTypeVariables[typeVariableConstructor]
?: error("Should by type variableConstructor: $typeVariableConstructor. ${c.allTypeVariables.values}")
addNewIncorporatedConstraint(typeVariable, type, ConstraintContext(kind, emptySet()))
addNewIncorporatedConstraint(typeVariable, type, ConstraintContext(kind, emptySet(), isNullabilityConstraint = false))
}
// from ConstraintIncorporator.Context
@@ -194,7 +194,7 @@ class ConstraintInjector(
type: KotlinTypeMarker,
constraintContext: ConstraintContext
) {
val (kind, derivedFrom, inputTypePosition) = constraintContext
val (kind, derivedFrom, inputTypePosition, isNullabilityConstraint) = constraintContext
var targetType = type
if (targetType.isUninferredParameter()) {
@@ -231,7 +231,12 @@ class ConstraintInjector(
}
}
possibleNewConstraints.add(typeVariable to Constraint(kind, targetType, position, derivedFrom = derivedFrom))
val newConstraint = Constraint(
kind, targetType, position,
derivedFrom = derivedFrom,
isNullabilityConstraint = isNullabilityConstraint
)
possibleNewConstraints.add(typeVariable to newConstraint)
inputTypePosition?.let { preservedInputTypePositions.add(it) }
}
@@ -264,5 +269,6 @@ class ConstraintInjector(
data class ConstraintContext(
val kind: ConstraintKind,
val derivedFrom: Set<TypeVariableMarker>,
val inputTypePosition: ConstraintPosition? = null
val inputTypePosition: ConstraintPosition? = null,
val isNullabilityConstraint: Boolean
)
@@ -80,23 +80,27 @@ class ResultTypeResolver(
}
private fun Context.isSuitableType(resultType: KotlinTypeMarker, variableWithConstraints: VariableWithConstraints): Boolean {
for (constraint in variableWithConstraints.constraints) {
if (!isProperType(constraint.type)) continue
val filteredConstraints = variableWithConstraints.constraints.filter { isProperType(it.type) }
for (constraint in filteredConstraints) {
if (!checkConstraint(this, constraint.type, constraint.kind, resultType)) return false
}
if (
!trivialConstraintTypeInferenceOracle.isSuitableResultedType(resultType)
&& isNothingNotSuitableFor(variableWithConstraints.typeVariable)
) return false
if (!trivialConstraintTypeInferenceOracle.isSuitableResultedType(resultType)) {
if (nothingIsForbiddenFor(variableWithConstraints.typeVariable)) return false
if (resultType.isNullableType() && checkSingleLowerNullabilityConstraint(filteredConstraints)) return false
}
return true
}
private fun isNothingNotSuitableFor(variable: TypeVariableMarker): Boolean {
private fun nothingIsForbiddenFor(variable: TypeVariableMarker): Boolean {
val parameterName = variable.safeAs<TypeVariableFromCallableDescriptor>()?.originalTypeParameter?.name ?: return false
val isSpecialFunctionParameter = statelessCallbacks?.isSpecialFunctionTypeParameterName(parameterName) ?: false
val isBangBangParameter = isSpecialFunctionParameter && statelessCallbacks?.isExclExclTypeParameterName(parameterName) ?: false
return isSpecialFunctionParameter && !isBangBangParameter
val isFromExclExcl = isSpecialFunctionParameter && statelessCallbacks?.isExclExclTypeParameterName(parameterName) ?: false
return isSpecialFunctionParameter && !isFromExclExcl
}
private fun checkSingleLowerNullabilityConstraint(constraints: List<Constraint>): Boolean {
return constraints.singleOrNull { it.kind.isLower() }?.isNullabilityConstraint ?: false
}
private fun Context.findSubType(variableWithConstraints: VariableWithConstraints): KotlinTypeMarker? {
@@ -68,7 +68,8 @@ class Constraint(
val type: KotlinTypeMarker, // flexible types here is allowed
val position: IncorporationConstraintPosition,
val typeHashCode: Int = type.hashCode(),
val derivedFrom: Set<TypeVariableMarker>
val derivedFrom: Set<TypeVariableMarker>,
val isNullabilityConstraint: Boolean
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
@@ -68,7 +68,8 @@ class MutableVariableWithConstraints(
constraint.type,
constraint.position,
constraint.typeHashCode,
derivedFrom = constraint.derivedFrom
derivedFrom = constraint.derivedFrom,
isNullabilityConstraint = false
)
else
constraint
@@ -0,0 +1,15 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
open class Cls
abstract class In<in I>
class SubIn : In<Cls?>()
inline fun <reified T : Cls> materialize(): T? = TODO()
fun <D> transform(transformer: In<D>, data: D): Unit = TODO()
fun test(subIn: SubIn) {
transform(subIn, materialize()) // D should be inferred to Cls?, not Nothing?
}
@@ -0,0 +1,15 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
open class Cls
abstract class In<in I>
class SubIn : In<Cls?>()
inline fun <reified T : Cls> materialize(): T? = TODO()
fun <D> transform(transformer: In<D>, data: D): Unit = TODO()
fun test(subIn: SubIn) {
transform(subIn, materialize()) // D should be inferred to Cls?, not Nothing?
}
@@ -0,0 +1,26 @@
package
public inline fun </*0*/ reified T : Cls> materialize(): T?
public fun test(/*0*/ subIn: SubIn): kotlin.Unit
public fun </*0*/ D> transform(/*0*/ transformer: In<D>, /*1*/ data: D): kotlin.Unit
public open class Cls {
public constructor Cls()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public abstract class In</*0*/ in I> {
public constructor In</*0*/ in I>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class SubIn : In<Cls?> {
public constructor SubIn()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,3 @@
fun <K> id(arg: K): K = arg
val v = id(null)
@@ -0,0 +1,3 @@
fun <K> id(arg: K): K = arg
val v = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Nothing?")!>id(null)<!>
@@ -0,0 +1,4 @@
package
public val v: kotlin.Nothing?
public fun </*0*/ K> id(/*0*/ arg: K): K
@@ -0,0 +1,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_EXPRESSION -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE -UNUSED_VALUE
class Out<out T : Any>(result: T?)
fun main() {
val a = Out(null)
a
var b: Out<Int>? = null
b = a
}
@@ -1,5 +1,3 @@
// !LANGUAGE: +NewInference
class Query<out T : Any> private constructor(
private val result: T?,
private val error: Throwable?,
@@ -1,5 +1,3 @@
// !LANGUAGE: +NewInference
class Query<out T : Any> private constructor(
private val result: T?,
private val error: Throwable?,
@@ -36,7 +36,6 @@ fun testOK(first: First, bound: Bound, second: Second) {
strictSelect(OutB(first), OutB(bound))
strictSelect(In(first), In(bound))
strictSelect(InB(first), InB(bound))
strictSelect(InB(first), InB(second)) // different behaviour in contravariant position
val out: Out<Bound> = strictSelect(Out(first), Out(second))
val outb: OutB<Bound> = strictSelect(OutB(first), OutB(second))
@@ -49,6 +48,7 @@ fun testFail(first: First, bound: Bound, second: Second) {
strictSelect(Inv(first), Inv(bound))
strictSelect(Out(first), Out(second))
strictSelect(In(first), In(second))
strictSelect(InB(first), InB(second))
strictSelect(Out(Inv(first)), Out(Inv(second)))
strictSelect(In(Inv(first)), In(Inv(second)))
}
@@ -36,7 +36,6 @@ fun testOK(first: First, bound: Bound, second: Second) {
strictSelect(OutB(first), OutB(bound))
strictSelect(In(first), In(bound))
strictSelect(InB(first), InB(bound))
<!OI;TYPE_INFERENCE_ONLY_INPUT_TYPES!>strictSelect<!>(InB(first), InB(second)) // different behaviour in contravariant position
val out: Out<Bound> = strictSelect(Out(first), Out(second))
val outb: OutB<Bound> = strictSelect(OutB(first), OutB(second))
@@ -49,6 +48,7 @@ fun testFail(first: First, bound: Bound, second: Second) {
<!TYPE_INFERENCE_ONLY_INPUT_TYPES!>strictSelect<!>(Inv(first), Inv(bound))
<!TYPE_INFERENCE_ONLY_INPUT_TYPES!>strictSelect<!>(Out(first), Out(second))
<!TYPE_INFERENCE_ONLY_INPUT_TYPES!>strictSelect<!>(In(first), In(second))
<!TYPE_INFERENCE_ONLY_INPUT_TYPES!>strictSelect<!>(InB(first), InB(second))
<!TYPE_INFERENCE_ONLY_INPUT_TYPES!>strictSelect<!>(Out(Inv(first)), Out(Inv(second)))
<!TYPE_INFERENCE_ONLY_INPUT_TYPES!>strictSelect<!>(In(Inv(first)), In(Inv(second)))
}
@@ -27,7 +27,7 @@ fun test1(): Map<Int, Int> = run {
}
fun test2(): Map<Int, Int> = run {
<!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH!>try {
<!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH!>try {
emptyMap()
} catch (e: ExcA) {
<!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, OI;TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>mapOf(<!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH!>"" to ""<!>)<!>
+2 -2
View File
@@ -2,10 +2,10 @@ FILE fqName:<root> fileName:/whenReturn.kt
FUN name:toString visibility:public modality:FINAL <> (grade:kotlin.String) returnType:kotlin.String
VALUE_PARAMETER name:grade index:0 type:kotlin.String
BLOCK_BODY
BLOCK type=kotlin.String origin=WHEN
BLOCK type=kotlin.Nothing origin=WHEN
VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:kotlin.String [val]
GET_VAR 'grade: kotlin.String declared in <root>.toString' type=kotlin.String origin=null
WHEN type=kotlin.String origin=WHEN
WHEN type=kotlin.Nothing origin=WHEN
BRANCH
if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ
arg0: GET_VAR 'val tmp_0: kotlin.String [val] declared in <root>.toString' type=kotlin.String origin=null
+9 -9
View File
@@ -1,10 +1,10 @@
FILE fqName:<root> fileName:/nonLocalReturn.kt
FUN name:test0 visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
CALL 'public final fun run <R> (block: kotlin.Function0<R of kotlin.run>): R of kotlin.run [inline] declared in kotlin' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: FUN_EXPR type=kotlin.Function0<kotlin.Unit> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit
CALL 'public final fun run <R> (block: kotlin.Function0<R of kotlin.run>): R of kotlin.run [inline] declared in kotlin' type=kotlin.Nothing origin=null
<R>: kotlin.Nothing
block: FUN_EXPR type=kotlin.Function0<kotlin.Nothing> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Nothing
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test0 (): kotlin.Unit declared in <root>'
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
@@ -33,12 +33,12 @@ FILE fqName:<root> fileName:/nonLocalReturn.kt
block: FUN_EXPR type=kotlin.Function0<kotlin.Unit> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
CALL 'public final fun run <R> (block: kotlin.Function0<R of kotlin.run>): R of kotlin.run [inline] declared in kotlin' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: FUN_EXPR type=kotlin.Function0<kotlin.Unit> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit
CALL 'public final fun run <R> (block: kotlin.Function0<R of kotlin.run>): R of kotlin.run [inline] declared in kotlin' type=kotlin.Nothing origin=null
<R>: kotlin.Nothing
block: FUN_EXPR type=kotlin.Function0<kotlin.Nothing> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Nothing
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Unit declared in <root>.test3.<anonymous>'
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Nothing declared in <root>.test3.<anonymous>'
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
FUN name:testLrmFoo1 visibility:public modality:FINAL <> (ints:kotlin.collections.List<kotlin.Int>) returnType:kotlin.Unit
VALUE_PARAMETER name:ints index:0 type:kotlin.collections.List<kotlin.Int>
@@ -285,7 +285,7 @@ fun case_24(a: ((() -> Unit) -> Unit), b: (() -> Unit)) {
}
// TESTCASE NUMBER: 25
fun case_25(a: (() -> Unit) -> Unit, b: (() -> Unit) -> Unit = if (<!SENSELESS_COMPARISON!>a == null<!>) <!DEBUG_INFO_EXPRESSION_TYPE("(() -> kotlin.Unit) -> kotlin.Unit & kotlin.Nothing"), DEBUG_INFO_SMARTCAST!>a<!> else <!TYPE_MISMATCH!>{{}}<!>) {
fun case_25(a: (() -> Unit) -> Unit, b: (() -> Unit) -> Unit = if (<!SENSELESS_COMPARISON!>a == null<!>) <!DEBUG_INFO_EXPRESSION_TYPE("(() -> kotlin.Unit) -> kotlin.Unit & kotlin.Nothing")!>a<!> else {{}}) {
<!DEBUG_INFO_EXPRESSION_TYPE("(() -> kotlin.Unit) -> kotlin.Unit")!>a<!>
<!DEBUG_INFO_EXPRESSION_TYPE("(() -> kotlin.Unit) -> kotlin.Unit")!>b<!>
}
@@ -10611,6 +10611,16 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
public void testNontrivialCallExpression() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/nontrivialCallExpression.kt");
}
@TestMetadata("preferArgumentToNullability.kt")
public void testPreferArgumentToNullability() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/preferArgumentToNullability.kt");
}
@TestMetadata("preferNothingToBound.kt")
public void testPreferNothingToBound() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/preferNothingToBound.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/inference/nothingType")
@@ -10606,6 +10606,16 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
public void testNontrivialCallExpression() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/nontrivialCallExpression.kt");
}
@TestMetadata("preferArgumentToNullability.kt")
public void testPreferArgumentToNullability() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/preferArgumentToNullability.kt");
}
@TestMetadata("preferNothingToBound.kt")
public void testPreferNothingToBound() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nestedCalls/preferNothingToBound.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/inference/nothingType")