From ee7e04b28edf852bebd611d78bbe3ac3563bfc32 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Tue, 14 Dec 2021 23:55:51 +0300 Subject: [PATCH] [FIR] Ignore smartcast for non-final members from Java, ^KT-50219 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...irOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ .../fir/resolve/dfa/FirDataFlowAnalyzer.kt | 1 + .../fir/resolve/dfa/VariableStorageImpl.kt | 2 ++ .../noSmartcastForNonFinalMembersFromJava.kt | 21 +++++++++++++++++++ .../noSmartcastForNonFinalMembersFromJava.txt | 20 ++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ 8 files changed, 68 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.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 f832d6a1b52..3b38e317fe0 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 @@ -27987,6 +27987,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/smartCasts/noErrorCheckForPackageLevelVal.kt"); } + @Test + @TestMetadata("noSmartcastForNonFinalMembersFromJava.kt") + public void testNoSmartcastForNonFinalMembersFromJava() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.kt"); + } + @Test @TestMetadata("noUnnecessarySmartCastForReceiver.kt") public void testNoUnnecessarySmartCastForReceiver() 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 ff9c349f6c4..c9352a4a891 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 @@ -27987,6 +27987,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/smartCasts/noErrorCheckForPackageLevelVal.kt"); } + @Test + @TestMetadata("noSmartcastForNonFinalMembersFromJava.kt") + public void testNoSmartcastForNonFinalMembersFromJava() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.kt"); + } + @Test @TestMetadata("noUnnecessarySmartCastForReceiver.kt") public void testNoUnnecessarySmartCastForReceiver() 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 f7d82e0ce03..35ce6b44a73 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 @@ -27987,6 +27987,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/smartCasts/noErrorCheckForPackageLevelVal.kt"); } + @Test + @TestMetadata("noSmartcastForNonFinalMembersFromJava.kt") + public void testNoSmartcastForNonFinalMembersFromJava() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.kt"); + } + @Test @TestMetadata("noUnnecessarySmartCastForReceiver.kt") public void testNoUnnecessarySmartCastForReceiver() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt index def2cf8e33b..1aad2c184ad 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.fir.contracts.description.ConeConstantReference import org.jetbrains.kotlin.fir.contracts.description.ConeReturnsEffectDeclaration import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor +import org.jetbrains.kotlin.fir.declarations.utils.isFinal import org.jetbrains.kotlin.fir.declarations.utils.isLocal import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.references.FirControlFlowGraphReference diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/VariableStorageImpl.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/VariableStorageImpl.kt index 700b943ae5c..e59d34c9505 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/VariableStorageImpl.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/VariableStorageImpl.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.fir.declarations.FirAnonymousObject import org.jetbrains.kotlin.fir.declarations.FirProperty import org.jetbrains.kotlin.fir.declarations.FirRegularClass import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor +import org.jetbrains.kotlin.fir.declarations.utils.isFinal import org.jetbrains.kotlin.fir.declarations.utils.modality import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.impl.FirNoReceiverExpression @@ -179,6 +180,7 @@ class VariableStorageImpl(private val session: FirSession) : VariableStorage() { } if (originalFir is FirThisReceiverExpression) return PropertyStability.STABLE_VALUE if (this !is FirVariableSymbol<*>) return null + if (this is FirFieldSymbol && !this.isFinal) return PropertyStability.MUTABLE_PROPERTY val property = this.fir as? FirProperty ?: return PropertyStability.STABLE_VALUE diff --git a/compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.kt b/compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.kt new file mode 100644 index 00000000000..695df590dfa --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.kt @@ -0,0 +1,21 @@ +// FIR_IDENTICAL +// KT-50219 + +// FILE: Base.java +public class Base { + protected String field; + protected final String finalField = "finalFieldValue"; +} + +// FILE: test.kt +class Derived : Base() { + fun foo() { + if (field == null) { + field.length + } + + if (finalField == null) { + finalField.length + } + } +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.txt b/compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.txt new file mode 100644 index 00000000000..05572323268 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.txt @@ -0,0 +1,20 @@ +package + +public open class Base { + public constructor Base() + protected/*protected and package*/ final var field: kotlin.String! + protected/*protected and package*/ final val finalField: kotlin.String! = "finalFieldValue" + 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 +} + +public final class Derived : Base { + public constructor Derived() + protected/*protected and package*/ final override /*1*/ /*fake_override*/ var field: kotlin.String! + protected/*protected and package*/ final override /*1*/ /*fake_override*/ val finalField: kotlin.String! + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun foo(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} 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 94671802e87..208b8718fd9 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 @@ -28077,6 +28077,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/smartCasts/noErrorCheckForPackageLevelVal.kt"); } + @Test + @TestMetadata("noSmartcastForNonFinalMembersFromJava.kt") + public void testNoSmartcastForNonFinalMembersFromJava() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/noSmartcastForNonFinalMembersFromJava.kt"); + } + @Test @TestMetadata("noUnnecessarySmartCastForReceiver.kt") public void testNoUnnecessarySmartCastForReceiver() throws Exception {