From b96708c3e29433153031db2aedebbe51435586e4 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Fri, 11 Feb 2022 14:20:28 +0300 Subject: [PATCH] [FE] Support reporting `INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION` for empty intersection types with type parameters --- .../diagnostics/KtFirDataClassConverters.kt | 10 ++++++++ .../api/fir/diagnostics/KtFirDiagnostics.kt | 6 +++++ .../fir/diagnostics/KtFirDiagnosticsImpl.kt | 7 ++++++ ...CompilerTestFE10TestdataTestGenerated.java | 24 +++++++++++++++++++ ...irOldFrontendDiagnosticsTestGenerated.java | 24 +++++++++++++++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 24 +++++++++++++++++++ .../ConstraintSystemCompletionContext.kt | 4 +++- ...romCovariantAndContravariantTypes.diag.txt | 8 +++---- .../tests/inference/kt45461.diag.txt | 4 ++-- .../tests/inference/kt45461_2.diag.txt | 10 ++++++++ .../tests/inference/kt45461_2.fir.kt | 11 +++++++++ .../diagnostics/tests/inference/kt45461_2.kt | 11 +++++++++ .../diagnostics/tests/inference/kt45461_2.txt | 18 ++++++++++++++ .../diagnostics/tests/inference/kt45461_3.kt | 11 +++++++++ .../diagnostics/tests/inference/kt45461_3.txt | 18 ++++++++++++++ .../diagnostics/tests/inference/kt45461_4.kt | 11 +++++++++ .../diagnostics/tests/inference/kt45461_4.txt | 18 ++++++++++++++ .../tests/inference/kt45461_5.diag.txt | 9 +++++++ .../tests/inference/kt45461_5.fir.kt | 11 +++++++++ .../diagnostics/tests/inference/kt45461_5.kt | 11 +++++++++ .../diagnostics/tests/inference/kt45461_5.txt | 18 ++++++++++++++ .../tests/inference/kt48765.diag.txt | 10 ++++---- .../tests/inference/kt48935.diag.txt | 4 ++-- .../tests/inference/kt49661.diag.txt | 2 +- .../test/runners/DiagnosticTestGenerated.java | 24 +++++++++++++++++++ .../kotlin/types/model/TypeSystemContext.kt | 18 ++++++++++---- 26 files changed, 307 insertions(+), 19 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_2.diag.txt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_2.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_2.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_2.txt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_3.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_3.txt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_4.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_4.txt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_5.diag.txt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_5.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_5.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt45461_5.txt diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDataClassConverters.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDataClassConverters.kt index d7fef737026..20b4ad0d59d 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDataClassConverters.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDataClassConverters.kt @@ -1976,6 +1976,16 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert token, ) } + add(FirErrors.INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION) { firDiagnostic -> + InferredTypeVariableIntoEmptyIntersectionImpl( + firDiagnostic.a, + firDiagnostic.b.map { coneKotlinType -> + firSymbolBuilder.typeBuilder.buildKtType(coneKotlinType) + }, + firDiagnostic as KtPsiDiagnostic, + token, + ) + } add(FirErrors.EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED) { firDiagnostic -> ExtensionInClassReferenceNotAllowedImpl( firSymbolBuilder.callableBuilder.buildCallableSymbol(firDiagnostic.a), diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDiagnostics.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDiagnostics.kt index e7500ac921c..8e176b12b43 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDiagnostics.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDiagnostics.kt @@ -1405,6 +1405,12 @@ sealed class KtFirDiagnostic : KtDiagnosticWithPsi { abstract val kotlinClass: FqName } + abstract class InferredTypeVariableIntoEmptyIntersection : KtFirDiagnostic() { + override val diagnosticClass get() = InferredTypeVariableIntoEmptyIntersection::class + abstract val typeVariableDescription: String + abstract val incompatibleTypes: List + } + abstract class ExtensionInClassReferenceNotAllowed : KtFirDiagnostic() { override val diagnosticClass get() = ExtensionInClassReferenceNotAllowed::class abstract val referencedDeclaration: KtCallableSymbol diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDiagnosticsImpl.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDiagnosticsImpl.kt index 706daf8de89..969385c8c52 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDiagnosticsImpl.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/diagnostics/KtFirDiagnosticsImpl.kt @@ -1687,6 +1687,13 @@ internal class PlatformClassMappedToKotlinImpl( override val token: ValidityToken, ) : KtFirDiagnostic.PlatformClassMappedToKotlin(), KtAbstractFirDiagnostic +internal class InferredTypeVariableIntoEmptyIntersectionImpl( + override val typeVariableDescription: String, + override val incompatibleTypes: List, + override val firDiagnostic: KtPsiDiagnostic, + override val token: ValidityToken, +) : KtFirDiagnostic.InferredTypeVariableIntoEmptyIntersection(), KtAbstractFirDiagnostic + internal class ExtensionInClassReferenceNotAllowedImpl( override val referencedDeclaration: KtCallableSymbol, override val firDiagnostic: KtPsiDiagnostic, diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 90cb98d4aef..e55f0b0f6e5 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -13840,6 +13840,30 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/inference/kt45461.kt"); } + @Test + @TestMetadata("kt45461_2.kt") + public void testKt45461_2() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_2.kt"); + } + + @Test + @TestMetadata("kt45461_3.kt") + public void testKt45461_3() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_3.kt"); + } + + @Test + @TestMetadata("kt45461_4.kt") + public void testKt45461_4() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_4.kt"); + } + + @Test + @TestMetadata("kt45461_5.kt") + public void testKt45461_5() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_5.kt"); + } + @Test @TestMetadata("kt46515.kt") public void testKt46515() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index cdf56cebf40..e9e11b6b8b0 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -13840,6 +13840,30 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inference/kt45461.kt"); } + @Test + @TestMetadata("kt45461_2.kt") + public void testKt45461_2() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_2.kt"); + } + + @Test + @TestMetadata("kt45461_3.kt") + public void testKt45461_3() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_3.kt"); + } + + @Test + @TestMetadata("kt45461_4.kt") + public void testKt45461_4() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_4.kt"); + } + + @Test + @TestMetadata("kt45461_5.kt") + public void testKt45461_5() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_5.kt"); + } + @Test @TestMetadata("kt46515.kt") public void testKt46515() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index a1ca03b08ec..e9eb435b3e3 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -13840,6 +13840,30 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/inference/kt45461.kt"); } + @Test + @TestMetadata("kt45461_2.kt") + public void testKt45461_2() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_2.kt"); + } + + @Test + @TestMetadata("kt45461_3.kt") + public void testKt45461_3() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_3.kt"); + } + + @Test + @TestMetadata("kt45461_4.kt") + public void testKt45461_4() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_4.kt"); + } + + @Test + @TestMetadata("kt45461_5.kt") + public void testKt45461_5() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_5.kt"); + } + @Test @TestMetadata("kt46515.kt") public void testKt46515() throws Exception { diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintSystemCompletionContext.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintSystemCompletionContext.kt index b36f2b6d974..278d621cdb4 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintSystemCompletionContext.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintSystemCompletionContext.kt @@ -111,6 +111,8 @@ abstract class ConstraintSystemCompletionContext : VariableFixationFinder.Contex fun List.extractUpperTypes(): List = filter { constraint -> - constraint.kind == ConstraintKind.UPPER && !constraint.type.contains { !it.typeConstructor().isClassTypeConstructor() } + constraint.kind == ConstraintKind.UPPER && !constraint.type.contains { + !it.typeConstructor().isClassTypeConstructor() && !it.typeConstructor().isTypeParameterTypeConstructor() + } }.map { it.type } } diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/selectFromCovariantAndContravariantTypes.diag.txt b/compiler/testData/diagnostics/tests/inference/commonSystem/selectFromCovariantAndContravariantTypes.diag.txt index 60d06aee45e..2b0d901c1af 100644 --- a/compiler/testData/diagnostics/tests/inference/commonSystem/selectFromCovariantAndContravariantTypes.diag.txt +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/selectFromCovariantAndContravariantTypes.diag.txt @@ -1,12 +1,12 @@ -/selectFromCovariantAndContravariantTypes.kt:12:22: warning: parameter 'y' is never used +/selectFromCovariantAndContravariantTypes.kt:11:22: warning: parameter 'y' is never used fun select(x: K, y: K): K = x ^ -/selectFromCovariantAndContravariantTypes.kt:13:19: warning: parameter 'x' is never used +/selectFromCovariantAndContravariantTypes.kt:12:19: warning: parameter 'x' is never used fun genericIn(x: In) {} ^ -/selectFromCovariantAndContravariantTypes.kt:14:20: warning: parameter 'x' is never used +/selectFromCovariantAndContravariantTypes.kt:13:20: warning: parameter 'x' is never used fun genericOut(x: Out) {} ^ -/selectFromCovariantAndContravariantTypes.kt:17:5: warning: type argument for a type parameter V can't be inferred because it's upper bounded by incompatible types: A, B. This will become an error in Kotlin 1.8 +/selectFromCovariantAndContravariantTypes.kt:16:5: warning: type argument for a type parameter V can't be inferred because it's upper bounded by incompatible types: A, B. This will become an error in Kotlin 1.8 genericIn(select(a, b)) ^ diff --git a/compiler/testData/diagnostics/tests/inference/kt45461.diag.txt b/compiler/testData/diagnostics/tests/inference/kt45461.diag.txt index 7662ff61537..67f32502222 100644 --- a/compiler/testData/diagnostics/tests/inference/kt45461.diag.txt +++ b/compiler/testData/diagnostics/tests/inference/kt45461.diag.txt @@ -1,6 +1,6 @@ -/kt45461.kt:6:25: warning: parameter 'foo' is never used +/kt45461.kt:5:25: warning: parameter 'foo' is never used fun takeFoo(foo: Foo) {} ^ -/kt45461.kt:11:19: warning: type argument for a type parameter S can't be inferred because it's upper bounded by incompatible types: String, Int. This will become an error in Kotlin 1.8 +/kt45461.kt:10:19: warning: type argument for a type parameter S can't be inferred because it's upper bounded by incompatible types: String, Int. This will become an error in Kotlin 1.8 Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 ^ diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_2.diag.txt b/compiler/testData/diagnostics/tests/inference/kt45461_2.diag.txt new file mode 100644 index 00000000000..59339fb658b --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_2.diag.txt @@ -0,0 +1,10 @@ +/kt45461_2.kt:5:25: warning: parameter 'foo' is never used + fun takeFoo(foo: Foo) {} + ^ +/kt45461_2.kt:8:10: warning: 'Int' is a final type, and thus a value of the type parameter is predetermined +fun main() { + ^ +/kt45461_2.kt:10:19: warning: type argument for a type parameter S can't be inferred because it's upper bounded by incompatible types: String, K. This will become an error in Kotlin 1.8 + Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 + ^ + diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_2.fir.kt b/compiler/testData/diagnostics/tests/inference/kt45461_2.fir.kt new file mode 100644 index 00000000000..c14087db519 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_2.fir.kt @@ -0,0 +1,11 @@ +// RENDER_DIAGNOSTICS_FULL_TEXT +class Foo + +class Bar { + fun takeFoo(foo: Foo) {} +} + +fun Int> main() { + val foo = Foo() + Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_2.kt b/compiler/testData/diagnostics/tests/inference/kt45461_2.kt new file mode 100644 index 00000000000..47db6f85b93 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_2.kt @@ -0,0 +1,11 @@ +// RENDER_DIAGNOSTICS_FULL_TEXT +class Foo + +class Bar { + fun takeFoo(foo: Foo) {} +} + +fun Int> main() { + val foo = Foo() + Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_2.txt b/compiler/testData/diagnostics/tests/inference/kt45461_2.txt new file mode 100644 index 00000000000..214853274d9 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_2.txt @@ -0,0 +1,18 @@ +package + +public fun main(): kotlin.Unit + +public final class Bar { + public constructor Bar() + 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 takeFoo(/*0*/ foo: Foo): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class Foo { + public constructor Foo() + 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/testData/diagnostics/tests/inference/kt45461_3.kt b/compiler/testData/diagnostics/tests/inference/kt45461_3.kt new file mode 100644 index 00000000000..65a9f54b37a --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_3.kt @@ -0,0 +1,11 @@ +// FIR_IDENTICAL +class Foo + +class Bar { + fun takeFoo(foo: Foo) {} +} + +fun String> main() { + val foo = Foo() + Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_3.txt b/compiler/testData/diagnostics/tests/inference/kt45461_3.txt new file mode 100644 index 00000000000..2e014b815a2 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_3.txt @@ -0,0 +1,18 @@ +package + +public fun main(): kotlin.Unit + +public final class Bar { + public constructor Bar() + 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 takeFoo(/*0*/ foo: Foo): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class Foo { + public constructor Foo() + 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/testData/diagnostics/tests/inference/kt45461_4.kt b/compiler/testData/diagnostics/tests/inference/kt45461_4.kt new file mode 100644 index 00000000000..2fa0decdec1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_4.kt @@ -0,0 +1,11 @@ +// FIR_IDENTICAL +class Foo + +class Bar { + fun takeFoo(foo: Foo) {} +} + +fun main() { + val foo = Foo() + Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_4.txt b/compiler/testData/diagnostics/tests/inference/kt45461_4.txt new file mode 100644 index 00000000000..360bbb0086d --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_4.txt @@ -0,0 +1,18 @@ +package + +public fun main(): kotlin.Unit + +public final class Bar { + public constructor Bar() + 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 takeFoo(/*0*/ foo: Foo): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class Foo { + public constructor Foo() + 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/testData/diagnostics/tests/inference/kt45461_5.diag.txt b/compiler/testData/diagnostics/tests/inference/kt45461_5.diag.txt new file mode 100644 index 00000000000..345c7877044 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_5.diag.txt @@ -0,0 +1,9 @@ +/kt45461_5.kt:5:25: warning: parameter 'foo' is never used + fun takeFoo(foo: Foo) {} + ^ +/kt45461_5.kt:8:23: warning: 'Int' is a final type, and thus a value of the type parameter is predetermined +fun main() { + ^ +/kt45461_5.kt:10:19: warning: type argument for a type parameter S can't be inferred because it's upper bounded by incompatible types: String, K. This will become an error in Kotlin 1.8 + Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 + ^ diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_5.fir.kt b/compiler/testData/diagnostics/tests/inference/kt45461_5.fir.kt new file mode 100644 index 00000000000..350fcda5b69 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_5.fir.kt @@ -0,0 +1,11 @@ +// RENDER_DIAGNOSTICS_FULL_TEXT +class Foo + +class Bar { + fun takeFoo(foo: Foo) {} +} + +fun Int> main() { + val foo = Foo() + Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_5.kt b/compiler/testData/diagnostics/tests/inference/kt45461_5.kt new file mode 100644 index 00000000000..bdd866063bf --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_5.kt @@ -0,0 +1,11 @@ +// RENDER_DIAGNOSTICS_FULL_TEXT +class Foo + +class Bar { + fun takeFoo(foo: Foo) {} +} + +fun Int> main() { + val foo = Foo() + Bar().takeFoo(foo) // error in 1.3.72, no error in 1.4.31 +} diff --git a/compiler/testData/diagnostics/tests/inference/kt45461_5.txt b/compiler/testData/diagnostics/tests/inference/kt45461_5.txt new file mode 100644 index 00000000000..17f69029a04 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt45461_5.txt @@ -0,0 +1,18 @@ +package + +public fun main(): kotlin.Unit + +public final class Bar { + public constructor Bar() + 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 takeFoo(/*0*/ foo: Foo): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class Foo { + public constructor Foo() + 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/testData/diagnostics/tests/inference/kt48765.diag.txt b/compiler/testData/diagnostics/tests/inference/kt48765.diag.txt index b4ab4c5d19d..f30446d8d81 100644 --- a/compiler/testData/diagnostics/tests/inference/kt48765.diag.txt +++ b/compiler/testData/diagnostics/tests/inference/kt48765.diag.txt @@ -1,15 +1,15 @@ -/kt48765.kt:5:44: warning: parameter 'x1' is never used +/kt48765.kt:4:44: warning: parameter 'x1' is never used fun > foo(x1: T2, x2: T1) {} ^ -/kt48765.kt:5:52: warning: parameter 'x2' is never used +/kt48765.kt:4:52: warning: parameter 'x2' is never used fun > foo(x1: T2, x2: T1) {} ^ -/kt48765.kt:9:13: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: String, Number. This will become an error in Kotlin 1.8 +/kt48765.kt:8:13: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: String, Number. This will become an error in Kotlin 1.8 B().foo(x, foo()) ^ -/kt48765.kt:13:9: warning: 'String' is a final type, and thus a value of the type parameter is predetermined +/kt48765.kt:12:9: warning: 'String' is a final type, and thus a value of the type parameter is predetermined fun foo(): T { ^ -/kt48765.kt:14:15: warning: unchecked cast: String to T +/kt48765.kt:13:15: warning: unchecked cast: String to T return "" as T // this cast is safe because String is final. ^ diff --git a/compiler/testData/diagnostics/tests/inference/kt48935.diag.txt b/compiler/testData/diagnostics/tests/inference/kt48935.diag.txt index 649d7d5bc27..8cb6db989a0 100644 --- a/compiler/testData/diagnostics/tests/inference/kt48935.diag.txt +++ b/compiler/testData/diagnostics/tests/inference/kt48935.diag.txt @@ -1,7 +1,7 @@ -/kt48935.kt:7:35: warning: parameter 'func' is never used +/kt48935.kt:6:35: warning: parameter 'func' is never used fun exampleGenericFunction(func: V) where T: Base, V: (T) -> Unit { ^ -/kt48935.kt:13:5: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: Base, DoesNotImplementBase. This will become an error in Kotlin 1.8 +/kt48935.kt:12:5: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: Base, DoesNotImplementBase. This will become an error in Kotlin 1.8 exampleGenericFunction(func) // expected this to be a compilation error as the T: Base constraint should not be satisfied ^ diff --git a/compiler/testData/diagnostics/tests/inference/kt49661.diag.txt b/compiler/testData/diagnostics/tests/inference/kt49661.diag.txt index 9c8443bfaa6..43f7298dc17 100644 --- a/compiler/testData/diagnostics/tests/inference/kt49661.diag.txt +++ b/compiler/testData/diagnostics/tests/inference/kt49661.diag.txt @@ -1,4 +1,4 @@ -/kt49661.kt:11:5: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: Foo, Int. This will become an error in Kotlin 1.8 +/kt49661.kt:10:5: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: Foo, Int. This will become an error in Kotlin 1.8 f { g() } ^ diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index f10638f4cd6..558ade22bdd 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -13846,6 +13846,30 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inference/kt45461.kt"); } + @Test + @TestMetadata("kt45461_2.kt") + public void testKt45461_2() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_2.kt"); + } + + @Test + @TestMetadata("kt45461_3.kt") + public void testKt45461_3() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_3.kt"); + } + + @Test + @TestMetadata("kt45461_4.kt") + public void testKt45461_4() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_4.kt"); + } + + @Test + @TestMetadata("kt45461_5.kt") + public void testKt45461_5() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt45461_5.kt"); + } + @Test @TestMetadata("kt46515.kt") public void testKt46515() throws Exception { diff --git a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt index 68206d806a7..d77a9dd6b3c 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/types/model/TypeSystemContext.kt @@ -178,15 +178,25 @@ interface TypeSystemInferenceExtensionContext : TypeSystemContext, TypeSystemBui fun KotlinTypeMarker.isFinal(): Boolean - fun Collection.isEmptyIntersection(): Boolean = - any { first -> - any { second -> + fun Collection.isEmptyIntersection(): Boolean { + val expandedTypes = buildSet { + for (type in this@isEmptyIntersection) { + val typeConstructor = type.typeConstructor() + when { + typeConstructor.isClassTypeConstructor() -> add(type) + typeConstructor.isTypeParameterTypeConstructor() -> addAll(typeConstructor.supertypes()) + } + } + }.takeIf { it.isNotEmpty() } ?: return false + + return expandedTypes.any { first -> + expandedTypes.any { second -> first !== second && first.isFinal() && - second.typeConstructor().isClassTypeConstructor() && !AbstractTypeChecker.isSubtypeOf(this@TypeSystemInferenceExtensionContext, first, second) } } + } fun KotlinTypeMarker.removeAnnotations(): KotlinTypeMarker fun KotlinTypeMarker.removeExactAnnotation(): KotlinTypeMarker