Prohibit protected method calls from inline function

#KT-21178 Fixed
This commit is contained in:
Mikhael Bogdanov
2019-11-29 13:27:57 +01:00
parent 8383274144
commit df96841c9d
11 changed files with 225 additions and 14 deletions
@@ -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")
@@ -1086,6 +1086,7 @@ public interface Errors {
DiagnosticFactory0<KtDeclaration> INLINE_PROPERTY_WITH_BACKING_FIELD = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtAnnotationEntry> NON_INTERNAL_PUBLISHED_API = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, CallableDescriptor> PROTECTED_CALL_FROM_PUBLIC_INLINE = DiagnosticFactory1.create(WARNING);
DiagnosticFactory1<PsiElement, CallableDescriptor> PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<KtElement, KtExpression, DeclarationDescriptor> INVALID_DEFAULT_FUNCTIONAL_PARAMETER_FOR_INLINE = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<KtElement, KtExpression, DeclarationDescriptor> NOT_SUPPORTED_INLINE_PARAMETER_IN_INLINE_PARAMETER_DEFAULT_VALUE = DiagnosticFactory2.create(ERROR);
@@ -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
@@ -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<Boolean>()
private var prohibitProtectedCallFromInline by Delegates.notNull<Boolean>()
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))
}
}
}
@@ -1,3 +1,4 @@
// !LANGUAGE: -ProhibitProtectedCallFromInline
// !DIAGNOSTICS: -EXPOSED_PARAMETER_TYPE -NOTHING_TO_INLINE
// FILE: JavaClass.java
@@ -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(<!UNUSED_PARAMETER!>value<!>) {}
inline fun call() {
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>test<!>()
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>z<!>
zVar
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>zVar<!> = "123"
}
internal inline fun callFromInternal() {
test()
zVar
zVar = "123"
}
@PublishedApi
internal inline fun callFromPublished() {
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>test<!>()
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>z<!>
zVar
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>zVar<!> = "123"
}
protected inline fun callFromProtected() {
test()
zVar
zVar = "123"
}
}
class B : A() {
inline fun testB() {
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>test<!>()
}
}
class C : JavaClass() {
inline fun call() {
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>bind<!>()
}
internal inline fun callFromInternal() {
bind()
}
protected inline fun callFromProtected() {
bind()
}
@PublishedApi
internal inline fun callFromPublished() {
<!PROTECTED_CALL_FROM_PUBLIC_INLINE_ERROR!>bind<!>()
}
}
internal class AInternal {
protected fun test() {}
protected val z: String = "1"
public var zVar: String = "1"
protected set(<!UNUSED_PARAMETER!>value<!>) {}
inline fun call() {
test()
}
@PublishedApi
internal inline fun call2() {
test()
}
}
private class X {
public class Z : A() {
public inline fun effictivelyNonPublic() {
test()
}
}
}
@@ -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
}
}
@@ -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")
@@ -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")
@@ -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