From 94c94d0605e9531518a9df7b7a0d93d1aec82d45 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 26 Apr 2023 15:21:13 +0200 Subject: [PATCH] K1: add a language feature to negate new errors from DiagnosticReporterByTrackingStrategy Related to KT-55055, KT-55056, KT-55079, KT-57854 --- ...CompilerTestFE10TestdataTestGenerated.java | 12 ++++++ ...sticCompilerFE10TestDataTestGenerated.java | 12 ++++++ ...eeOldFrontendDiagnosticsTestGenerated.java | 12 ++++++ ...siOldFrontendDiagnosticsTestGenerated.java | 12 ++++++ .../DiagnosticReporterByTrackingStrategy.kt | 39 +++++++++++++------ .../inconsistentTypeInference_noReporting.kt | 9 +++++ .../upperBoundViolation_noReporting.kt | 11 ++++++ .../test/runners/DiagnosticTestGenerated.java | 12 ++++++ .../kotlin/config/LanguageVersionSettings.kt | 7 ++++ 9 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt create mode 100644 compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.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 938733a467e..c2c6cff3312 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 @@ -36736,6 +36736,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt"); } + @Test + @TestMetadata("inconsistentTypeInference_noReporting.kt") + public void testInconsistentTypeInference_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt"); + } + @Test @TestMetadata("incorrectCalls.kt") public void testIncorrectCalls() throws Exception { @@ -36808,6 +36814,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation.kt"); } + @Test + @TestMetadata("upperBoundViolation_noReporting.kt") + public void testUpperBoundViolation_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.kt"); + } + @Test @TestMetadata("useInferenceInformationFromExtension.kt") public void testUseInferenceInformationFromExtension() 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 8f8fca13681..e9114c2ae6c 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 @@ -36736,6 +36736,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt"); } + @Test + @TestMetadata("inconsistentTypeInference_noReporting.kt") + public void testInconsistentTypeInference_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt"); + } + @Test @TestMetadata("incorrectCalls.kt") public void testIncorrectCalls() throws Exception { @@ -36808,6 +36814,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation.kt"); } + @Test + @TestMetadata("upperBoundViolation_noReporting.kt") + public void testUpperBoundViolation_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.kt"); + } + @Test @TestMetadata("useInferenceInformationFromExtension.kt") public void testUseInferenceInformationFromExtension() 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 f1dd144470f..f7578886639 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 @@ -36736,6 +36736,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt"); } + @Test + @TestMetadata("inconsistentTypeInference_noReporting.kt") + public void testInconsistentTypeInference_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt"); + } + @Test @TestMetadata("incorrectCalls.kt") public void testIncorrectCalls() throws Exception { @@ -36808,6 +36814,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation.kt"); } + @Test + @TestMetadata("upperBoundViolation_noReporting.kt") + public void testUpperBoundViolation_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.kt"); + } + @Test @TestMetadata("useInferenceInformationFromExtension.kt") public void testUseInferenceInformationFromExtension() 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 b18f4c319c3..8c1555d842b 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 @@ -36832,6 +36832,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt"); } + @Test + @TestMetadata("inconsistentTypeInference_noReporting.kt") + public void testInconsistentTypeInference_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt"); + } + @Test @TestMetadata("incorrectCalls.kt") public void testIncorrectCalls() throws Exception { @@ -36904,6 +36910,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation.kt"); } + @Test + @TestMetadata("upperBoundViolation_noReporting.kt") + public void testUpperBoundViolation_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.kt"); + } + @Test @TestMetadata("useInferenceInformationFromExtension.kt") public void testUseInferenceInformationFromExtension() 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 6faff3e14fb..ef93e25d28e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/DiagnosticReporterByTrackingStrategy.kt @@ -185,7 +185,7 @@ class DiagnosticReporterByTrackingStrategy( private fun unknownError(diagnostic: KotlinCallDiagnostic, onTarget: String) { if (AbstractTypeChecker.RUN_SLOW_ASSERTIONS) { throw AssertionError("$onTarget should not be called with ${diagnostic::class.java}") - } else { + } else if (reportAdditionalErrors) { trace.report( NEW_INFERENCE_UNKNOWN_ERROR.on( psiKotlinCall.psiCall.callElement, @@ -567,15 +567,17 @@ class DiagnosticReporterByTrackingStrategy( val originalCall = (position as DeclaredUpperBoundConstraintPositionImpl).kotlinCall val typeParameterDescriptor = position.typeParameter val ownerDescriptor = typeParameterDescriptor.containingDeclaration - trace.reportDiagnosticOnce( - UPPER_BOUND_VIOLATION_IN_CONSTRAINT.on( - (originalCall as PSIKotlinCall).psiCall.callElement, - typeParameterDescriptor.name, - ownerDescriptor.name, - error.upperKotlinType, - error.lowerKotlinType + if (reportAdditionalErrors) { + trace.reportDiagnosticOnce( + UPPER_BOUND_VIOLATION_IN_CONSTRAINT.on( + (originalCall as PSIKotlinCall).psiCall.callElement, + typeParameterDescriptor.name, + ownerDescriptor.name, + error.upperKotlinType, + error.lowerKotlinType + ) ) - ) + } } is DelegatedPropertyConstraintPosition<*> -> { // DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE, reported later @@ -590,7 +592,7 @@ class DiagnosticReporterByTrackingStrategy( SimpleConstraintSystemConstraintPosition -> { if (AbstractTypeChecker.RUN_SLOW_ASSERTIONS) { throw AssertionError("Constraint error in unexpected position: $position") - } else { + } else if (reportAdditionalErrors) { report( TYPE_MISMATCH_IN_CONSTRAINT.on( psiKotlinCall.psiCall.callElement, @@ -629,7 +631,11 @@ class DiagnosticReporterByTrackingStrategy( val expression = argument.psiExpression ?: run { val psiCall = (selectorCall as? PSIKotlinCall)?.psiCall ?: psiKotlinCall.psiCall - if (context.languageVersionSettings.supportsFeature(LanguageFeature.ProperTypeInferenceConstraintsProcessing)) { + // Note: we don't report RECEIVER_TYPE_MISMATCH w/out ProperTypeInferenceConstraintsProcessing + // See KT-57854. This is needed for intellij.go.tests (recursive generics case) compilation with K1 + if (context.languageVersionSettings.supportsFeature(LanguageFeature.ProperTypeInferenceConstraintsProcessing) && + reportAdditionalErrors + ) { report( RECEIVER_TYPE_MISMATCH.on( psiCall.calleeExpression ?: psiCall.callElement, error.upperKotlinType, error.lowerKotlinType @@ -657,6 +663,17 @@ class DiagnosticReporterByTrackingStrategy( report(typeMismatchDiagnostic.on(deparenthesized, error.upperKotlinType, error.lowerKotlinType)) } + /** + * Should we report additional errors appeared in Kotlin compiler 1.9.0 or not + * + * This property appeared in Kotlin compiler 1.9.0, after we discovered some "swallowed" diagnostics in this class. + * We added a set of diagnostics (see property usages) to have additional protection before migrating to K2. + * For details see KT-55055, KT-55056, KT-55079. + * This property is normally true, but can be disabled with a feature NoAdditionalErrorsInK1DiagnosticReporter + */ + private val reportAdditionalErrors: Boolean + get() = !context.languageVersionSettings.supportsFeature(LanguageFeature.NoAdditionalErrorsInK1DiagnosticReporter) + override fun constraintError(error: ConstraintSystemError) { when (error) { is NewConstraintMismatch -> reportConstraintErrorByPosition(error, error.position.from) diff --git a/compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt b/compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt new file mode 100644 index 00000000000..0c9f8f5d225 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt @@ -0,0 +1,9 @@ +// FIR_IDENTICAL +// !LANGUAGE: +NoAdditionalErrorsInK1DiagnosticReporter + +fun foo() { + buildList { + add("Boom") + println(plus(1)[0]) + } +} diff --git a/compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.kt b/compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.kt new file mode 100644 index 00000000000..787db971da7 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.kt @@ -0,0 +1,11 @@ +// FIR_IDENTICAL +// !LANGUAGE: +NoAdditionalErrorsInK1DiagnosticReporter +// ISSUE: KT-55055 +fun printGenericNumber(t: T) = println("Number is $t") + +fun main() { + buildList { // inferred into MutableList + add("Boom") + printGenericNumber(this[0]) + } +} 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 b83154bc6a4..91e9e6fadc5 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 @@ -37604,6 +37604,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference2.kt"); } + @Test + @TestMetadata("inconsistentTypeInference_noReporting.kt") + public void testInconsistentTypeInference_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/inconsistentTypeInference_noReporting.kt"); + } + @Test @TestMetadata("incorrectCalls.kt") public void testIncorrectCalls() throws Exception { @@ -37676,6 +37682,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation.kt"); } + @Test + @TestMetadata("upperBoundViolation_noReporting.kt") + public void testUpperBoundViolation_noReporting() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/builderInference/upperBoundViolation_noReporting.kt"); + } + @Test @TestMetadata("useInferenceInformationFromExtension.kt") public void testUseInferenceInformationFromExtension() throws Exception { diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 1b5917b019e..e83a7b7a07c 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -313,6 +313,13 @@ enum class LanguageFeature( // equality & identity operators in cases where K1 would report warnings or would not report anything. ReportErrorsForComparisonOperators(sinceVersion = null, kind = BUG_FIX), + // Disabled for indefinite time. + // Disables reporting of new errors (see KT-55055, KT-55056, KT-55079) in DiagnosticReporterByTrackingStrategy. + // All these errors are "lost" errors which existed always, but wasn't reported before 1.9.0. + // When this feature is disabled, all these "lost" errors are reported properly. + // When this feature is enabled, no such errors are reported. + NoAdditionalErrorsInK1DiagnosticReporter(sinceVersion = null, kind = OTHER), + // Experimental features BreakContinueInInlineLambdas(null), // KT-1436