From 70662007a54bced7cfd3d2c6a0089426968fe1a1 Mon Sep 17 00:00:00 2001 From: Brian Norman Date: Wed, 12 Jul 2023 08:50:44 -0500 Subject: [PATCH] [AllOpen] Private properties can be considered stable even when open The AllOpen plugin can make private members open. But for private properties, they can be considered stable for smart-casting if they do not have a custom getter. #KT-58049 Fixed --- .../fir/resolve/dfa/VariableStorageImpl.kt | 3 ++ .../kotlin/generators/tests/GenerateTests.kt | 3 ++ .../AbstractDiagnosticTestForAllOpen.kt | 19 ++++++++++ .../testData/diagnostics/kt54260.fir.kt | 9 +++++ .../allopen/testData/diagnostics/kt54260.kt | 5 +-- .../testData/diagnostics/smartcast.fir.kt | 24 ++++++++++++ .../allopen/testData/diagnostics/smartcast.kt | 24 ++++++++++++ ...DiagnosticTestForAllOpenBaseGenerated.java | 38 +++++++++++++++++++ ...TreeDiagnosticTestForAllOpenGenerated.java | 6 +++ ...rPsiDiagnosticTestForAllOpenGenerated.java | 6 +++ 10 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 plugins/allopen/test/org/jetbrains/kotlin/allopen/AbstractDiagnosticTestForAllOpen.kt create mode 100644 plugins/allopen/testData/diagnostics/kt54260.fir.kt create mode 100644 plugins/allopen/testData/diagnostics/smartcast.fir.kt create mode 100644 plugins/allopen/testData/diagnostics/smartcast.kt create mode 100644 plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/DiagnosticTestForAllOpenBaseGenerated.java 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 2f9c44c2cea..33512ab159d 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 @@ -6,6 +6,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.declarations.FirAnonymousObject @@ -14,6 +15,7 @@ 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.declarations.utils.visibility import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.impl.FirNoReceiverExpression import org.jetbrains.kotlin.fir.moduleData @@ -162,6 +164,7 @@ class VariableStorageImpl(private val session: FirSession) : VariableStorage() { property.isVar -> PropertyStability.MUTABLE_PROPERTY property.receiverParameter != null -> PropertyStability.PROPERTY_WITH_GETTER property.getter.let { it != null && it !is FirDefaultPropertyAccessor } -> PropertyStability.PROPERTY_WITH_GETTER + property.visibility == Visibilities.Private -> PropertyStability.STABLE_VALUE property.modality != Modality.FINAL -> { val dispatchReceiver = (originalFir.unwrapElement() as? FirQualifiedAccessExpression)?.dispatchReceiver ?: return null val receiverType = dispatchReceiver.typeRef.coneTypeSafe()?.fullyExpandedType(session) ?: return null diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 60af7bbb50d..baf6a3c0ec0 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -286,6 +286,9 @@ fun main(args: Array) { testClass { model("bytecodeListing", excludedPattern = excludedFirTestdataPattern) } + testClass { + model("diagnostics", excludedPattern = excludedFirTestdataPattern) + } testClass { model("diagnostics", excludedPattern = excludedFirTestdataPattern) } diff --git a/plugins/allopen/test/org/jetbrains/kotlin/allopen/AbstractDiagnosticTestForAllOpen.kt b/plugins/allopen/test/org/jetbrains/kotlin/allopen/AbstractDiagnosticTestForAllOpen.kt new file mode 100644 index 00000000000..51e79341da3 --- /dev/null +++ b/plugins/allopen/test/org/jetbrains/kotlin/allopen/AbstractDiagnosticTestForAllOpen.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.allopen + +import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder +import org.jetbrains.kotlin.test.runners.AbstractDiagnosticTest + +abstract class AbstractDiagnosticTestForAllOpenBase : AbstractDiagnosticTest() { + override fun configure(builder: TestConfigurationBuilder) { + super.configure(builder) + + with(builder) { + useConfigurators(::AllOpenEnvironmentConfigurator) + } + } +} diff --git a/plugins/allopen/testData/diagnostics/kt54260.fir.kt b/plugins/allopen/testData/diagnostics/kt54260.fir.kt new file mode 100644 index 00000000000..36a668f0233 --- /dev/null +++ b/plugins/allopen/testData/diagnostics/kt54260.fir.kt @@ -0,0 +1,9 @@ +// WITH_STDLIB + +annotation class AllOpen + +@AllOpen +annotation class ConsoleCommands( + val value: String = "", + val scope: String +) diff --git a/plugins/allopen/testData/diagnostics/kt54260.kt b/plugins/allopen/testData/diagnostics/kt54260.kt index d24058ef424..f24494383b2 100644 --- a/plugins/allopen/testData/diagnostics/kt54260.kt +++ b/plugins/allopen/testData/diagnostics/kt54260.kt @@ -1,10 +1,9 @@ -// FIR_IDENTICAL // WITH_STDLIB annotation class AllOpen @AllOpen annotation class ConsoleCommands( - val value: String = "", - val scope: String + val value: String = "", + val scope: String ) diff --git a/plugins/allopen/testData/diagnostics/smartcast.fir.kt b/plugins/allopen/testData/diagnostics/smartcast.fir.kt new file mode 100644 index 00000000000..0bfd9c99186 --- /dev/null +++ b/plugins/allopen/testData/diagnostics/smartcast.fir.kt @@ -0,0 +1,24 @@ +// WITH_STDLIB +// ISSUE: KT-58049 + +annotation class AllOpen + +@AllOpen +class Test( + val publicProp: String?, + protected val protectedProp: String?, + internal val internalProp: String?, + private val privateProp: String?, +) { + fun test() { + checkNotNull(publicProp) + checkNotNull(protectedProp) + checkNotNull(internalProp) + checkNotNull(privateProp) + + println(publicProp.length) + println(protectedProp.length) + println(internalProp.length) + println(privateProp.length) + } +} diff --git a/plugins/allopen/testData/diagnostics/smartcast.kt b/plugins/allopen/testData/diagnostics/smartcast.kt new file mode 100644 index 00000000000..2ad567f7f4d --- /dev/null +++ b/plugins/allopen/testData/diagnostics/smartcast.kt @@ -0,0 +1,24 @@ +// WITH_STDLIB +// ISSUE: KT-58049 + +annotation class AllOpen + +@AllOpen +class Test( + val publicProp: String?, + protected val protectedProp: String?, + internal val internalProp: String?, + private val privateProp: String?, +) { + fun test() { + checkNotNull(publicProp) + checkNotNull(protectedProp) + checkNotNull(internalProp) + checkNotNull(privateProp) + + println(publicProp.length) + println(protectedProp.length) + println(internalProp.length) + println(privateProp.length) + } +} diff --git a/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/DiagnosticTestForAllOpenBaseGenerated.java b/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/DiagnosticTestForAllOpenBaseGenerated.java new file mode 100644 index 00000000000..c4beddfc1ec --- /dev/null +++ b/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/DiagnosticTestForAllOpenBaseGenerated.java @@ -0,0 +1,38 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.allopen; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.util.KtTestUtil; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.GenerateTestsKt}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("plugins/allopen/testData/diagnostics") +@TestDataPath("$PROJECT_ROOT") +public class DiagnosticTestForAllOpenBaseGenerated extends AbstractDiagnosticTestForAllOpenBase { + @Test + public void testAllFilesPresentInDiagnostics() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/allopen/testData/diagnostics"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); + } + + @Test + @TestMetadata("kt54260.kt") + public void testKt54260() throws Exception { + runTest("plugins/allopen/testData/diagnostics/kt54260.kt"); + } + + @Test + @TestMetadata("smartcast.kt") + public void testSmartcast() throws Exception { + runTest("plugins/allopen/testData/diagnostics/smartcast.kt"); + } +} diff --git a/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/FirLightTreeDiagnosticTestForAllOpenGenerated.java b/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/FirLightTreeDiagnosticTestForAllOpenGenerated.java index fd56b4abdba..7c87b5361bb 100644 --- a/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/FirLightTreeDiagnosticTestForAllOpenGenerated.java +++ b/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/FirLightTreeDiagnosticTestForAllOpenGenerated.java @@ -29,4 +29,10 @@ public class FirLightTreeDiagnosticTestForAllOpenGenerated extends AbstractFirLi public void testKt54260() throws Exception { runTest("plugins/allopen/testData/diagnostics/kt54260.kt"); } + + @Test + @TestMetadata("smartcast.kt") + public void testSmartcast() throws Exception { + runTest("plugins/allopen/testData/diagnostics/smartcast.kt"); + } } diff --git a/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/FirPsiDiagnosticTestForAllOpenGenerated.java b/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/FirPsiDiagnosticTestForAllOpenGenerated.java index 8ecd46ca028..dc51fb43d0a 100644 --- a/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/FirPsiDiagnosticTestForAllOpenGenerated.java +++ b/plugins/allopen/tests-gen/org/jetbrains/kotlin/allopen/FirPsiDiagnosticTestForAllOpenGenerated.java @@ -29,4 +29,10 @@ public class FirPsiDiagnosticTestForAllOpenGenerated extends AbstractFirPsiDiagn public void testKt54260() throws Exception { runTest("plugins/allopen/testData/diagnostics/kt54260.kt"); } + + @Test + @TestMetadata("smartcast.kt") + public void testSmartcast() throws Exception { + runTest("plugins/allopen/testData/diagnostics/smartcast.kt"); + } }