From c30e045b1bd9a35a0706a06f9ef98b7123435f40 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 27 May 2019 13:18:59 +0300 Subject: [PATCH] [NI] Don't consider `Any!`-constraint from upper bounds as more specific #KT-31624 Fixed --- .../fir/FirDiagnosticsSmokeTestGenerated.java | 5 +++ .../components/ConstraintInjector.kt | 10 +++--- .../platformNothingAsUsefulConstraint.kt | 35 ++++++++++++++++++ .../platformNothingAsUsefulConstraint.txt | 36 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 5 +++ .../DiagnosticsUsingJavacTestGenerated.java | 5 +++ 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.kt create mode 100644 compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.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 3e527e64541..00f8899cdf3 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -10219,6 +10219,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok public void testNothingWithCallableReference() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/nothingType/nothingWithCallableReference.kt"); } + + @TestMetadata("platformNothingAsUsefulConstraint.kt") + public void testPlatformNothingAsUsefulConstraint() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.kt"); + } } @TestMetadata("compiler/testData/diagnostics/tests/inference/recursiveCalls") diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintInjector.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintInjector.kt index 8fdf6b6015a..ccfebe53e9f 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintInjector.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintInjector.kt @@ -101,12 +101,10 @@ class ConstraintInjector(val constraintIncorporator: ConstraintIncorporator, val return true // T <: T(?!) } - if (constraintType.isSimpleType()) { - if (constraint.position.from is DeclaredUpperBoundConstraintPosition && - constraint.kind == UPPER && constraintType.isNullableAny() - ) { - return true // T <: Any? - } + if (constraint.position.from is DeclaredUpperBoundConstraintPosition && + constraint.kind == UPPER && constraintType.isNullableAny() + ) { + return true // T <: Any? } return false diff --git a/compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.kt b/compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.kt new file mode 100644 index 00000000000..2c7c30c4ff6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.kt @@ -0,0 +1,35 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE + +// FILE: Tasks.java + +public class Tasks { + public static Inv call(JSam f) { + return null; + } +} + +// FILE: JSam.java + +public interface JSam { + V call(); +} + +// FILE: test.kt + +fun withLock(g: () -> K): K = g() + +class Out +class Inv + +fun Inv.asOut(): Out = TODO() + +fun test() { + val o: Out = Tasks.call { + withLock { TODO() } + }.asOut() + + ..Inv<(kotlin.Nothing..kotlin.Nothing?)>?)")!>Tasks.call { + withLock { TODO() } + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.txt b/compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.txt new file mode 100644 index 00000000000..781c72f7a6f --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.txt @@ -0,0 +1,36 @@ +package + +public fun test(): kotlin.Unit +public fun withLock(/*0*/ g: () -> K): K +public fun Inv.asOut(): Out + +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 +} + +public interface JSam { + public abstract fun call(): V! + 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 { + public constructor Out() + 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 open class Tasks { + public constructor Tasks() + 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 + + // Static members + public open fun call(/*0*/ f: JSam!): Inv! +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index e4b82335a1c..e57c8a3f796 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -10226,6 +10226,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { public void testNothingWithCallableReference() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/nothingType/nothingWithCallableReference.kt"); } + + @TestMetadata("platformNothingAsUsefulConstraint.kt") + public void testPlatformNothingAsUsefulConstraint() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.kt"); + } } @TestMetadata("compiler/testData/diagnostics/tests/inference/recursiveCalls") diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index ec5a45d07d3..4716bafbf3d 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -10221,6 +10221,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing public void testNothingWithCallableReference() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/nothingType/nothingWithCallableReference.kt"); } + + @TestMetadata("platformNothingAsUsefulConstraint.kt") + public void testPlatformNothingAsUsefulConstraint() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/nothingType/platformNothingAsUsefulConstraint.kt"); + } } @TestMetadata("compiler/testData/diagnostics/tests/inference/recursiveCalls")