From 21521aa397e9419272ed98bebae2e67ad30d994b Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Tue, 27 Oct 2020 09:55:32 +0100 Subject: [PATCH] Deprecate protected constructors call from public inline function #KT-21177 --- ...irOldFrontendDiagnosticsTestGenerated.java | 5 ++++ .../jetbrains/kotlin/diagnostics/Errors.java | 1 + .../rendering/DefaultErrorMessages.java | 1 + .../resolve/calls/checkers/InlineChecker.kt | 17 +++++++++---- .../diagnostics/tests/inline/kt15410.kt | 2 +- .../diagnostics/tests/inline/kt21177.fir.kt | 18 +++++++++++++ .../diagnostics/tests/inline/kt21177.kt | 18 +++++++++++++ .../diagnostics/tests/inline/kt21177.txt | 25 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 5 ++++ .../DiagnosticsUsingJavacTestGenerated.java | 5 ++++ 10 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inline/kt21177.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inline/kt21177.kt create mode 100644 compiler/testData/diagnostics/tests/inline/kt21177.txt diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index 7cad9d0b1ba..f25811e0fa1 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -12386,6 +12386,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/inline/kt19679.kt"); } + @TestMetadata("kt21177.kt") + public void testKt21177() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/kt21177.kt"); + } + @TestMetadata("kt4869.kt") public void testKt4869() throws Exception { runTest("compiler/testData/diagnostics/tests/inline/kt4869.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index d8f128127ca..4ab63010616 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -1130,6 +1130,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_CONSTRUCTOR_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 fb09ce2e121..93ac7c86915 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -1018,6 +1018,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_CONSTRUCTOR_CALL_FROM_PUBLIC_INLINE, "Protected constructor 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); 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 3d7d92f250a..37bb400e12e 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 @@ -265,14 +265,21 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC checkPrivateClassMemberAccess(calledDescriptor, expression, context) } - if (calledDescriptor !is ConstructorDescriptor && + val isConstructorCall = calledDescriptor is ConstructorDescriptor + if ((!isConstructorCall || expression !is KtConstructorCalleeExpression) && isInlineFunPublicOrPublishedApi && inlineFunEffectiveVisibility.toVisibility() !== Visibilities.Protected && calledFunEffectiveVisibility.toVisibility() === Visibilities.Protected) { - 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)) + when { + isConstructorCall -> { + context.trace.report(PROTECTED_CONSTRUCTOR_CALL_FROM_PUBLIC_INLINE.on(expression, calledDescriptor)) + } + 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)) + } } } } diff --git a/compiler/testData/diagnostics/tests/inline/kt15410.kt b/compiler/testData/diagnostics/tests/inline/kt15410.kt index 1bfe158cd71..f2421a1319f 100644 --- a/compiler/testData/diagnostics/tests/inline/kt15410.kt +++ b/compiler/testData/diagnostics/tests/inline/kt15410.kt @@ -6,5 +6,5 @@ open class Foo protected constructor() inline fun foo(f: () -> Unit) = object: Foo() {} class A : Foo() { - inline fun foo(f: () -> Unit) = Foo() + inline fun foo(f: () -> Unit) = Foo() } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inline/kt21177.fir.kt b/compiler/testData/diagnostics/tests/inline/kt21177.fir.kt new file mode 100644 index 00000000000..45412c060c0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/kt21177.fir.kt @@ -0,0 +1,18 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +class SomeContainer { + protected class Limit + + protected fun makeLimit(): Limit = TODO() + + public inline fun foo(f: () -> Unit) { + Limit() + makeLimit() + } +} + +open class A protected constructor() { + inline fun foo(f: () -> Unit) { + A() + } +} diff --git a/compiler/testData/diagnostics/tests/inline/kt21177.kt b/compiler/testData/diagnostics/tests/inline/kt21177.kt new file mode 100644 index 00000000000..9d8dcdd28a1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/kt21177.kt @@ -0,0 +1,18 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +class SomeContainer { + protected class Limit + + protected fun makeLimit(): Limit = TODO() + + public inline fun foo(f: () -> Unit) { + Limit() + makeLimit() + } +} + +open class A protected constructor() { + inline fun foo(f: () -> Unit) { + A() + } +} diff --git a/compiler/testData/diagnostics/tests/inline/kt21177.txt b/compiler/testData/diagnostics/tests/inline/kt21177.txt new file mode 100644 index 00000000000..dfe110aad4e --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/kt21177.txt @@ -0,0 +1,25 @@ +package + +public open class A { + protected constructor A() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final inline fun foo(/*0*/ f: () -> kotlin.Unit): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class SomeContainer { + public constructor SomeContainer() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final inline fun foo(/*0*/ f: () -> kotlin.Unit): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + protected final fun makeLimit(): SomeContainer.Limit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + protected final class Limit { + public constructor Limit() + 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 + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 6ff186956b4..290b35dd7da 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -12393,6 +12393,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali runTest("compiler/testData/diagnostics/tests/inline/kt19679.kt"); } + @TestMetadata("kt21177.kt") + public void testKt21177() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/kt21177.kt"); + } + @TestMetadata("kt4869.kt") public void testKt4869() throws Exception { runTest("compiler/testData/diagnostics/tests/inline/kt4869.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 0c013339bac..573adcf8980 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -12388,6 +12388,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/inline/kt19679.kt"); } + @TestMetadata("kt21177.kt") + public void testKt21177() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/kt21177.kt"); + } + @TestMetadata("kt4869.kt") public void testKt4869() throws Exception { runTest("compiler/testData/diagnostics/tests/inline/kt4869.kt");