From 57908eba77850f1f8ddf93ada4afcc8555fab0c6 Mon Sep 17 00:00:00 2001 From: Nikolay Lunyak Date: Tue, 25 Jul 2023 13:16:37 +0000 Subject: [PATCH] [FIR] Prevent replacing UNNECESSARY_SAFE_CALL with UNEXPECTED_SAFE_CALL UNNECESSARY_SAFE_CALL is a warning, UNEXPECTED_SAFE_CALL is an error, thus it's a breaking change. Also see KT-60695. ^KT-59860 Fixed Merge-request: KT-MR-11210 Merged-by: Nikolay Lunyak --- .../DiagnosticCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...versedDiagnosticCompilerFE10TestDataTestGenerated.java | 6 ++++++ .../FirLightTreeOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../FirPsiOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../checkers/expression/FirUnnecessarySafeCallChecker.kt | 7 ++++++- .../diagnostics/tests/SafeCallOnFakePackage.fir.kt | 6 +++--- .../safeCallAfterVariableInitialization.fir.kt | 2 +- compiler/testData/diagnostics/tests/unexpectedSafeCall.kt | 8 ++++++++ .../testsWithStdLib/resolve/javaStaticMembers.fir.kt | 4 ++-- .../kotlin/test/runners/DiagnosticTestGenerated.java | 6 ++++++ 10 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/unexpectedSafeCall.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 20255b6e40c..ae708b7d994 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 @@ -1245,6 +1245,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt"); } + @Test + @TestMetadata("unexpectedSafeCall.kt") + public void testUnexpectedSafeCall() throws Exception { + runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt"); + } + @Test @TestMetadata("UnitByDefaultForFunctionTypes.kt") public void testUnitByDefaultForFunctionTypes() 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 c700271ba0c..b4968f3f3c6 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 @@ -1245,6 +1245,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt"); } + @Test + @TestMetadata("unexpectedSafeCall.kt") + public void testUnexpectedSafeCall() throws Exception { + runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt"); + } + @Test @TestMetadata("UnitByDefaultForFunctionTypes.kt") public void testUnitByDefaultForFunctionTypes() 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 05035c484ad..26278132bea 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 @@ -1245,6 +1245,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt"); } + @Test + @TestMetadata("unexpectedSafeCall.kt") + public void testUnexpectedSafeCall() throws Exception { + runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt"); + } + @Test @TestMetadata("UnitByDefaultForFunctionTypes.kt") public void testUnitByDefaultForFunctionTypes() 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 37a0848291a..6a4a9ffe1af 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 @@ -1245,6 +1245,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt"); } + @Test + @TestMetadata("unexpectedSafeCall.kt") + public void testUnexpectedSafeCall() throws Exception { + runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt"); + } + @Test @TestMetadata("UnitByDefaultForFunctionTypes.kt") public void testUnitByDefaultForFunctionTypes() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUnnecessarySafeCallChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUnnecessarySafeCallChecker.kt index 246ad63df73..6f34ff2ed9f 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUnnecessarySafeCallChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirUnnecessarySafeCallChecker.kt @@ -12,8 +12,13 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticReporter import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors import org.jetbrains.kotlin.diagnostics.reportOn import org.jetbrains.kotlin.fir.expressions.FirSafeCallExpression +import org.jetbrains.kotlin.fir.expressions.calleeReference import org.jetbrains.kotlin.fir.languageVersionSettings +import org.jetbrains.kotlin.fir.references.resolved +import org.jetbrains.kotlin.fir.references.toResolvedCallableSymbol import org.jetbrains.kotlin.fir.resolve.fullyExpandedType +import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol +import org.jetbrains.kotlin.fir.types.ConeErrorType import org.jetbrains.kotlin.fir.types.canBeNull import org.jetbrains.kotlin.fir.types.coneType import org.jetbrains.kotlin.fir.types.isUnit @@ -21,7 +26,7 @@ import org.jetbrains.kotlin.fir.types.isUnit object FirUnnecessarySafeCallChecker : FirSafeCallExpressionChecker() { override fun check(expression: FirSafeCallExpression, context: CheckerContext, reporter: DiagnosticReporter) { val receiverType = expression.receiver.typeRef.coneType.fullyExpandedType(context.session) - if (receiverType.isUnit || expression.receiver.source?.elementType == KtNodeTypes.SUPER_EXPRESSION) { + if (expression.receiver.source?.elementType == KtNodeTypes.SUPER_EXPRESSION) { reporter.reportOn(expression.source, FirErrors.UNEXPECTED_SAFE_CALL, context) return } diff --git a/compiler/testData/diagnostics/tests/SafeCallOnFakePackage.fir.kt b/compiler/testData/diagnostics/tests/SafeCallOnFakePackage.fir.kt index bb6402ecbb8..8ce0b47dbe0 100644 --- a/compiler/testData/diagnostics/tests/SafeCallOnFakePackage.fir.kt +++ b/compiler/testData/diagnostics/tests/SafeCallOnFakePackage.fir.kt @@ -10,7 +10,7 @@ val s: String = "test" // FILE: test.kt fun ff() { - val a = Test?.FOO - val b = foo?.s - System?.out.println(a + b) + val a = Test?.FOO + val b = foo?.s + System?.out.println(a + b) } diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/safeCallAfterVariableInitialization.fir.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/safeCallAfterVariableInitialization.fir.kt index 3dac965ae1a..72a59471e21 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/safeCallAfterVariableInitialization.fir.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/safeCallAfterVariableInitialization.fir.kt @@ -2,6 +2,6 @@ fun test() { val b: Int - run { b = 1 }?.let {} + run { b = 1 }?.let {} b.inc() } diff --git a/compiler/testData/diagnostics/tests/unexpectedSafeCall.kt b/compiler/testData/diagnostics/tests/unexpectedSafeCall.kt new file mode 100644 index 00000000000..296fb10536c --- /dev/null +++ b/compiler/testData/diagnostics/tests/unexpectedSafeCall.kt @@ -0,0 +1,8 @@ +// FIR_IDENTICAL +// DIAGNOSTICS: -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE +// ISSUE: KT-59860 + +fun test() { + val b: Int + run { b = 1 }?.let {} // K1: UNNECESSARY_SAFE_CALL, K2: UNEXPECTED_SAFE_CALL +} diff --git a/compiler/testData/diagnostics/testsWithStdLib/resolve/javaStaticMembers.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/resolve/javaStaticMembers.fir.kt index 3bfaa40a735..a341c52e10c 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/resolve/javaStaticMembers.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/resolve/javaStaticMembers.fir.kt @@ -6,7 +6,7 @@ public class Test { // FILE: test.kt fun ff() { val a = Test.FOO - val b = Test?.FOO + val b = Test?.FOO System.out.println(a + b) - System?.out.println(a + b) + System?.out.println(a + b) } 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 8c2f61fb2a7..090ed47075f 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 @@ -1245,6 +1245,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/UnderscoreUsageInVariableAsFunctionCall.kt"); } + @Test + @TestMetadata("unexpectedSafeCall.kt") + public void testUnexpectedSafeCall() throws Exception { + runTest("compiler/testData/diagnostics/tests/unexpectedSafeCall.kt"); + } + @Test @TestMetadata("UnitByDefaultForFunctionTypes.kt") public void testUnitByDefaultForFunctionTypes() throws Exception {