From 650e2501bbd2566713752aaecf4f64fe3ba697a3 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Fri, 1 Nov 2019 15:50:48 +0300 Subject: [PATCH] [NI] Prioritize variables with trivial constraints over complex ones Consider the following constraint system (from the test example): Nothing? <: V1 F!! <: V2 Inv <: S Inv <: S Where V1, V2, S are type variables, and F has nullable upper bound. Type variable fixation order should be: V2 -> V1 -> S, and the problem was that previously after fixation of type variable V2 we were trying to fix S (before V1), so we had the following constraints on S: Inv <: S Inv <: S => S were fixed to Inv And after this V1 was fixed to F!! which is contradictory as Nothing? is not a subtype of F!!. #KT-33033 Fixed --- .../fir/FirDiagnosticsSmokeTestGenerated.java | 5 +++++ .../components/VariableFixationFinder.kt | 4 ++-- ...hingConstraintEarlierThanComplexVariable.kt | 16 ++++++++++++++++ ...ingConstraintEarlierThanComplexVariable.txt | 18 ++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 5 +++++ .../DiagnosticsUsingJavacTestGenerated.java | 5 +++++ 6 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt create mode 100644 compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.txt diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java index 1a70e6828bc..12f79a0f339 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -10334,6 +10334,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/inference/constraints/errorUpperBoundConstraint.kt"); } + @TestMetadata("fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt") + public void testFixTypeVariableWithNothingConstraintEarlierThanComplexVariable() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt"); + } + @TestMetadata("ignoreConstraintFromImplicitInNothing.kt") public void testIgnoreConstraintFromImplicitInNothing() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/constraints/ignoreConstraintFromImplicitInNothing.kt"); diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/VariableFixationFinder.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/VariableFixationFinder.kt index f6019bceb8e..5af7ad1f3ca 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/VariableFixationFinder.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/VariableFixationFinder.kt @@ -52,8 +52,8 @@ class VariableFixationFinder( private enum class TypeVariableFixationReadiness { FORBIDDEN, WITHOUT_PROPER_ARGUMENT_CONSTRAINT, // proper constraint from arguments -- not from upper bound for type parameters - WITH_TRIVIAL_OR_NON_PROPER_CONSTRAINTS, // proper trivial constraint from arguments, Nothing <: T WITH_COMPLEX_DEPENDENCY, // if type variable T has constraint with non fixed type variable inside (non-top-level): T <: Foo + WITH_TRIVIAL_OR_NON_PROPER_CONSTRAINTS, // proper trivial constraint from arguments, Nothing <: T RELATED_TO_ANY_OUTPUT_TYPE, READY_FOR_FIXATION, } @@ -65,8 +65,8 @@ class VariableFixationFinder( !notFixedTypeVariables.contains(variable) || dependencyProvider.isVariableRelatedToTopLevelType(variable) -> TypeVariableFixationReadiness.FORBIDDEN !variableHasProperArgumentConstraints(variable) -> TypeVariableFixationReadiness.WITHOUT_PROPER_ARGUMENT_CONSTRAINT - variableHasTrivialOrNonProperConstraints(variable) -> TypeVariableFixationReadiness.WITH_TRIVIAL_OR_NON_PROPER_CONSTRAINTS hasDependencyToOtherTypeVariables(variable) -> TypeVariableFixationReadiness.WITH_COMPLEX_DEPENDENCY + variableHasTrivialOrNonProperConstraints(variable) -> TypeVariableFixationReadiness.WITH_TRIVIAL_OR_NON_PROPER_CONSTRAINTS dependencyProvider.isVariableRelatedToAnyOutputType(variable) -> TypeVariableFixationReadiness.RELATED_TO_ANY_OUTPUT_TYPE else -> TypeVariableFixationReadiness.READY_FOR_FIXATION } diff --git a/compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt b/compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt new file mode 100644 index 00000000000..6e6afe5bda0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt @@ -0,0 +1,16 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +class Inv + +fun foo(t: K?) = Inv() + +fun bar(t: T) = foo(t) +fun bar1(t: V1): Inv = foo(t) +fun bar2(t: V2): Inv = foo(t) + +fun select(x: S, y: S): S = x + +fun fail(t: T?) = if (t == null) bar(t) else bar(t) +fun fail1(t: F?, n: Nothing?) = select(bar1(n), bar2(t!!)) +fun fail2(t: F?, n: Nothing?) = if (t == null) bar1(t) else bar2(t) +fun fail3(t: F?) = select(bar1(null), bar2(t?.let { it })) diff --git a/compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.txt b/compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.txt new file mode 100644 index 00000000000..943e23e63e7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.txt @@ -0,0 +1,18 @@ +package + +public fun bar(/*0*/ t: T): Inv +public fun bar1(/*0*/ t: V1): Inv +public fun bar2(/*0*/ t: V2): Inv +public fun fail(/*0*/ t: T?): Inv +public fun fail1(/*0*/ t: F?, /*1*/ n: kotlin.Nothing?): Inv +public fun fail2(/*0*/ t: F?, /*1*/ n: kotlin.Nothing?): Inv +public fun fail3(/*0*/ t: F?): Inv +public fun foo(/*0*/ t: K?): Inv +public fun select(/*0*/ x: S, /*1*/ y: S): S + +public final class Inv { + public constructor Inv() + 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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index daf9f3f98cf..c6eacd1f618 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -10341,6 +10341,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/inference/constraints/errorUpperBoundConstraint.kt"); } + @TestMetadata("fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt") + public void testFixTypeVariableWithNothingConstraintEarlierThanComplexVariable() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt"); + } + @TestMetadata("ignoreConstraintFromImplicitInNothing.kt") public void testIgnoreConstraintFromImplicitInNothing() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/constraints/ignoreConstraintFromImplicitInNothing.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 077f957e73d..73c1c692e44 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -10336,6 +10336,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/inference/constraints/errorUpperBoundConstraint.kt"); } + @TestMetadata("fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt") + public void testFixTypeVariableWithNothingConstraintEarlierThanComplexVariable() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/constraints/fixTypeVariableWithNothingConstraintEarlierThanComplexVariable.kt"); + } + @TestMetadata("ignoreConstraintFromImplicitInNothing.kt") public void testIgnoreConstraintFromImplicitInNothing() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/constraints/ignoreConstraintFromImplicitInNothing.kt");