diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java index 21e6c01df9c..114ba25642f 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -11305,9 +11305,14 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/inline/propagation.kt"); } - @TestMetadata("protectedDepecation.kt") - public void testProtectedDepecation() throws Exception { - runTest("compiler/testData/diagnostics/tests/inline/protectedDepecation.kt"); + @TestMetadata("protectedCallDepecation.kt") + public void testProtectedCallDepecation() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/protectedCallDepecation.kt"); + } + + @TestMetadata("protectedCallError.kt") + public void testProtectedCallError() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/protectedCallError.kt"); } @TestMetadata("publishedApi.kt") diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 1393945c66a..09a569dfbef 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -1086,6 +1086,7 @@ public interface Errors { DiagnosticFactory0 INLINE_PROPERTY_WITH_BACKING_FIELD = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE); DiagnosticFactory0 NON_INTERNAL_PUBLISHED_API = DiagnosticFactory0.create(ERROR); DiagnosticFactory1 PROTECTED_CALL_FROM_PUBLIC_INLINE = DiagnosticFactory1.create(WARNING); + DiagnosticFactory1 PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR = DiagnosticFactory1.create(ERROR); DiagnosticFactory2 INVALID_DEFAULT_FUNCTIONAL_PARAMETER_FOR_INLINE = DiagnosticFactory2.create(ERROR); DiagnosticFactory2 NOT_SUPPORTED_INLINE_PARAMETER_IN_INLINE_PARAMETER_DEFAULT_VALUE = DiagnosticFactory2.create(ERROR); 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 b66bf943578..ba57167a7de 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -975,6 +975,7 @@ public class DefaultErrorMessages { MAP.put(INLINE_PROPERTY_WITH_BACKING_FIELD, "Inline property cannot have backing field"); MAP.put(NON_INTERNAL_PUBLISHED_API, "@PublishedApi annotation is only applicable for internal declaration"); MAP.put(PROTECTED_CALL_FROM_PUBLIC_INLINE, "Protected function call from public-API inline function is deprecated", NAME); + MAP.put(PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR, "Protected function call from public-API inline function is prohibited", NAME); MAP.put(INVALID_DEFAULT_FUNCTIONAL_PARAMETER_FOR_INLINE, "Invalid default value for inline parameter: ''{0}''. Only lambdas, anonymous functions, and callable references are supported", ELEMENT_TEXT, SHORT_NAMES_IN_TYPES); MAP.put(NOT_SUPPORTED_INLINE_PARAMETER_IN_INLINE_PARAMETER_DEFAULT_VALUE, "Usage of inline parameter ''{0}'' in default value for another inline parameter is not supported", ELEMENT_TEXT, SHORT_NAMES_IN_TYPES); //Inline non locals 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 7b7f14e930d..d51e3bd0761 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 @@ -21,7 +21,6 @@ import org.jetbrains.kotlin.builtins.isBuiltinFunctionalType import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.contracts.parsing.isFromContractDsl import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.diagnostics.Errors.* import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* @@ -58,10 +57,13 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC private var supportDefaultValueInline by Delegates.notNull() + private var prohibitProtectedCallFromInline by Delegates.notNull() + override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) { val expression = resolvedCall.call.calleeExpression ?: return supportDefaultValueInline = context.languageVersionSettings.supportsFeature(LanguageFeature.InlineDefaultFunctionalParameters) + prohibitProtectedCallFromInline = context.languageVersionSettings.supportsFeature(LanguageFeature.ProhibitProtectedCallFromInline) //checking that only invoke or inlinable extension called on function parameter val targetDescriptor = resolvedCall.resultingDescriptor @@ -227,7 +229,7 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC expression: KtElement ) { if (targetDescriptor.original === descriptor) { - context.trace.report(Errors.RECURSION_IN_INLINE.on(expression, expression, descriptor)) + context.trace.report(RECURSION_IN_INLINE.on(expression, expression, descriptor)) } } @@ -258,7 +260,7 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC if (isInlineFunPublicOrPublishedApi && !isCalledFunPublicOrPublishedApi && calledDescriptor.visibility !== Visibilities.LOCAL) { - context.trace.report(Errors.NON_PUBLIC_CALL_FROM_PUBLIC_INLINE.on(expression, calledDescriptor, descriptor)) + context.trace.report(NON_PUBLIC_CALL_FROM_PUBLIC_INLINE.on(expression, calledDescriptor, descriptor)) } else { checkPrivateClassMemberAccess(calledDescriptor, expression, context) } @@ -267,7 +269,11 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC isInlineFunPublicOrPublishedApi && inlineFunEffectiveVisibility.toVisibility() !== Visibilities.PROTECTED && calledFunEffectiveVisibility.toVisibility() === Visibilities.PROTECTED) { - context.trace.report(Errors.PROTECTED_CALL_FROM_PUBLIC_INLINE.on(expression, calledDescriptor)) + if (prohibitProtectedCallFromInline) { + context.trace.report(PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR.on(expression, calledDescriptor)) + } else { + context.trace.report(PROTECTED_CALL_FROM_PUBLIC_INLINE.on(expression, calledDescriptor)) + } } } @@ -278,7 +284,7 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC ) { if (!isEffectivelyPrivateApiFunction) { if (declarationDescriptor.isInsidePrivateClass) { - context.trace.report(Errors.PRIVATE_CLASS_MEMBER_FROM_INLINE.on(expression, declarationDescriptor, descriptor)) + context.trace.report(PRIVATE_CLASS_MEMBER_FROM_INLINE.on(expression, declarationDescriptor, descriptor)) } } } diff --git a/compiler/testData/diagnostics/tests/inline/protectedDepecation.kt b/compiler/testData/diagnostics/tests/inline/protectedCallDepecation.kt similarity index 97% rename from compiler/testData/diagnostics/tests/inline/protectedDepecation.kt rename to compiler/testData/diagnostics/tests/inline/protectedCallDepecation.kt index 16971c16679..ac79595a9a4 100644 --- a/compiler/testData/diagnostics/tests/inline/protectedDepecation.kt +++ b/compiler/testData/diagnostics/tests/inline/protectedCallDepecation.kt @@ -1,3 +1,4 @@ +// !LANGUAGE: -ProhibitProtectedCallFromInline // !DIAGNOSTICS: -EXPOSED_PARAMETER_TYPE -NOTHING_TO_INLINE // FILE: JavaClass.java diff --git a/compiler/testData/diagnostics/tests/inline/protectedDepecation.txt b/compiler/testData/diagnostics/tests/inline/protectedCallDepecation.txt similarity index 100% rename from compiler/testData/diagnostics/tests/inline/protectedDepecation.txt rename to compiler/testData/diagnostics/tests/inline/protectedCallDepecation.txt diff --git a/compiler/testData/diagnostics/tests/inline/protectedCallError.kt b/compiler/testData/diagnostics/tests/inline/protectedCallError.kt new file mode 100644 index 00000000000..04aefe9d4bf --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/protectedCallError.kt @@ -0,0 +1,102 @@ +// !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() + } + } + +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inline/protectedCallError.txt b/compiler/testData/diagnostics/tests/inline/protectedCallError.txt new file mode 100644 index 00000000000..9e65e486e4a --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/protectedCallError.txt @@ -0,0 +1,84 @@ +package + +public open class A { + public constructor A() + protected final val z: kotlin.String = "1" + public final var zVar: kotlin.String + public final inline fun call(): kotlin.Unit + internal final inline fun callFromInternal(): kotlin.Unit + protected final inline fun callFromProtected(): kotlin.Unit + @kotlin.PublishedApi internal final inline fun callFromPublished(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + protected final fun test(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +internal final class AInternal { + public constructor AInternal() + protected final val z: kotlin.String = "1" + public final var zVar: kotlin.String + public final inline fun call(): kotlin.Unit + @kotlin.PublishedApi internal final inline fun call2(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + protected final fun test(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class B : A { + public constructor B() + protected final override /*1*/ /*fake_override*/ val z: kotlin.String + public final override /*1*/ /*fake_override*/ var zVar: kotlin.String + public final override /*1*/ inline /*fake_override*/ fun call(): kotlin.Unit + internal final override /*1*/ inline /*fake_override*/ fun callFromInternal(): kotlin.Unit + protected final override /*1*/ inline /*fake_override*/ fun callFromProtected(): kotlin.Unit + @kotlin.PublishedApi internal final override /*1*/ inline /*fake_override*/ fun callFromPublished(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + protected final override /*1*/ /*fake_override*/ fun test(): kotlin.Unit + public final inline fun testB(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class C : JavaClass { + public constructor C() + protected/*protected and package*/ open override /*1*/ /*fake_override*/ fun bind(): kotlin.Unit + public final inline fun call(): kotlin.Unit + internal final inline fun callFromInternal(): kotlin.Unit + protected final inline fun callFromProtected(): kotlin.Unit + @kotlin.PublishedApi internal final inline fun callFromPublished(): kotlin.Unit + 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 abstract class JavaClass { + public constructor JavaClass() + protected/*protected and package*/ open fun bind(): kotlin.Unit + 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 +} + +private final class X { + public constructor X() + 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 Z : A { + public constructor Z() + protected final override /*1*/ /*fake_override*/ val z: kotlin.String + public final override /*1*/ /*fake_override*/ var zVar: kotlin.String + public final override /*1*/ inline /*fake_override*/ fun call(): kotlin.Unit + internal final override /*1*/ inline /*fake_override*/ fun callFromInternal(): kotlin.Unit + protected final override /*1*/ inline /*fake_override*/ fun callFromProtected(): kotlin.Unit + @kotlin.PublishedApi internal final override /*1*/ inline /*fake_override*/ fun callFromPublished(): kotlin.Unit + public final inline fun effictivelyNonPublic(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + protected final override /*1*/ /*fake_override*/ fun test(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index c53f3b995ca..0b0ccd56f03 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -11312,9 +11312,14 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/inline/propagation.kt"); } - @TestMetadata("protectedDepecation.kt") - public void testProtectedDepecation() throws Exception { - runTest("compiler/testData/diagnostics/tests/inline/protectedDepecation.kt"); + @TestMetadata("protectedCallDepecation.kt") + public void testProtectedCallDepecation() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/protectedCallDepecation.kt"); + } + + @TestMetadata("protectedCallError.kt") + public void testProtectedCallError() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/protectedCallError.kt"); } @TestMetadata("publishedApi.kt") diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 05798236e39..77ebe60bf73 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -11307,9 +11307,14 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/inline/propagation.kt"); } - @TestMetadata("protectedDepecation.kt") - public void testProtectedDepecation() throws Exception { - runTest("compiler/testData/diagnostics/tests/inline/protectedDepecation.kt"); + @TestMetadata("protectedCallDepecation.kt") + public void testProtectedCallDepecation() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/protectedCallDepecation.kt"); + } + + @TestMetadata("protectedCallError.kt") + public void testProtectedCallError() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/protectedCallError.kt"); } @TestMetadata("publishedApi.kt") diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 03f828e997b..7e06ebb0f23 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -114,6 +114,7 @@ enum class LanguageFeature( TrailingCommas(KOTLIN_1_4), ProhibitInvisibleAbstractMethodsInSuperclasses(KOTLIN_1_4, kind = BUG_FIX), ProhibitNonReifiedArraysAsReifiedTypeArguments(KOTLIN_1_4, kind = BUG_FIX), + ProhibitProtectedCallFromInline(KOTLIN_1_4, kind = BUG_FIX), ProperVisibilityForCompanionObjectInstanceField(sinceVersion = null, kind = BUG_FIX), // Temporarily disabled, see KT-27084/KT-22379