From 88c43e7f7bbfeb9c38967d3769a0626355850e90 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 18 Feb 2021 18:10:15 +0300 Subject: [PATCH] [FE] Assume that effective visibility of sealed class constructor is internal #KT-45033 Fixed #KT-45043 --- ...irOldFrontendDiagnosticsTestGenerated.java | 12 +++++++++ .../resolve/ExposedVisibilityChecker.kt | 6 ++++- .../sealed/internalTypeInConstructor.fir.kt | 9 +++++++ .../tests/sealed/internalTypeInConstructor.kt | 9 +++++++ .../sealed/internalTypeInConstructor.txt | 16 ++++++++++++ .../sealed/privateTypeInConstructor.fir.kt | 16 ++++++++++++ .../tests/sealed/privateTypeInConstructor.kt | 16 ++++++++++++ .../tests/sealed/privateTypeInConstructor.txt | 26 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 12 +++++++++ 9 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.fir.kt create mode 100644 compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.kt create mode 100644 compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.txt create mode 100644 compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.fir.kt create mode 100644 compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.kt create mode 100644 compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.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 57943e90f00..be4efe9b6a6 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 @@ -24313,6 +24313,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/sealed/inheritorInDifferentModule.kt"); } + @Test + @TestMetadata("internalTypeInConstructor.kt") + public void testInternalTypeInConstructor() throws Exception { + runTest("compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.kt"); + } + @Test @TestMetadata("kt44316.kt") public void testKt44316() throws Exception { @@ -24433,6 +24439,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/sealed/OperationWhen.kt"); } + @Test + @TestMetadata("privateTypeInConstructor.kt") + public void testPrivateTypeInConstructor() throws Exception { + runTest("compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.kt"); + } + @Test @TestMetadata("protectedConstructors_disabled.kt") public void testProtectedConstructors_disabled() throws Exception { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/ExposedVisibilityChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ExposedVisibilityChecker.kt index 99dfa56d74d..efd92ebe785 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/ExposedVisibilityChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ExposedVisibilityChecker.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.diagnostics.DiagnosticFactory3 import org.jetbrains.kotlin.diagnostics.Errors.* import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.visibilityModifier import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.isError @@ -80,7 +81,10 @@ class ExposedVisibilityChecker(private val trace: BindingTrace? = null) { // for checking situation with modified basic visibility visibility: DescriptorVisibility = functionDescriptor.visibility ): Boolean { - val functionVisibility = functionDescriptor.effectiveVisibility(visibility) + var functionVisibility = functionDescriptor.effectiveVisibility(visibility) + if (functionDescriptor is ConstructorDescriptor && functionDescriptor.constructedClass.isSealed() && function.visibilityModifier() == null) { + functionVisibility = EffectiveVisibility.Private + } var result = true if (function !is KtConstructor<*>) { val restricting = functionDescriptor.returnType?.leastPermissiveDescriptor(functionVisibility) diff --git a/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.fir.kt b/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.fir.kt new file mode 100644 index 00000000000..f7ad3c9a727 --- /dev/null +++ b/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.fir.kt @@ -0,0 +1,9 @@ +// ISSUE: KT-45033 +// DIAGNOSTICS: -UNUSED_PARAMETER + +internal class Bar + +sealed class Foo( + internal val x: Bar, + y: Bar +) diff --git a/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.kt b/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.kt new file mode 100644 index 00000000000..b5872a1cbc6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.kt @@ -0,0 +1,9 @@ +// ISSUE: KT-45033 +// DIAGNOSTICS: -UNUSED_PARAMETER + +internal class Bar + +sealed class Foo( + internal val x: Bar, + y: Bar +) diff --git a/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.txt b/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.txt new file mode 100644 index 00000000000..bc5d64e76bc --- /dev/null +++ b/compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.txt @@ -0,0 +1,16 @@ +package + +internal final class Bar { + public constructor Bar() + 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 sealed class Foo { + protected constructor Foo(/*0*/ x: Bar, /*1*/ y: Bar) + internal final val x: Bar + 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/testData/diagnostics/tests/sealed/privateTypeInConstructor.fir.kt b/compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.fir.kt new file mode 100644 index 00000000000..79e080e9b09 --- /dev/null +++ b/compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.fir.kt @@ -0,0 +1,16 @@ +// ISSUE: KT-45043 +// DIAGNOSTICS: -UNUSED_PARAMETER + +private class Bar + +sealed class SealedFoo( + val x: Bar, + private val y: Bar, + z: Bar +) + +abstract class AbstractFoo( + val x: Bar, + private val y: Bar, + z: Bar +) diff --git a/compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.kt b/compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.kt new file mode 100644 index 00000000000..05d13d85b64 --- /dev/null +++ b/compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.kt @@ -0,0 +1,16 @@ +// ISSUE: KT-45043 +// DIAGNOSTICS: -UNUSED_PARAMETER + +private class Bar + +sealed class SealedFoo( + val x: Bar, + private val y: Bar, + z: Bar +) + +abstract class AbstractFoo( + val x: Bar, + private val y: Bar, + z: Bar +) diff --git a/compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.txt b/compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.txt new file mode 100644 index 00000000000..3f33b21eb9d --- /dev/null +++ b/compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.txt @@ -0,0 +1,26 @@ +package + +public abstract class AbstractFoo { + public constructor AbstractFoo(/*0*/ x: Bar, /*1*/ y: Bar, /*2*/ z: Bar) + public final val x: Bar + private final val y: Bar + 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 Bar { + public constructor Bar() + 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 sealed class SealedFoo { + protected constructor SealedFoo(/*0*/ x: Bar, /*1*/ y: Bar, /*2*/ z: Bar) + public final val x: Bar + private final val y: Bar + 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 65516cfb384..99d2bb2e949 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 @@ -24403,6 +24403,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/sealed/inheritorInDifferentModule.kt"); } + @Test + @TestMetadata("internalTypeInConstructor.kt") + public void testInternalTypeInConstructor() throws Exception { + runTest("compiler/testData/diagnostics/tests/sealed/internalTypeInConstructor.kt"); + } + @Test @TestMetadata("kt44316.kt") public void testKt44316() throws Exception { @@ -24523,6 +24529,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/sealed/OperationWhen.kt"); } + @Test + @TestMetadata("privateTypeInConstructor.kt") + public void testPrivateTypeInConstructor() throws Exception { + runTest("compiler/testData/diagnostics/tests/sealed/privateTypeInConstructor.kt"); + } + @Test @TestMetadata("protectedConstructors_disabled.kt") public void testProtectedConstructors_disabled() throws Exception {