From bb6f466162a6ba6a4bb816adc02ebcda26182e1f Mon Sep 17 00:00:00 2001 From: Brian Norman Date: Fri, 26 Jan 2024 13:55:18 -0600 Subject: [PATCH] [FIR] Do not smartcast class delegation implemented properties When a property is implemented via class delegation, it should be considered unstable for smart-casting. This is because it is unknown what kind of stability the underlying class delegate property has. It could be a stable property, or it could be implemented via a custom getter and unstable. ^KT-57417 Fixed --- ...agnosticCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...edDiagnosticCompilerFE10TestDataTestGenerated.java | 6 ++++++ ...rLightTreeOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../FirPsiOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../kotlin/fir/resolve/dfa/VariableStorageImpl.kt | 7 ++----- .../smartCasts/varnotnull/classDelegation.fir.kt | 11 +++++++++++ .../tests/smartCasts/varnotnull/classDelegation.kt | 11 +++++++++++ .../kotlin/test/runners/DiagnosticTestGenerated.java | 6 ++++++ 8 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.fir.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.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 49ec7f23267..18672bba892 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 @@ -36967,6 +36967,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/capturedInlineModifiedBefore.kt"); } + @Test + @TestMetadata("classDelegation.kt") + public void testClassDelegation() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.kt"); + } + @Test @TestMetadata("doWhileWithBreak.kt") public void testDoWhileWithBreak() 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 ac33d9f23f9..68990d48834 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 @@ -36967,6 +36967,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/capturedInlineModifiedBefore.kt"); } + @Test + @TestMetadata("classDelegation.kt") + public void testClassDelegation() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.kt"); + } + @Test @TestMetadata("doWhileWithBreak.kt") public void testDoWhileWithBreak() 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 91f85bbfbc4..e6cdb4445e6 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 @@ -34635,6 +34635,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/capturedInlineModifiedBefore.kt"); } + @Test + @TestMetadata("classDelegation.kt") + public void testClassDelegation() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.kt"); + } + @Test @TestMetadata("doWhileWithBreak.kt") public void testDoWhileWithBreak() 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 cfbe1730ad0..8041ddbbd3c 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 @@ -34761,6 +34761,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/capturedInlineModifiedBefore.kt"); } + @Test + @TestMetadata("classDelegation.kt") + public void testClassDelegation() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.kt"); + } + @Test @TestMetadata("doWhileWithBreak.kt") public void testDoWhileWithBreak() throws Exception { 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 497a02d0a0a..f1a616e275d 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 @@ -7,8 +7,7 @@ package org.jetbrains.kotlin.fir.resolve.dfa import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Visibilities -import org.jetbrains.kotlin.fir.FirElement -import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.declarations.FirAnonymousObject import org.jetbrains.kotlin.fir.declarations.FirProperty import org.jetbrains.kotlin.fir.declarations.FirRegularClass @@ -18,8 +17,6 @@ import org.jetbrains.kotlin.fir.declarations.utils.isFinal import org.jetbrains.kotlin.fir.declarations.utils.modality import org.jetbrains.kotlin.fir.declarations.utils.visibility import org.jetbrains.kotlin.fir.expressions.* -import org.jetbrains.kotlin.fir.moduleData -import org.jetbrains.kotlin.fir.originalOrSelf import org.jetbrains.kotlin.fir.references.FirThisReference import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.toSymbol @@ -173,7 +170,7 @@ class VariableStorageImpl(private val session: FirSession) : VariableStorage() { val property = this.fir as? FirProperty ?: return PropertyStability.STABLE_VALUE return when { - property.delegate != null -> PropertyStability.DELEGATED_PROPERTY + property.delegate != null || property.isDelegated -> PropertyStability.DELEGATED_PROPERTY property.isLocal -> if (property.isVal) PropertyStability.STABLE_VALUE else PropertyStability.LOCAL_VAR property.isVar -> PropertyStability.MUTABLE_PROPERTY property.receiverParameter != null -> PropertyStability.PROPERTY_WITH_GETTER diff --git a/compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.fir.kt new file mode 100644 index 00000000000..1274772571d --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.fir.kt @@ -0,0 +1,11 @@ +// ISSUE: KT-57417 + +interface HasProperty { + val property: Int? +} + +class Test(delegate: HasProperty) : HasProperty by delegate + +fun test(a: Test) { + if (a.property != null) a.property + 1 +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.kt b/compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.kt new file mode 100644 index 00000000000..fd62222348b --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.kt @@ -0,0 +1,11 @@ +// ISSUE: KT-57417 + +interface HasProperty { + val property: Int? +} + +class Test(delegate: HasProperty) : HasProperty by delegate + +fun test(a: Test) { + if (a.property != null) a.property + 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 95870d18c6e..1e055a12758 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 @@ -36967,6 +36967,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/capturedInlineModifiedBefore.kt"); } + @Test + @TestMetadata("classDelegation.kt") + public void testClassDelegation() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/varnotnull/classDelegation.kt"); + } + @Test @TestMetadata("doWhileWithBreak.kt") public void testDoWhileWithBreak() throws Exception {