From 195ca10394eaf184a9c284d2b481d33ec2e2c318 Mon Sep 17 00:00:00 2001 From: "vladislav.grechko" Date: Wed, 28 Jun 2023 11:59:56 +0200 Subject: [PATCH] [FIR] Report error on non-public API access in @PublishedApi inline fun ^KT-59695: Fixed --- .../FirInlineDeclarationChecker.kt | 2 +- .../nonPublicMember/publishedApi.fir.kt | 48 ----- .../inline/nonPublicMember/publishedApi.kt | 2 + .../inline/protectedCallDepecation.fir.kt | 8 +- .../tests/inline/protectedCallError.fir.kt | 102 ---------- .../tests/inline/protectedCallError.kt | 2 + .../tests/inline/publishedApi.fir.kt | 175 ------------------ .../diagnostics/tests/inline/publishedApi.kt | 3 + .../tests/inline/superCallDepecation.fir.kt | 2 +- .../inline/superCallDepecationWarning.fir.kt | 2 +- 10 files changed, 14 insertions(+), 332 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/inline/nonPublicMember/publishedApi.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/inline/protectedCallError.fir.kt delete mode 100644 compiler/testData/diagnostics/tests/inline/publishedApi.fir.kt diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirInlineDeclarationChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirInlineDeclarationChecker.kt index a410282e822..163e8d4d25b 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirInlineDeclarationChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirInlineDeclarationChecker.kt @@ -47,7 +47,7 @@ object FirInlineDeclarationChecker : FirFunctionChecker() { if (context.session.inlineCheckerExtension?.isGenerallyOk(declaration, context, reporter) == false) return if (declaration !is FirPropertyAccessor && declaration !is FirSimpleFunction) return - val effectiveVisibility = declaration.effectiveVisibility + val effectiveVisibility = declaration.publishedApiEffectiveVisibility ?: declaration.effectiveVisibility checkInlineFunctionBody(declaration, effectiveVisibility, context, reporter) checkCallableDeclaration(declaration, context, reporter) } diff --git a/compiler/testData/diagnostics/tests/inline/nonPublicMember/publishedApi.fir.kt b/compiler/testData/diagnostics/tests/inline/nonPublicMember/publishedApi.fir.kt deleted file mode 100644 index 0916a9f9499..00000000000 --- a/compiler/testData/diagnostics/tests/inline/nonPublicMember/publishedApi.fir.kt +++ /dev/null @@ -1,48 +0,0 @@ -// !DIAGNOSTICS: -EXPOSED_PARAMETER_TYPE -NOTHING_TO_INLINE - - -inline fun call(a: A) { - a.test() - - privateFun() - - internalFun() -} - -@PublishedApi -internal inline fun callFromPublishedApi(a: A) { - a.test() - - privateFun() - - internalFun() -} - -internal class A { - @PublishedApi - internal fun test() { - test() - - privateFun() - - internalFun() - } - - @PublishedApi - internal fun testInline() { - test() - - privateFun() - - internalFun() - } -} - - -private fun privateFun() { - -} - -internal fun internalFun() { - -} diff --git a/compiler/testData/diagnostics/tests/inline/nonPublicMember/publishedApi.kt b/compiler/testData/diagnostics/tests/inline/nonPublicMember/publishedApi.kt index 238a1580f88..74a02479452 100644 --- a/compiler/testData/diagnostics/tests/inline/nonPublicMember/publishedApi.kt +++ b/compiler/testData/diagnostics/tests/inline/nonPublicMember/publishedApi.kt @@ -1,5 +1,7 @@ // !DIAGNOSTICS: -EXPOSED_PARAMETER_TYPE -NOTHING_TO_INLINE +// FIR_IDENTICAL + inline fun call(a: A) { a.test() diff --git a/compiler/testData/diagnostics/tests/inline/protectedCallDepecation.fir.kt b/compiler/testData/diagnostics/tests/inline/protectedCallDepecation.fir.kt index 8e32f29b65d..6db2baf2f2e 100644 --- a/compiler/testData/diagnostics/tests/inline/protectedCallDepecation.fir.kt +++ b/compiler/testData/diagnostics/tests/inline/protectedCallDepecation.fir.kt @@ -32,10 +32,10 @@ open class A { @PublishedApi internal inline fun callFromPublished() { - test() - z + test() + z zVar - zVar = "123" + zVar = "123" } protected inline fun callFromProtected() { @@ -67,7 +67,7 @@ class C : JavaClass() { @PublishedApi internal inline fun callFromPublished() { - bind() + bind() } } diff --git a/compiler/testData/diagnostics/tests/inline/protectedCallError.fir.kt b/compiler/testData/diagnostics/tests/inline/protectedCallError.fir.kt deleted file mode 100644 index a6044469d59..00000000000 --- a/compiler/testData/diagnostics/tests/inline/protectedCallError.fir.kt +++ /dev/null @@ -1,102 +0,0 @@ -// !LANGUAGE: +ProhibitProtectedCallFromInline -// !DIAGNOSTICS: -EXPOSED_PARAMETER_TYPE -NOTHING_TO_INLINE - -// FILE: JavaClass.java - - -public abstract class JavaClass { - protected void bind() {} -} - -// FILE: main.kt -open class A { - protected fun test() {} - - protected val z: String = "1" - - public var zVar: String = "1" - protected set(value) {} - - inline fun call() { - test() - z - zVar - zVar = "123" - } - - internal inline fun callFromInternal() { - test() - zVar - zVar = "123" - } - - @PublishedApi - internal inline fun callFromPublished() { - test() - z - zVar - zVar = "123" - } - - protected inline fun callFromProtected() { - test() - zVar - zVar = "123" - } - -} - -class B : A() { - inline fun testB() { - test() - } -} - -class C : JavaClass() { - inline fun call() { - bind() - } - - internal inline fun callFromInternal() { - bind() - } - - protected inline fun callFromProtected() { - bind() - } - - @PublishedApi - internal inline fun callFromPublished() { - bind() - } -} - - -internal class AInternal { - protected fun test() {} - - protected val z: String = "1" - - public var zVar: String = "1" - protected set(value) {} - - - inline fun call() { - test() - } - - @PublishedApi - internal inline fun call2() { - test() - } -} - -private class X { - - public class Z : A() { - public inline fun effictivelyNonPublic() { - test() - } - } - -} diff --git a/compiler/testData/diagnostics/tests/inline/protectedCallError.kt b/compiler/testData/diagnostics/tests/inline/protectedCallError.kt index 52bf6b69eaa..013a1d40f35 100644 --- a/compiler/testData/diagnostics/tests/inline/protectedCallError.kt +++ b/compiler/testData/diagnostics/tests/inline/protectedCallError.kt @@ -1,6 +1,8 @@ // !LANGUAGE: +ProhibitProtectedCallFromInline // !DIAGNOSTICS: -EXPOSED_PARAMETER_TYPE -NOTHING_TO_INLINE +// FIR_IDENTICAL + // FILE: JavaClass.java diff --git a/compiler/testData/diagnostics/tests/inline/publishedApi.fir.kt b/compiler/testData/diagnostics/tests/inline/publishedApi.fir.kt deleted file mode 100644 index ba28cd7abd9..00000000000 --- a/compiler/testData/diagnostics/tests/inline/publishedApi.fir.kt +++ /dev/null @@ -1,175 +0,0 @@ -// !DIAGNOSTICS: -EXPOSED_PARAMETER_TYPE -NOTHING_TO_INLINE -inline fun call(a: A) { - a.test() - publishedTopLevel() - - a.publishedVar - a.publishedVar = 1 - - publishedVarTopLevel - publishedVarTopLevel = 1 -} - -inline var inlineVar: Int - get() { - val a = A() - a.test() - publishedTopLevel() - - a.publishedVar - a.publishedVar = 1 - - publishedVarTopLevel - publishedVarTopLevel = 1 - return 1 - } - set(value) { - val a = A() - a.test() - publishedTopLevel() - - a.publishedVar - a.publishedVar = 1 - - publishedVarTopLevel - publishedVarTopLevel = 1 - } - -@PublishedApi -internal class A { - @PublishedApi - internal fun test() { - publicFun() - internalFun() - privateFun() - - publicVarTopLevel - publicVarTopLevel = 1 - internalVarTopLevel - internalVarTopLevel = 1 - privateVarTopLevel - privateVarTopLevel = 1 - - publishedVar - publishedVar = 1 - } - - @PublishedApi - internal inline fun testInline() { - publicFun() - internalFun() - privateFun() - - publicVarTopLevel - publicVarTopLevel = 1 - internalVarTopLevel - internalVarTopLevel = 1 - privateVarTopLevel - privateVarTopLevel = 1 - - publishedVar - publishedVar = 1 - } - - - @PublishedApi - internal inline var publishedVar: Int - get() { - publicFun() - internalFun() - privateFun() - - publicVarTopLevel - publicVarTopLevel = 1 - internalVarTopLevel - internalVarTopLevel = 1 - privateVarTopLevel - privateVarTopLevel = 1 - return 1 - } - - set(value) { - publicFun() - internalFun() - privateFun() - - publicVarTopLevel - publicVarTopLevel = 1 - internalVarTopLevel - internalVarTopLevel = 1 - privateVarTopLevel - privateVarTopLevel = 1 - } - -} - -@PublishedApi -internal fun publishedTopLevel() { - publicFun() - internalFun() - privateFun() - - publicVarTopLevel - publicVarTopLevel = 1 - internalVarTopLevel - internalVarTopLevel = 1 - privateVarTopLevel - privateVarTopLevel = 1 -} - -@PublishedApi -inline internal fun publishedTopLevelInline() { - publicFun() - internalFun() - privateFun() - - publicVarTopLevel - publicVarTopLevel = 1 - internalVarTopLevel - internalVarTopLevel = 1 - privateVarTopLevel - privateVarTopLevel = 1 -} - -@PublishedApi -inline internal var publishedVarTopLevel: Int - get() { - publicFun() - internalFun() - privateFun() - - publicVarTopLevel - publicVarTopLevel = 1 - internalVarTopLevel - internalVarTopLevel = 1 - privateVarTopLevel - privateVarTopLevel = 1 - return 1 - } - - set(value) { - publicFun() - internalFun() - privateFun() - - publicVarTopLevel - publicVarTopLevel = 1 - internalVarTopLevel - internalVarTopLevel = 1 - privateVarTopLevel - privateVarTopLevel = 1 - } - - -fun publicFun() {} - -internal fun internalFun() {} - -private fun privateFun() {} - - -var publicVarTopLevel = 1 - -internal var internalVarTopLevel = 1 - -private var privateVarTopLevel = 1 diff --git a/compiler/testData/diagnostics/tests/inline/publishedApi.kt b/compiler/testData/diagnostics/tests/inline/publishedApi.kt index 80f67ef56fa..2732f18d5cb 100644 --- a/compiler/testData/diagnostics/tests/inline/publishedApi.kt +++ b/compiler/testData/diagnostics/tests/inline/publishedApi.kt @@ -1,4 +1,7 @@ // !DIAGNOSTICS: -EXPOSED_PARAMETER_TYPE -NOTHING_TO_INLINE + +// FIR_IDENTICAL + inline fun call(a: A) { a.test() publishedTopLevel() diff --git a/compiler/testData/diagnostics/tests/inline/superCallDepecation.fir.kt b/compiler/testData/diagnostics/tests/inline/superCallDepecation.fir.kt index 0b1476f7748..687b8cd1576 100644 --- a/compiler/testData/diagnostics/tests/inline/superCallDepecation.fir.kt +++ b/compiler/testData/diagnostics/tests/inline/superCallDepecation.fir.kt @@ -13,7 +13,7 @@ open class AndroidTargetConfigurator : @PublishedApi internal inline fun inlineFunPublished(): String { - return super.classFun() + super.getConfiguratorSettings() + super.getConfiguratorSettings() + return super.classFun() + super.getConfiguratorSettings() + super.getConfiguratorSettings() } public inline fun inlineFunAnonymousObjects(): String { diff --git a/compiler/testData/diagnostics/tests/inline/superCallDepecationWarning.fir.kt b/compiler/testData/diagnostics/tests/inline/superCallDepecationWarning.fir.kt index b1ab5fdbb56..8be253c6323 100644 --- a/compiler/testData/diagnostics/tests/inline/superCallDepecationWarning.fir.kt +++ b/compiler/testData/diagnostics/tests/inline/superCallDepecationWarning.fir.kt @@ -13,7 +13,7 @@ open class AndroidTargetConfigurator : @PublishedApi internal inline fun inlineFunPublished(): String { - return super.classFun() + super.getConfiguratorSettings() + super.getConfiguratorSettings() + return super.classFun() + super.getConfiguratorSettings() + super.getConfiguratorSettings() } public inline fun inlineFunAnonymousObjects(): String {