From a83caec94fbf5070393595b8f1ee2520d8d9c5ff Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 25 Jan 2023 11:33:06 +0200 Subject: [PATCH] [FE 1.0] Implement deprecation PRIVATE_CLASS_MEMBER_FROM_INLINE_WARNING warning ^KT-55179 ^KT-56171 Fixed --- ...osisCompilerTestFE10TestdataTestGenerated.java | 6 ++++++ .../FirOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...tendDiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ .../org/jetbrains/kotlin/diagnostics/Errors.java | 2 ++ .../rendering/DefaultErrorMessages.java | 1 + .../resolve/calls/checkers/InlineChecker.kt | 4 ++++ .../tests/inline/nonPublicMember/kt55179.fir.kt | 15 +++++++++++++++ .../tests/inline/nonPublicMember/kt55179.kt | 15 +++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ .../jetbrains/kotlin/resolve/DescriptorUtils.kt | 7 +++++++ 10 files changed, 68 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.kt 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 f7d49a7a1e0..4c5805cdbdc 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 @@ -18069,6 +18069,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/inline/nonPublicMember/kt14887.kt"); } + @Test + @TestMetadata("kt55179.kt") + public void testKt55179() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.kt"); + } + @Test @TestMetadata("localClass.kt") public void testLocalClass() 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 8ad156abdf7..16c14a9deb8 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 @@ -18075,6 +18075,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inline/nonPublicMember/kt14887.kt"); } + @Test + @TestMetadata("kt55179.kt") + public void testKt55179() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.kt"); + } + @Test @TestMetadata("localClass.kt") public void testLocalClass() 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 f0de1c2ed67..8531652e5be 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 @@ -18069,6 +18069,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/inline/nonPublicMember/kt14887.kt"); } + @Test + @TestMetadata("kt55179.kt") + public void testKt55179() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.kt"); + } + @Test @TestMetadata("localClass.kt") public void testLocalClass() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 25cb21b9142..a23a7f17ec4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -1292,6 +1292,8 @@ public interface Errors { DiagnosticFactory0 DEPRECATED_IMPLICIT_NON_PUBLIC_API_ACCESS = DiagnosticFactory0.create(WARNING, CALL_ELEMENT); DiagnosticFactory2 PRIVATE_CLASS_MEMBER_FROM_INLINE = DiagnosticFactory2.create(ERROR, CALL_ELEMENT); + DiagnosticFactory2 PRIVATE_CLASS_MEMBER_FROM_INLINE_WARNING = + DiagnosticFactory2.create(WARNING, CALL_ELEMENT); DiagnosticFactory1 NON_LOCAL_RETURN_NOT_ALLOWED = DiagnosticFactory1.create(ERROR, CALL_ELEMENT); DiagnosticFactory1 NOT_YET_SUPPORTED_IN_INLINE = DiagnosticFactory1.create(ERROR, NOT_SUPPORTED_IN_INLINE_MOST_RELEVANT); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index df65ee19ef9..3224e89dc4e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -1121,6 +1121,7 @@ public class DefaultErrorMessages { MAP.put(NON_PUBLIC_CALL_FROM_PUBLIC_INLINE, "Public-API inline function cannot access non-public-API ''{0}''", SHORT_NAMES_IN_TYPES, SHORT_NAMES_IN_TYPES); MAP.put(DEPRECATED_IMPLICIT_NON_PUBLIC_API_ACCESS, "Deprecated implicit access of non-public-API from public-API inline function"); MAP.put(PRIVATE_CLASS_MEMBER_FROM_INLINE, "Non-private inline function cannot access members of private classes: ''{0}''", SHORT_NAMES_IN_TYPES, SHORT_NAMES_IN_TYPES); + MAP.put(PRIVATE_CLASS_MEMBER_FROM_INLINE_WARNING, "Non-private inline function cannot access members of private classes: ''{0}''. This warning will become an error in 2.0", SHORT_NAMES_IN_TYPES, SHORT_NAMES_IN_TYPES); MAP.put(NOT_YET_SUPPORTED_IN_INLINE, "{0} are not yet supported in inline functions", STRING); MAP.put(DECLARATION_CANT_BE_INLINED, "'inline' modifier is not allowed on virtual members. Only private or final members can be inlined"); MAP.put(DECLARATION_CANT_BE_INLINED_WARNING, "'inline' modifier is not allowed on virtual enum members. Only private or final members can be inlined. This warning will become an error in K2"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InlineChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InlineChecker.kt index b607f8e6cb5..433630ba832 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InlineChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InlineChecker.kt @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyPrivateApi import org.jetbrains.kotlin.resolve.descriptorUtil.isInsidePrivateClass +import org.jetbrains.kotlin.resolve.descriptorUtil.isMemberOfCompanionOfPrivateClass import org.jetbrains.kotlin.resolve.inline.InlineUtil import org.jetbrains.kotlin.resolve.inline.InlineUtil.allowsNonLocalReturns import org.jetbrains.kotlin.resolve.inline.InlineUtil.checkNonLocalReturnUsage @@ -314,6 +315,9 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC if (declarationDescriptor.isInsidePrivateClass) { context.trace.report(PRIVATE_CLASS_MEMBER_FROM_INLINE.on(expression, declarationDescriptor, descriptor)) context.reportDeprecationOnReplacement(expression, replacementForReport) + } else if (declarationDescriptor.isMemberOfCompanionOfPrivateClass) { + context.trace.report(PRIVATE_CLASS_MEMBER_FROM_INLINE_WARNING.on(expression, declarationDescriptor, descriptor)) + context.reportDeprecationOnReplacement(expression, replacementForReport) } } } diff --git a/compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.fir.kt b/compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.fir.kt new file mode 100644 index 00000000000..7b5e1f2d6da --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.fir.kt @@ -0,0 +1,15 @@ +// ISSUE: KT-55179 +// SKIP_TXT + +private class Foo { + companion object { + fun buildFoo() = Foo() + } +} + +internal inline fun foo() { + Foo() + Foo.Companion + Foo.buildFoo() +} + diff --git a/compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.kt b/compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.kt new file mode 100644 index 00000000000..f7587bf9a9b --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.kt @@ -0,0 +1,15 @@ +// ISSUE: KT-55179 +// SKIP_TXT + +private class Foo { + companion object { + fun buildFoo() = Foo() + } +} + +internal inline fun foo() { + Foo() + Foo.Companion + Foo.buildFoo() +} + 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 6e51290318c..41bc082b06f 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 @@ -18075,6 +18075,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inline/nonPublicMember/kt14887.kt"); } + @Test + @TestMetadata("kt55179.kt") + public void testKt55179() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/nonPublicMember/kt55179.kt"); + } + @Test @TestMetadata("localClass.kt") public void testLocalClass() throws Exception { diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt index c71ffc4042e..ce0bb9c3558 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt @@ -135,6 +135,13 @@ val DeclarationDescriptor.isInsidePrivateClass: Boolean return parent != null && DescriptorVisibilities.isPrivate(parent.visibility) } +val DeclarationDescriptor.isMemberOfCompanionOfPrivateClass: Boolean + get() { + val parent = containingDeclaration as? ClassDescriptor ?: return false + if (!parent.isCompanionObject) return false + return parent.isInsidePrivateClass + } + val DeclarationDescriptor.isInsideInterface: Boolean get() { val parent = containingDeclaration as? ClassDescriptor