From 561ef5947a830835dd41db780fdb730ae3ddbe8f Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Wed, 22 Sep 2021 12:15:06 +0300 Subject: [PATCH] Introduce error for `PROTECTED_CONSTRUCTOR_CALL_FROM_PUBLIC_INLINE` ^KT-42972 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 6 +++++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++++ .../jetbrains/kotlin/diagnostics/Errors.java | 2 +- .../resolve/calls/checkers/InlineChecker.kt | 4 ++- .../diagnostics/tests/inline/kt15410.kt | 2 +- .../diagnostics/tests/inline/kt21177.kt | 4 +-- .../tests/inline/kt21177Warning.fir.kt | 19 ++++++++++++++ .../tests/inline/kt21177Warning.kt | 19 ++++++++++++++ .../tests/inline/kt21177Warning.txt | 25 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 +++++ .../kotlin/config/LanguageVersionSettings.kt | 1 + 11 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inline/kt21177Warning.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inline/kt21177Warning.kt create mode 100644 compiler/testData/diagnostics/tests/inline/kt21177Warning.txt 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 a3d33d28524..011c853c055 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 @@ -15795,6 +15795,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inline/kt21177.kt"); } + @Test + @TestMetadata("kt21177Warning.kt") + public void testKt21177Warning() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/kt21177Warning.kt"); + } + @Test @TestMetadata("kt4869.kt") public void testKt4869() 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 69c1fe343ed..f19ed0d95e3 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 @@ -15795,6 +15795,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/inline/kt21177.kt"); } + @Test + @TestMetadata("kt21177Warning.kt") + public void testKt21177Warning() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/kt21177Warning.kt"); + } + @Test @TestMetadata("kt4869.kt") public void testKt4869() 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 4a07a994e92..ec54deaa0e5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -1163,7 +1163,7 @@ public interface Errors { DiagnosticFactory0 INLINE_PROPERTY_WITH_BACKING_FIELD = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE); DiagnosticFactory0 NON_INTERNAL_PUBLISHED_API = DiagnosticFactory0.create(ERROR); DiagnosticFactoryForDeprecation1 PROTECTED_CALL_FROM_PUBLIC_INLINE = DiagnosticFactoryForDeprecation1.create(LanguageFeature.ProhibitProtectedCallFromInline); - DiagnosticFactory1 PROTECTED_CONSTRUCTOR_CALL_FROM_PUBLIC_INLINE = DiagnosticFactory1.create(WARNING); + DiagnosticFactoryForDeprecation1 PROTECTED_CONSTRUCTOR_CALL_FROM_PUBLIC_INLINE = DiagnosticFactoryForDeprecation1.create(LanguageFeature.ProhibitProtectedConstructorCallFromPublicInline); DiagnosticFactoryForDeprecation1 SUPER_CALL_FROM_PUBLIC_INLINE = DiagnosticFactoryForDeprecation1.create(LanguageFeature.ProhibitSuperCallsFromPublicInline); 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/resolve/calls/checkers/InlineChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/InlineChecker.kt index 579ce80d239..ac15450869b 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 @@ -277,7 +277,9 @@ internal class InlineChecker(private val descriptor: FunctionDescriptor) : CallC calledFunEffectiveVisibility.toVisibility() === Visibilities.Protected) { when { isConstructorCall -> { - context.trace.report(PROTECTED_CONSTRUCTOR_CALL_FROM_PUBLIC_INLINE.on(expression, calledDescriptor)) + context.trace.report( + PROTECTED_CONSTRUCTOR_CALL_FROM_PUBLIC_INLINE.on(context.languageVersionSettings, expression, calledDescriptor) + ) } else -> { context.trace.report( diff --git a/compiler/testData/diagnostics/tests/inline/kt15410.kt b/compiler/testData/diagnostics/tests/inline/kt15410.kt index cb4a847079a..7c8029214f1 100644 --- a/compiler/testData/diagnostics/tests/inline/kt15410.kt +++ b/compiler/testData/diagnostics/tests/inline/kt15410.kt @@ -5,5 +5,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() } diff --git a/compiler/testData/diagnostics/tests/inline/kt21177.kt b/compiler/testData/diagnostics/tests/inline/kt21177.kt index 9d8dcdd28a1..9ddd57fe1de 100644 --- a/compiler/testData/diagnostics/tests/inline/kt21177.kt +++ b/compiler/testData/diagnostics/tests/inline/kt21177.kt @@ -6,13 +6,13 @@ class SomeContainer { protected fun makeLimit(): Limit = TODO() public inline fun foo(f: () -> Unit) { - Limit() + Limit() makeLimit() } } open class A protected constructor() { inline fun foo(f: () -> Unit) { - A() + A() } } diff --git a/compiler/testData/diagnostics/tests/inline/kt21177Warning.fir.kt b/compiler/testData/diagnostics/tests/inline/kt21177Warning.fir.kt new file mode 100644 index 00000000000..bdd049e333a --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/kt21177Warning.fir.kt @@ -0,0 +1,19 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER +// !LANGUAGE: -ProhibitProtectedConstructorCallFromPublicInline + +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/kt21177Warning.kt b/compiler/testData/diagnostics/tests/inline/kt21177Warning.kt new file mode 100644 index 00000000000..0c96644d8bd --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/kt21177Warning.kt @@ -0,0 +1,19 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER +// !LANGUAGE: -ProhibitProtectedConstructorCallFromPublicInline + +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/kt21177Warning.txt b/compiler/testData/diagnostics/tests/inline/kt21177Warning.txt new file mode 100644 index 00000000000..dfe110aad4e --- /dev/null +++ b/compiler/testData/diagnostics/tests/inline/kt21177Warning.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-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 de5fbcaf4e4..cbd6d51b95f 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 @@ -15801,6 +15801,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inline/kt21177.kt"); } + @Test + @TestMetadata("kt21177Warning.kt") + public void testKt21177Warning() throws Exception { + runTest("compiler/testData/diagnostics/tests/inline/kt21177Warning.kt"); + } + @Test @TestMetadata("kt4869.kt") public void testKt4869() throws Exception { diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 132e960469b..8f761ef0336 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -215,6 +215,7 @@ enum class LanguageFeature( UseBuilderInferenceOnlyIfNeeded(KOTLIN_1_6), SuspendConversion(KOTLIN_1_6), ProhibitSuperCallsFromPublicInline(KOTLIN_1_6), + ProhibitProtectedConstructorCallFromPublicInline(KOTLIN_1_6), // 1.7