[NI] Dont' add trivial constraints with Nothing from incorporation

#KT-24490 Fixed
 #KT-26816 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2019-01-18 14:47:55 +03:00
parent 662e2287cc
commit 147d7844bc
17 changed files with 250 additions and 12 deletions
@@ -19,7 +19,10 @@ import org.jetbrains.kotlin.utils.addIfNotNull
import java.util.*
// todo problem: intersection types in constrains: A <: Number, B <: Inv<A & Any> =>? B <: Inv<out Number & Any>
class ConstraintIncorporator(val typeApproximator: TypeApproximator) {
class ConstraintIncorporator(
val typeApproximator: TypeApproximator,
val trivialConstraintTypeInferenceOracle: TrivialConstraintTypeInferenceOracle
) {
interface Context {
val allTypeVariablesWithConstraints: Collection<VariableWithConstraints>
@@ -99,9 +102,10 @@ class ConstraintIncorporator(val typeApproximator: TypeApproximator) {
otherVariable: NewTypeVariable,
otherConstraint: Constraint
) {
val baseConstraintType = baseConstraint.type
val typeForApproximation = when (otherConstraint.kind) {
ConstraintKind.EQUALITY -> {
baseConstraint.type.substitute(otherVariable, otherConstraint.type)
baseConstraintType.substituteTypeVariable(otherVariable, otherConstraint.type)
}
ConstraintKind.UPPER -> {
val newCapturedTypeConstructor = NewCapturedTypeConstructor(
@@ -113,7 +117,7 @@ class ConstraintIncorporator(val typeApproximator: TypeApproximator) {
newCapturedTypeConstructor,
lowerType = null
)
baseConstraint.type.substitute(otherVariable, temporaryCapturedType)
baseConstraintType.substituteTypeVariable(otherVariable, temporaryCapturedType)
}
ConstraintKind.LOWER -> {
val newCapturedTypeConstructor = NewCapturedTypeConstructor(
@@ -125,23 +129,24 @@ class ConstraintIncorporator(val typeApproximator: TypeApproximator) {
newCapturedTypeConstructor,
lowerType = otherConstraint.type
)
baseConstraint.type.substitute(otherVariable, temporaryCapturedType)
baseConstraintType.substituteTypeVariable(otherVariable, temporaryCapturedType)
}
}
if (baseConstraint.kind != ConstraintKind.UPPER) {
c.addNewIncorporatedConstraint(approximateCapturedTypes(typeForApproximation, toSuper = false), targetVariable.defaultType)
val generatedConstraintType = approximateCapturedTypes(typeForApproximation, toSuper = false)
if (!trivialConstraintTypeInferenceOracle.isGeneratedConstraintTrivial(otherConstraint, generatedConstraintType)) {
c.addNewIncorporatedConstraint(generatedConstraintType, targetVariable.defaultType)
}
}
if (baseConstraint.kind != ConstraintKind.LOWER) {
c.addNewIncorporatedConstraint(targetVariable.defaultType, approximateCapturedTypes(typeForApproximation, toSuper = true))
val generatedConstraintType = approximateCapturedTypes(typeForApproximation, toSuper = true)
if (!trivialConstraintTypeInferenceOracle.isGeneratedConstraintTrivial(otherConstraint, generatedConstraintType)) {
c.addNewIncorporatedConstraint(targetVariable.defaultType, generatedConstraintType)
}
}
}
private fun UnwrappedType.substitute(typeVariable: NewTypeVariable, value: UnwrappedType): UnwrappedType {
val substitutor = NewTypeSubstitutorByConstructorMap(mapOf(typeVariable.freshTypeConstructor to value))
return substitutor.safeSubstitute(this)
}
private fun approximateCapturedTypes(type: UnwrappedType, toSuper: Boolean): UnwrappedType =
if (toSuper) typeApproximator.approximateToSuperType(type, TypeApproximatorConfiguration.IncorporationConfiguration) ?: type
else typeApproximator.approximateToSubType(type, TypeApproximatorConfiguration.IncorporationConfiguration) ?: type
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.NewCapturedType
@@ -164,4 +165,9 @@ class FreshVariableNewTypeSubstitutor(val freshVariables: List<TypeVariableFromC
companion object {
val Empty = FreshVariableNewTypeSubstitutor(emptyList())
}
}
fun UnwrappedType.substituteTypeVariable(typeVariable: NewTypeVariable, value: UnwrappedType): UnwrappedType {
val substitutor = NewTypeSubstitutorByConstructorMap(mapOf(typeVariable.freshTypeConstructor to value))
return substitutor.safeSubstitute(this)
}
@@ -7,7 +7,9 @@ package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.resolve.calls.inference.model.Constraint
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind
import org.jetbrains.kotlin.resolve.calls.inference.model.NewTypeVariable
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.typeUtil.contains
import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.isNullableNothing
@@ -26,7 +28,31 @@ class TrivialConstraintTypeInferenceOracle {
fun isSuitableResultedType(resultType: UnwrappedType): Boolean {
return !resultType.isNothingOrNullableNothing()
}
// It's possible to generate Nothing-like constraints inside incorporation mechanism:
// For instance, when two type variables are in subtyping relation `T <: K`, after incorporation
// there will be constraint `approximation(out K) <: K` => `Nothing <: K`, which is innocent
// but can change result of the constraint system.
// Therefore, here we avoid adding such trivial constraints to have stable constraint system
fun isGeneratedConstraintTrivial(
otherConstraint: Constraint,
generatedConstraintType: UnwrappedType
): Boolean {
if (generatedConstraintType.isNothing()) return true
// If type that will be used to generate new constraint already contains `Nothing(?)`,
// then we can't decide that resulting constraint will be useless
if (otherConstraint.type.contains { it.isNothingOrNullableNothing() }) return false
// It's important to preserve constraints with nullable Nothing: `Nothing? <: T` (see implicitNothingConstraintFromReturn.kt test)
if (generatedConstraintType.containsOnlyNonNullableNothing()) return true
return false
}
}
private fun UnwrappedType.isNothingOrNullableNothing(): Boolean =
isNothing() || isNullableNothing()
isNothing() || isNullableNothing()
private fun UnwrappedType.containsOnlyNonNullableNothing(): Boolean =
contains { it.isNothing() } && !contains { it.isNullableNothing() }
@@ -0,0 +1,13 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
class Out<out T>(val o: T)
interface Base
class Inv<K> : Base
fun <S> select(x: S, y: S): S = x
fun test(a1: Inv<Number>, a2: Inv<Nothing?>): Out<Base> {
return select(Out(a1), Out(a2))
}
@@ -0,0 +1,25 @@
package
public fun </*0*/ S> select(/*0*/ x: S, /*1*/ y: S): S
public fun test(/*0*/ a1: Inv<kotlin.Number>, /*1*/ a2: Inv<kotlin.Nothing?>): Out<Base>
public interface Base {
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 Inv</*0*/ K> : Base {
public constructor Inv</*0*/ K>()
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 Out</*0*/ out T> {
public constructor Out</*0*/ out T>(/*0*/ o: T)
public final val o: T
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,15 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER
class Simple
fun test(s: Simple?): Simple? {
myRun {
s?.let { return it }
}
return s
}
inline fun <R> myRun(block: () -> R): R = TODO()
inline fun <K, V> K.let(block: (K) -> V): V = TODO()
@@ -0,0 +1,12 @@
package
public inline fun </*0*/ R> myRun(/*0*/ block: () -> R): R
public fun test(/*0*/ s: Simple?): Simple?
public inline fun </*0*/ K, /*1*/ V> K.let(/*0*/ block: (K) -> V): V
public final class Simple {
public constructor Simple()
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,13 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
fun <T> bar(i: T): T = i
fun foo(i: Int) = i
fun dontRun(body: () -> Unit) = Unit
class Case1 {
fun test() {
dontRun { val x = bar(bar { -> bar { -> 2} }) }
}
}
@@ -0,0 +1,13 @@
package
public fun </*0*/ T> bar(/*0*/ i: T): T
public fun dontRun(/*0*/ body: () -> kotlin.Unit): kotlin.Unit
public fun foo(/*0*/ i: kotlin.Int): kotlin.Int
public final class Case1 {
public constructor Case1()
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 final fun test(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,12 @@
// !LANGUAGE: +NewInference
fun <K> id1(k: K): K = k
fun <V> id2(v: V): V = v
fun test() {
id1 {
id2 {
3
}
}
}
@@ -0,0 +1,5 @@
package
public fun </*0*/ K> id1(/*0*/ k: K): K
public fun </*0*/ V> id2(/*0*/ v: V): V
public fun test(): kotlin.Unit
@@ -0,0 +1,37 @@
// !LANGUAGE: +NewInference
val configurations4 = listOf(
3 to mapOf(
2 to listOf(
1 to listOf(
{
2
}
)
)
)
)
val configurations3 = listOf(
3 to mapOf(
2 to listOf(
{
2
}
)
)
)
val configurations2 = mapOf(
2 to listOf(
{
2
}
)
)
val configurations1 = listOf(
{
2
}
)
@@ -0,0 +1,6 @@
package
public val configurations1: kotlin.collections.List<() -> kotlin.Int>
public val configurations2: kotlin.collections.Map<kotlin.Int, kotlin.collections.List<() -> kotlin.Int>>
public val configurations3: kotlin.collections.List<kotlin.Pair<kotlin.Int, kotlin.collections.Map<kotlin.Int, kotlin.collections.List<() -> kotlin.Int>>>>
public val configurations4: kotlin.collections.List<kotlin.Pair<kotlin.Int, kotlin.collections.Map<kotlin.Int, kotlin.collections.List<kotlin.Pair<kotlin.Int, kotlin.collections.List<() -> kotlin.Int>>>>>>
@@ -9962,11 +9962,31 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/complexDependancyOnVariableWithTrivialConstraint.kt");
}
@TestMetadata("generateConstraintWithInnerNothingType.kt")
public void testGenerateConstraintWithInnerNothingType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/generateConstraintWithInnerNothingType.kt");
}
@TestMetadata("implicitNothingConstraintFromReturn.kt")
public void testImplicitNothingConstraintFromReturn() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/implicitNothingConstraintFromReturn.kt");
}
@TestMetadata("kt24490.kt")
public void testKt24490() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/kt24490.kt");
}
@TestMetadata("lambdaNothingAndExpectedType.kt")
public void testLambdaNothingAndExpectedType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/lambdaNothingAndExpectedType.kt");
}
@TestMetadata("nestedLambdaInferenceWithIncorporationOfVariables.kt")
public void testNestedLambdaInferenceWithIncorporationOfVariables() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/nestedLambdaInferenceWithIncorporationOfVariables.kt");
}
@TestMetadata("nothingWithCallableReference.kt")
public void testNothingWithCallableReference() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/nothingWithCallableReference.kt");
@@ -1822,6 +1822,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt15516.kt");
}
@TestMetadata("nestedLambdaInferenceWithListMap.kt")
public void testNestedLambdaInferenceWithListMap() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/nestedLambdaInferenceWithListMap.kt");
}
@TestMetadata("nestedSuspendCallInsideLambda.kt")
public void testNestedSuspendCallInsideLambda() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/nestedSuspendCallInsideLambda.kt");
@@ -1822,6 +1822,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt15516.kt");
}
@TestMetadata("nestedLambdaInferenceWithListMap.kt")
public void testNestedLambdaInferenceWithListMap() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/nestedLambdaInferenceWithListMap.kt");
}
@TestMetadata("nestedSuspendCallInsideLambda.kt")
public void testNestedSuspendCallInsideLambda() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/nestedSuspendCallInsideLambda.kt");
@@ -9962,11 +9962,31 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inference/nothingType/complexDependancyOnVariableWithTrivialConstraint.kt");
}
@TestMetadata("generateConstraintWithInnerNothingType.kt")
public void testGenerateConstraintWithInnerNothingType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/generateConstraintWithInnerNothingType.kt");
}
@TestMetadata("implicitNothingConstraintFromReturn.kt")
public void testImplicitNothingConstraintFromReturn() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/implicitNothingConstraintFromReturn.kt");
}
@TestMetadata("kt24490.kt")
public void testKt24490() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/kt24490.kt");
}
@TestMetadata("lambdaNothingAndExpectedType.kt")
public void testLambdaNothingAndExpectedType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/lambdaNothingAndExpectedType.kt");
}
@TestMetadata("nestedLambdaInferenceWithIncorporationOfVariables.kt")
public void testNestedLambdaInferenceWithIncorporationOfVariables() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/nestedLambdaInferenceWithIncorporationOfVariables.kt");
}
@TestMetadata("nothingWithCallableReference.kt")
public void testNothingWithCallableReference() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/nothingWithCallableReference.kt");