From cd09c8ba51a42c9d553dd239c0fc7c9196133542 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Fri, 6 Aug 2021 10:13:54 +0300 Subject: [PATCH] Report `CANNOT_INFER_PARAMETER_TYPE` on any error value parameters of a lambda ^KT-48058 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 6 +++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++++ .../resolve/FunctionDescriptorResolver.kt | 7 +++--- .../tests/functionLiterals/kt47493.fir.kt | 23 +++++++++++++++++++ .../tests/functionLiterals/kt47493.kt | 23 +++++++++++++++++++ .../tests/functionLiterals/kt47493.txt | 5 ++++ ...ompleteResolveFunctionLiteralsNoUse.fir.kt | 6 +++++ ...notCompleteResolveFunctionLiteralsNoUse.kt | 3 +-- ...CompleteResolveWithFunctionLiterals.fir.kt | 9 ++++++++ ...nnotCompleteResolveWithFunctionLiterals.kt | 3 +-- .../cannotInferParameterTypeWithInference.kt | 2 +- .../resolve/overloadConflicts/allLambdas.kt | 2 +- .../test/runners/DiagnosticTestGenerated.java | 6 +++++ ...CompilerTestFE10TestdataTestGenerated.java | 6 +++++ 14 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/kt47493.fir.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/kt47493.kt create mode 100644 compiler/testData/diagnostics/tests/functionLiterals/kt47493.txt create mode 100644 compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.fir.kt 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 6281b9375b9..2788b3e472f 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 @@ -10705,6 +10705,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/functionLiterals/kt4529.kt"); } + @Test + @TestMetadata("kt47493.kt") + public void testKt47493() throws Exception { + runTest("compiler/testData/diagnostics/tests/functionLiterals/kt47493.kt"); + } + @Test @TestMetadata("kt6541_extensionForExtensionFunction.kt") public void testKt6541_extensionForExtensionFunction() 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 864df50edbd..d2f1c32df88 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 @@ -10705,6 +10705,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/functionLiterals/kt4529.kt"); } + @Test + @TestMetadata("kt47493.kt") + public void testKt47493() throws Exception { + runTest("compiler/testData/diagnostics/tests/functionLiterals/kt47493.kt"); + } + @Test @TestMetadata("kt6541_extensionForExtensionFunction.kt") public void testKt6541_extensionForExtensionFunction() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/FunctionDescriptorResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/FunctionDescriptorResolver.kt index d305f15004e..fce68e65772 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/FunctionDescriptorResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/FunctionDescriptorResolver.kt @@ -65,6 +65,7 @@ import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils.isFunctionExpression import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils.isFunctionLiteral +import org.jetbrains.kotlin.types.isError import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations import java.util.* @@ -446,10 +447,8 @@ class FunctionDescriptorResolver( } } else { type = if (isFunctionLiteral(functionDescriptor) || isFunctionExpression(functionDescriptor)) { - val containsUninferredParameter = TypeUtils.contains(expectedType) { - TypeUtils.isDontCarePlaceholder(it) || ErrorUtils.isUninferredParameter(it) - } - if (expectedType == null || containsUninferredParameter) { + val containsErrorType = TypeUtils.contains(expectedType) { it.isError } + if (expectedType == null || containsErrorType) { trace.report(CANNOT_INFER_PARAMETER_TYPE.on(valueParameter)) } diff --git a/compiler/testData/diagnostics/tests/functionLiterals/kt47493.fir.kt b/compiler/testData/diagnostics/tests/functionLiterals/kt47493.fir.kt new file mode 100644 index 00000000000..b92c4e7057e --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/kt47493.fir.kt @@ -0,0 +1,23 @@ +fun test1() { + try { + { toDouble -> + } + } catch (e: Exception) { + + } +} + +fun test2() { + try { + + } catch (e: Exception) { + { toDouble -> + } + } +} + +fun box(): String { + test1() + test2() + return "OK" +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/kt47493.kt b/compiler/testData/diagnostics/tests/functionLiterals/kt47493.kt new file mode 100644 index 00000000000..24aee361d1e --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/kt47493.kt @@ -0,0 +1,23 @@ +fun test1() { + try { + { toDouble -> + } + } catch (e: Exception) { + + } +} + +fun test2() { + try { + + } catch (e: Exception) { + { toDouble -> + } + } +} + +fun box(): String { + test1() + test2() + return "OK" +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/kt47493.txt b/compiler/testData/diagnostics/tests/functionLiterals/kt47493.txt new file mode 100644 index 00000000000..2bb951ebf07 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/kt47493.txt @@ -0,0 +1,5 @@ +package + +public fun box(): kotlin.String +public fun test1(): kotlin.Unit +public fun test2(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.fir.kt b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.fir.kt new file mode 100644 index 00000000000..c90d9f55e36 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.fir.kt @@ -0,0 +1,6 @@ +package f + +fun h(i: Int, a: Any, r: R, f: (Boolean) -> Int) = 1 +fun h(a: Any, i: Int, r: R, f: (Boolean) -> Int) = 1 + +fun test() = h(1, 1, 1, { b -> 42 }) diff --git a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.kt b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.kt index 5609e7b262e..8978c920a84 100644 --- a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.kt +++ b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveFunctionLiteralsNoUse.kt @@ -1,7 +1,6 @@ -// FIR_IDENTICAL package f fun h(i: Int, a: Any, r: R, f: (Boolean) -> Int) = 1 fun h(a: Any, i: Int, r: R, f: (Boolean) -> Int) = 1 -fun test() = h(1, 1, 1, { b -> 42 }) +fun test() = h(1, 1, 1, { b -> 42 }) diff --git a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.fir.kt b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.fir.kt new file mode 100644 index 00000000000..4f7d4791c23 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.fir.kt @@ -0,0 +1,9 @@ +// !DIAGNOSTICS: -CONFLICTING_JVM_DECLARATIONS +package f + +fun h(f: (Boolean) -> R) = 1 +fun h(f: (String) -> R) = 2 + +fun test() = h{ i -> getAnswer() } + +fun getAnswer() = 42 diff --git a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.kt b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.kt index 5900439683f..c630f37b25b 100644 --- a/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.kt +++ b/compiler/testData/diagnostics/tests/inference/cannotCompleteResolveWithFunctionLiterals.kt @@ -1,10 +1,9 @@ -// FIR_IDENTICAL // !DIAGNOSTICS: -CONFLICTING_JVM_DECLARATIONS package f fun h(f: (Boolean) -> R) = 1 fun h(f: (String) -> R) = 2 -fun test() = h{ i -> getAnswer() } +fun test() = h{ i -> getAnswer() } fun getAnswer() = 42 diff --git a/compiler/testData/diagnostics/tests/inference/reportingImprovements/cannotInferParameterTypeWithInference.kt b/compiler/testData/diagnostics/tests/inference/reportingImprovements/cannotInferParameterTypeWithInference.kt index 6f8d089028f..f22e8b83089 100644 --- a/compiler/testData/diagnostics/tests/inference/reportingImprovements/cannotInferParameterTypeWithInference.kt +++ b/compiler/testData/diagnostics/tests/inference/reportingImprovements/cannotInferParameterTypeWithInference.kt @@ -12,5 +12,5 @@ fun test1() { fun bar(f: (A)->Unit) {} fun test2() { - bar { a -> } // here we don't have 'cannot infer parameter type' error + bar { a -> } // here we don't have 'cannot infer parameter type' error } diff --git a/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.kt b/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.kt index dfd344a41c7..fc64260ac88 100644 --- a/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.kt +++ b/compiler/testData/diagnostics/tests/resolve/overloadConflicts/allLambdas.kt @@ -6,4 +6,4 @@ object X2 fun foo(x: T1, f: (T1) -> T1) = X1 fun foo(xf: () -> T2, f: (T2) -> T2) = X2 -val test: X2 = foo({ 0 }, { it -> it + 1 }) +val test: X2 = foo({ 0 }, { it -> it + 1 }) 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 696cdec20c4..e726ac8d227 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 @@ -10711,6 +10711,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/functionLiterals/kt4529.kt"); } + @Test + @TestMetadata("kt47493.kt") + public void testKt47493() throws Exception { + runTest("compiler/testData/diagnostics/tests/functionLiterals/kt47493.kt"); + } + @Test @TestMetadata("kt6541_extensionForExtensionFunction.kt") public void testKt6541_extensionForExtensionFunction() throws Exception { diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index e0bbda5c650..eb28f1861fd 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -10705,6 +10705,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/functionLiterals/kt4529.kt"); } + @Test + @TestMetadata("kt47493.kt") + public void testKt47493() throws Exception { + runTest("compiler/testData/diagnostics/tests/functionLiterals/kt47493.kt"); + } + @Test @TestMetadata("kt6541_extensionForExtensionFunction.kt") public void testKt6541_extensionForExtensionFunction() throws Exception {