From 23071220892dba1f289c3f996663f3115a5b859f Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Thu, 17 Mar 2022 15:49:52 +0400 Subject: [PATCH] [FE 1.0] Report errors for recursive types from completion as well Such diagnostics could be missed at least for plus assignment's right side because it wasn't report for the second time of analysis ^KT-48546 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 24 +++++++++++++++++++ ...irOldFrontendDiagnosticsTestGenerated.java | 12 ++++++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 12 ++++++++++ .../DiagnosticReporterByTrackingStrategy.kt | 1 + .../calls/tasks/AbstractTracingStrategy.java | 7 ++++++ .../resolve/calls/tasks/TracingStrategy.java | 5 ++++ .../ControlStructureTypingUtils.java | 5 ++++ .../calls/components/KotlinCallCompleter.kt | 6 +++++ .../calls/model/KotlinCallDiagnostics.kt | 4 ++++ .../tests/delegatedProperty/kt48546.fir.kt | 17 +++++++++++++ .../tests/delegatedProperty/kt48546.kt | 17 +++++++++++++ .../tests/delegatedProperty/kt48546.txt | 10 ++++++++ .../diagnostics/tests/delegation/kt48546.kt | 2 +- .../testsWithStdLib/coroutines/kt18292.kt | 2 +- .../test/runners/DiagnosticTestGenerated.java | 12 ++++++++++ 15 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/delegatedProperty/kt48546.fir.kt create mode 100644 compiler/testData/diagnostics/tests/delegatedProperty/kt48546.kt create mode 100644 compiler/testData/diagnostics/tests/delegatedProperty/kt48546.txt 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 9005d36bbb3..4fd29a7fa66 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 @@ -8080,6 +8080,18 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt4640.kt"); } + @Test + @TestMetadata("kt48546.kt") + public void testKt48546() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt48546.kt"); + } + + @Test + @TestMetadata("kt48546Strict.kt") + public void testKt48546Strict() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt48546Strict.kt"); + } + @Test @TestMetadata("localVariable.kt") public void testLocalVariable() throws Exception { @@ -8524,6 +8536,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/delegation/kt44843.kt"); } + @Test + @TestMetadata("kt48546.kt") + public void testKt48546() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegation/kt48546.kt"); + } + @Test @TestMetadata("kt49477.kt") public void testKt49477() throws Exception { @@ -25050,6 +25068,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/resolve/inferenceInLinkedLambdasDependentOnExpectedType.kt"); } + @Test + @TestMetadata("kt28109.kt") + public void testKt28109() throws Exception { + runTest("compiler/testData/diagnostics/tests/resolve/kt28109.kt"); + } + @Test @TestMetadata("kt36264.kt") public void testKt36264() 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 d40b993e38b..3b407a850bc 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 @@ -8080,6 +8080,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt4640.kt"); } + @Test + @TestMetadata("kt48546.kt") + public void testKt48546() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt48546.kt"); + } + @Test @TestMetadata("localVariable.kt") public void testLocalVariable() throws Exception { @@ -8524,6 +8530,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/delegation/kt44843.kt"); } + @Test + @TestMetadata("kt48546.kt") + public void testKt48546() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegation/kt48546.kt"); + } + @Test @TestMetadata("kt49477.kt") public void testKt49477() 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 ead36887f9a..883afd29e9d 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 @@ -8080,6 +8080,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt4640.kt"); } + @Test + @TestMetadata("kt48546.kt") + public void testKt48546() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt48546.kt"); + } + @Test @TestMetadata("localVariable.kt") public void testLocalVariable() throws Exception { @@ -8524,6 +8530,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/delegation/kt44843.kt"); } + @Test + @TestMetadata("kt48546.kt") + public void testKt48546() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegation/kt48546.kt"); + } + @Test @TestMetadata("kt49477.kt") public void testKt49477() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt index ad39d0a017d..da6f595c216 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt @@ -75,6 +75,7 @@ class DiagnosticReporterByTrackingStrategy( trace, (diagnostic as NoValueForParameter).parameterDescriptor ) + TypeCheckerHasRanIntoRecursion::class.java -> tracingStrategy.recursiveType(trace) InstantiationOfAbstractClass::class.java -> tracingStrategy.instantiationOfAbstractClass(trace) AbstractSuperCall::class.java -> { val superExpression = (diagnostic as AbstractSuperCall).receiver.psiExpression as? KtSuperExpression diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/AbstractTracingStrategy.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/AbstractTracingStrategy.java index baf7d4f2e29..50a4eda4334 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/AbstractTracingStrategy.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/AbstractTracingStrategy.java @@ -137,6 +137,13 @@ public abstract class AbstractTracingStrategy implements TracingStrategy { trace.report(CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS.on(call.getCallElement())); } + @Override + public void recursiveType(@NotNull BindingTrace trace) { + KtExpression expression = call.getCalleeExpression(); + if (expression == null) return; + trace.report(TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM.getErrorFactory().on(expression)); + } + @Override public void abstractSuperCall(@NotNull BindingTrace trace) { trace.report(ABSTRACT_SUPER_CALL.on(reference)); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TracingStrategy.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TracingStrategy.java index 04ab20a8ee9..2c0fdb98590 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TracingStrategy.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/TracingStrategy.java @@ -81,6 +81,9 @@ public interface TracingStrategy { @NotNull Collection> descriptors ) {} + @Override + public void recursiveType(@NotNull BindingTrace trace) {} + @Override public void instantiationOfAbstractClass(@NotNull BindingTrace trace) {} @@ -144,6 +147,8 @@ public interface TracingStrategy { @NotNull Collection> descriptors ); + void recursiveType(@NotNull BindingTrace trace); + void instantiationOfAbstractClass(@NotNull BindingTrace trace); void abstractSuperCall(@NotNull BindingTrace trace); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingUtils.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingUtils.java index 965dc84a70a..fb554bd9a9b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingUtils.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ControlStructureTypingUtils.java @@ -584,6 +584,11 @@ public class ControlStructureTypingUtils { logError(); } + @Override + public void recursiveType(@NotNull BindingTrace trace) { + logError(); + } + @Override public void unresolvedReferenceWrongReceiver( @NotNull BindingTrace trace, @NotNull Collection> candidates diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt index 014109e5b95..24980f9d24e 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt @@ -26,6 +26,8 @@ import org.jetbrains.kotlin.resolve.calls.tower.forceResolution import org.jetbrains.kotlin.types.error.ErrorUtils import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.UnwrappedType +import org.jetbrains.kotlin.types.error.ErrorType +import org.jetbrains.kotlin.types.error.ErrorTypeKind import org.jetbrains.kotlin.types.model.safeSubstitute import org.jetbrains.kotlin.utils.addToStdlib.same @@ -269,6 +271,10 @@ class KotlinCallCompleter( } constraintSystem.errors.forEach(diagnosticsHolder::addError) + + if (returnType is ErrorType && returnType.kind == ErrorTypeKind.RECURSIVE_TYPE) { + diagnosticsHolder.addDiagnostic(TypeCheckerHasRanIntoRecursion(resolvedCallAtom)) + } } private fun prepareCandidateForCompletion( diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallDiagnostics.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallDiagnostics.kt index ca5051393a6..3b7405c28fe 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallDiagnostics.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallDiagnostics.kt @@ -116,6 +116,10 @@ class WrongCountOfTypeArguments( override fun report(reporter: DiagnosticReporter) = reporter.onTypeArguments(this) } +class TypeCheckerHasRanIntoRecursion : KotlinCallDiagnostic(INAPPLICABLE) { + override fun report(reporter: DiagnosticReporter) = reporter.onCall(this) +} + // Callable reference resolution class CallableReferenceNotCompatible( argument: CallableReferenceResolutionAtom, diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.fir.kt new file mode 100644 index 00000000000..80c0827bf9e --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.fir.kt @@ -0,0 +1,17 @@ +// WITH_STDLIB + +object DelegateTest { + var result = "" + val f by lazy { + result += f.toString() // Compiler crash + "hello" + } +} + +object DelegateTest2 { + var result = "" + val f by lazy { + result += f + "hello" + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.kt b/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.kt new file mode 100644 index 00000000000..0b92c03151f --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.kt @@ -0,0 +1,17 @@ +// WITH_STDLIB + +object DelegateTest { + var result = "" + val f by lazy { + result += f.toString() // Compiler crash + "hello" + } +} + +object DelegateTest2 { + var result = "" + val f by lazy { + result += f + "hello" + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.txt b/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.txt new file mode 100644 index 00000000000..62ba3e9515a --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/kt48546.txt @@ -0,0 +1,10 @@ +package + +public object DelegateTest { + private constructor DelegateTest() + public final val f: kotlin.String + public final var result: kotlin.String + 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/delegation/kt48546.kt b/compiler/testData/diagnostics/tests/delegation/kt48546.kt index 8f075c4176c..6e266489d06 100644 --- a/compiler/testData/diagnostics/tests/delegation/kt48546.kt +++ b/compiler/testData/diagnostics/tests/delegation/kt48546.kt @@ -3,7 +3,7 @@ object DelegateTest { var result = "" val f by lazy { - result += f.toString() // Compiler crash + result += f.toString() // Compiler crash "hello" } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/kt18292.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/kt18292.kt index 2a93b60c16f..a236d190ec6 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/kt18292.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/kt18292.kt @@ -13,6 +13,6 @@ suspend fun fib(n: Long) = async { when { n < 2 -> n - else -> fib(n - 1).await() + fib(n - 2).await() + else -> fib(n - 1).await() + fib(n - 2).await() } } 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 5b82e83e073..07f387c03b0 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 @@ -8086,6 +8086,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt4640.kt"); } + @Test + @TestMetadata("kt48546.kt") + public void testKt48546() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/kt48546.kt"); + } + @Test @TestMetadata("localVariable.kt") public void testLocalVariable() throws Exception { @@ -8530,6 +8536,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/delegation/kt44843.kt"); } + @Test + @TestMetadata("kt48546.kt") + public void testKt48546() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegation/kt48546.kt"); + } + @Test @TestMetadata("kt49477.kt") public void testKt49477() throws Exception {