From 8a863e00ba148f47d11c825faffd92c494e52ba6 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Mon, 11 Dec 2023 10:13:39 +0100 Subject: [PATCH] [FIR] Add proper constraint if Unit upper bound leads to Unit lambda return type #KT-63524 Fixed --- ...iagnosticCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...sedDiagnosticCompilerFE10TestDataTestGenerated.java | 6 ++++++ ...irLightTreeOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../FirPsiOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../resolve/inference/PostponedArgumentsAnalyzer.kt | 4 ++-- .../tests/inference/UnitUpperboundLambdaReturnType.kt | 10 ++++++++++ .../completion/postponedArgumentsAnalysis/basic.fir.kt | 4 ++-- .../postponedArgumentsAnalysis/suspendFunctions.fir.kt | 4 ++-- .../kotlin/test/runners/DiagnosticTestGenerated.java | 6 ++++++ 9 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index d90d11e7a29..6052161d5f7 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -17058,6 +17058,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/inference/underscoredTypeInForbiddenPositions.kt"); } + @Test + @TestMetadata("UnitUpperboundLambdaReturnType.kt") + public void testUnitUpperboundLambdaReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt"); + } + @Test @TestMetadata("unsoundness1.kt") public void testUnsoundness1() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index d5d8ae441bd..61596dd7d8d 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -17058,6 +17058,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/inference/underscoredTypeInForbiddenPositions.kt"); } + @Test + @TestMetadata("UnitUpperboundLambdaReturnType.kt") + public void testUnitUpperboundLambdaReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt"); + } + @Test @TestMetadata("unsoundness1.kt") public void testUnsoundness1() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index b5c64c9f867..2d3cd24f57c 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -17052,6 +17052,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/inference/underscoredTypeInForbiddenPositions.kt"); } + @Test + @TestMetadata("UnitUpperboundLambdaReturnType.kt") + public void testUnitUpperboundLambdaReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt"); + } + @Test @TestMetadata("unsoundness1.kt") public void testUnsoundness1() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index e305281fe54..57feba1b3bd 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -17058,6 +17058,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/inference/underscoredTypeInForbiddenPositions.kt"); } + @Test + @TestMetadata("UnitUpperboundLambdaReturnType.kt") + public void testUnitUpperboundLambdaReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt"); + } + @Test @TestMetadata("unsoundness1.kt") public void testUnsoundness1() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt index b0a7342c14c..3b4b7a8005b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt @@ -201,10 +201,10 @@ class PostponedArgumentsAnalyzer( } } - if (!hasExpressionInReturnArguments && !lambdaExpectedTypeIsUnit) { + if (!hasExpressionInReturnArguments) { builder.addSubtypeConstraint( components.session.builtinTypes.unitType.type, - returnTypeRef.type, + substitute(lambda.returnType), ConeLambdaArgumentConstraintPosition(lambda.atom) ) } diff --git a/compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt b/compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt new file mode 100644 index 00000000000..79380b6e9d5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt @@ -0,0 +1,10 @@ +// FIR_IDENTICAL +// DIAGNOSTICS: -FINAL_UPPER_BOUND + +fun fork(task: () -> U) {} + +fun test() { + fork {} + fork { Unit } + fork { 42 } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt index 9d89ed21758..eda4fcd1b63 100644 --- a/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/completion/postponedArgumentsAnalysis/basic.fir.kt @@ -138,8 +138,8 @@ fun main() { * K <: (C) -> Unit -> TypeVariable(_RP1) >: C * K == (B) -> Unit -> TypeVariable(_RP1) == B */ - val x17: (C) -> Unit = selectB(id { it }, id { it }, id<(B) -> Unit> { x -> x }) - val x18: (C) -> Unit = select(id { it }, { it }, id<(B) -> Unit> { x -> x }) + val x17: (C) -> Unit = selectB(id { it }, id { it }, id<(B) -> Unit> { x -> x }) + val x18: (C) -> Unit = select(id { it }, { it }, id<(B) -> Unit> { x -> x }) // Resolution of extension/non-extension functions combination val x19: String.() -> Unit = select(")!>id { this }, ")!>id(fun(x: String) {})) diff --git a/compiler/testData/diagnostics/testsWithStdLib/inference/completion/postponedArgumentsAnalysis/suspendFunctions.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/inference/completion/postponedArgumentsAnalysis/suspendFunctions.fir.kt index 7d960f86839..1520a1d44a4 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/inference/completion/postponedArgumentsAnalysis/suspendFunctions.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/inference/completion/postponedArgumentsAnalysis/suspendFunctions.fir.kt @@ -19,7 +19,7 @@ fun main() { val x1: suspend (Int) -> Unit = takeSuspend(")!>id { it }, ")!>{ x -> x }) // Here, the error should be - val x2: (Int) -> Unit = takeSuspend(")!>id { it }, ")!>{ x -> x }) - val x3: suspend (Int) -> Unit = takeSimpleFunction(")!>id { it }, ")!>{ x -> x }) + val x2: (Int) -> Unit = takeSuspend(")!>id { it }, ")!>{ x -> x }) + val x3: suspend (Int) -> Unit = takeSimpleFunction(")!>id { it }, ")!>{ x -> x }) val x4: (Int) -> Unit = takeSimpleFunction(id Unit> {}, ")!>{}) } 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 7b17c36c944..6bbc8b19137 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 @@ -17058,6 +17058,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inference/underscoredTypeInForbiddenPositions.kt"); } + @Test + @TestMetadata("UnitUpperboundLambdaReturnType.kt") + public void testUnitUpperboundLambdaReturnType() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/UnitUpperboundLambdaReturnType.kt"); + } + @Test @TestMetadata("unsoundness1.kt") public void testUnsoundness1() throws Exception {