[FE] Allow declare sealed class inheritors in different files in one module

#KT-13495
This commit is contained in:
Dmitriy Novozhilov
2020-11-16 11:27:04 +03:00
committed by TeamCityServer
parent 1a377069dd
commit f5f1984a60
20 changed files with 222 additions and 13 deletions
@@ -20778,6 +20778,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/sealed/LocalSealed.kt");
}
@TestMetadata("MultipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/diagnostics/tests/sealed/MultipleFiles_enabled.kt");
}
@TestMetadata("NestedSealed.kt")
public void testNestedSealed() throws Exception {
runTest("compiler/testData/diagnostics/tests/sealed/NestedSealed.kt");
@@ -30255,6 +30255,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/sealed"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("multipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/codegen/box/sealed/multipleFiles_enabled.kt");
}
@TestMetadata("objects.kt")
public void testObjects() throws Exception {
runTest("compiler/testData/codegen/box/sealed/objects.kt");
@@ -323,7 +323,8 @@ class FunctionDescriptorResolver(
scope: LexicalScope,
classDescriptor: ClassDescriptor,
classElement: KtPureClassOrObject,
trace: BindingTrace
trace: BindingTrace,
languageVersionSettings: LanguageVersionSettings
): ClassConstructorDescriptorImpl? {
if (classDescriptor.kind == ClassKind.ENUM_ENTRY || !classElement.hasPrimaryConstructor()) return null
return createConstructorDescriptor(
@@ -333,7 +334,8 @@ class FunctionDescriptorResolver(
classElement.primaryConstructorModifierList,
classElement.primaryConstructor ?: classElement,
classElement.primaryConstructorParameters,
trace
trace,
languageVersionSettings
)
}
@@ -341,7 +343,8 @@ class FunctionDescriptorResolver(
scope: LexicalScope,
classDescriptor: ClassDescriptor,
constructor: KtSecondaryConstructor,
trace: BindingTrace
trace: BindingTrace,
languageVersionSettings: LanguageVersionSettings
): ClassConstructorDescriptorImpl {
return createConstructorDescriptor(
scope,
@@ -350,7 +353,8 @@ class FunctionDescriptorResolver(
constructor.modifierList,
constructor,
constructor.valueParameters,
trace
trace,
languageVersionSettings
)
}
@@ -361,7 +365,8 @@ class FunctionDescriptorResolver(
modifierList: KtModifierList?,
declarationToTrace: KtPureElement,
valueParameters: List<KtParameter>,
trace: BindingTrace
trace: BindingTrace,
languageVersionSettings: LanguageVersionSettings
): ClassConstructorDescriptorImpl {
val constructorDescriptor = ClassConstructorDescriptorImpl.create(
classDescriptor,
@@ -387,7 +392,7 @@ class FunctionDescriptorResolver(
resolveValueParameters(constructorDescriptor, parameterScope, valueParameters, trace, null),
resolveVisibilityFromModifiers(
modifierList,
DescriptorUtils.getDefaultConstructorVisibility(classDescriptor)
DescriptorUtils.getDefaultConstructorVisibility(classDescriptor, languageVersionSettings.supportsFeature(LanguageFeature.FreedomForSealedClasses))
)
)
constructor.returnType = classDescriptor.defaultType
@@ -488,7 +488,7 @@ open class LazyClassMemberScope(
if (DescriptorUtils.canHaveDeclaredConstructors(thisDescriptor) || hasPrimaryConstructor) {
val constructor = c.functionDescriptorResolver.resolvePrimaryConstructorDescriptor(
thisDescriptor.scopeForConstructorHeaderResolution, thisDescriptor, classOrObject, trace
thisDescriptor.scopeForConstructorHeaderResolution, thisDescriptor, classOrObject, trace, c.languageVersionSettings
)
constructor ?: return null
setDeferredReturnType(constructor)
@@ -505,7 +505,7 @@ open class LazyClassMemberScope(
return classOrObject.secondaryConstructors.map { constructor ->
val descriptor = c.functionDescriptorResolver.resolveSecondaryConstructorDescriptor(
thisDescriptor.scopeForConstructorHeaderResolution, thisDescriptor, constructor, trace
thisDescriptor.scopeForConstructorHeaderResolution, thisDescriptor, constructor, trace, c.languageVersionSettings
)
setDeferredReturnType(descriptor)
descriptor
@@ -0,0 +1,24 @@
// ISSUE: KT-13495
// IGNORE_BACKEND_FIR: JVM_IR
// !LANGUAGE: +FreedomForSealedClasses
// FILE: a.kt
sealed class Base {
class A : Base()
}
// FILE: b.kt
class B : Base()
// FILE: c.kt
fun getLetter(base: Base): String = when (base) {
is Base.A -> "O"
is B -> "K"
}
fun box(): String {
return getLetter(Base.A()) + getLetter(B())
}
@@ -0,0 +1,30 @@
// ISSUE: KT-13495
// !DIAGNOSTICS: -UNUSED_VARIABLE
// !LANGUAGE: +FreedomForSealedClasses
// FILE: a.kt
sealed class Base {
class A : Base()
}
// FILE: b.kt
class B : <!HIDDEN!>Base<!>()
// FILE: c.kt
class Container {
class C : <!HIDDEN, SEALED_SUPERTYPE!>Base<!>()
inner class D : <!HIDDEN, SEALED_SUPERTYPE!>Base<!>()
}
// FILE: d.kt
fun test(base: Base) {
val x = when (base) {
is Base.A -> 1
is B -> 2
}
}
@@ -0,0 +1,30 @@
// ISSUE: KT-13495
// !DIAGNOSTICS: -UNUSED_VARIABLE
// !LANGUAGE: +FreedomForSealedClasses
// FILE: a.kt
sealed class Base {
class A : Base()
}
// FILE: b.kt
class B : Base()
// FILE: c.kt
class Container {
class C : <!SEALED_SUPERTYPE!>Base<!>()
inner class D : <!SEALED_SUPERTYPE!>Base<!>()
}
// FILE: d.kt
fun test(base: Base) {
val x = when (base) {
is Base.A -> 1
is B -> 2
}
}
@@ -0,0 +1,45 @@
package
public fun test(/*0*/ base: Base): kotlin.Unit
public final class B : Base {
public constructor B()
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 Base {
internal constructor Base()
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 A : Base {
public constructor A()
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 Container {
public constructor Container()
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 C : Base {
public constructor C()
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 inner class D : Base {
public constructor D()
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
}
}
@@ -20855,6 +20855,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/sealed/LocalSealed.kt");
}
@TestMetadata("MultipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/diagnostics/tests/sealed/MultipleFiles_enabled.kt");
}
@TestMetadata("NestedSealed.kt")
public void testNestedSealed() throws Exception {
runTest("compiler/testData/diagnostics/tests/sealed/NestedSealed.kt");
@@ -20780,6 +20780,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/sealed/LocalSealed.kt");
}
@TestMetadata("MultipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/diagnostics/tests/sealed/MultipleFiles_enabled.kt");
}
@TestMetadata("NestedSealed.kt")
public void testNestedSealed() throws Exception {
runTest("compiler/testData/diagnostics/tests/sealed/NestedSealed.kt");
@@ -32026,6 +32026,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/sealed"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@TestMetadata("multipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/codegen/box/sealed/multipleFiles_enabled.kt");
}
@TestMetadata("objects.kt")
public void testObjects() throws Exception {
runTest("compiler/testData/codegen/box/sealed/objects.kt");
@@ -29660,6 +29660,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/sealed"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@TestMetadata("multipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/codegen/box/sealed/multipleFiles_enabled.kt");
}
@TestMetadata("objects.kt")
public void testObjects() throws Exception {
runTest("compiler/testData/codegen/box/sealed/objects.kt");
@@ -30255,6 +30255,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/sealed"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("multipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/codegen/box/sealed/multipleFiles_enabled.kt");
}
@TestMetadata("objects.kt")
public void testObjects() throws Exception {
runTest("compiler/testData/codegen/box/sealed/objects.kt");
@@ -143,6 +143,8 @@ enum class LanguageFeature(
UseCorrectExecutionOrderForVarargArguments(KOTLIN_1_5, kind = BUG_FIX),
JvmRecordSupport(KOTLIN_1_5),
FreedomForSealedClasses(KOTLIN_1_5),
// Temporarily disabled, see KT-27084/KT-22379
SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX),
@@ -35,10 +35,14 @@ import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getB
public class DescriptorFactory {
private static class DefaultClassConstructorDescriptor extends ClassConstructorDescriptorImpl {
public DefaultClassConstructorDescriptor(@NotNull ClassDescriptor containingClass, @NotNull SourceElement source) {
public DefaultClassConstructorDescriptor(
@NotNull ClassDescriptor containingClass,
@NotNull SourceElement source,
boolean freedomForSealedInterfacesSupported
) {
super(containingClass, null, Annotations.Companion.getEMPTY(), true, Kind.DECLARATION, source);
initialize(Collections.<ValueParameterDescriptor>emptyList(),
getDefaultConstructorVisibility(containingClass));
getDefaultConstructorVisibility(containingClass, freedomForSealedInterfacesSupported));
}
}
@@ -130,7 +134,11 @@ public class DescriptorFactory {
@NotNull ClassDescriptor containingClass,
@NotNull SourceElement source
) {
return new DefaultClassConstructorDescriptor(containingClass, source);
/*
* Language version settings are needed here only for computing default visibility of constructors of sealed classes
* Since object can not be sealed class it's OK to pass default settings here
*/
return new DefaultClassConstructorDescriptor(containingClass, source, false);
}
@NotNull
@@ -380,11 +380,21 @@ public class DescriptorUtils {
}
@NotNull
public static DescriptorVisibility getDefaultConstructorVisibility(@NotNull ClassDescriptor classDescriptor) {
public static DescriptorVisibility getDefaultConstructorVisibility(
@NotNull ClassDescriptor classDescriptor,
boolean freedomForSealedInterfacesSupported
) {
ClassKind classKind = classDescriptor.getKind();
if (classKind == ClassKind.ENUM_CLASS || classKind.isSingleton() || isSealedClass(classDescriptor)) {
if (classKind == ClassKind.ENUM_CLASS || classKind.isSingleton()) {
return DescriptorVisibilities.PRIVATE;
}
if (isSealedClass(classDescriptor)) {
if (freedomForSealedInterfacesSupported) {
return DescriptorVisibilities.INTERNAL;
} else {
return DescriptorVisibilities.PRIVATE;
}
}
if (isAnonymousObject(classDescriptor)) {
return DescriptorVisibilities.DEFAULT_VISIBILITY;
}
@@ -24526,6 +24526,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/sealed"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@TestMetadata("multipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/codegen/box/sealed/multipleFiles_enabled.kt");
}
@TestMetadata("objects.kt")
public void testObjects() throws Exception {
runTest("compiler/testData/codegen/box/sealed/objects.kt");
@@ -24526,6 +24526,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/sealed"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("multipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/codegen/box/sealed/multipleFiles_enabled.kt");
}
@TestMetadata("objects.kt")
public void testObjects() throws Exception {
runTest("compiler/testData/codegen/box/sealed/objects.kt");
@@ -24526,6 +24526,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/sealed"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true);
}
@TestMetadata("multipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/codegen/box/sealed/multipleFiles_enabled.kt");
}
@TestMetadata("objects.kt")
public void testObjects() throws Exception {
runTest("compiler/testData/codegen/box/sealed/objects.kt");
@@ -12997,6 +12997,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/sealed"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
@TestMetadata("multipleFiles_enabled.kt")
public void testMultipleFiles_enabled() throws Exception {
runTest("compiler/testData/codegen/box/sealed/multipleFiles_enabled.kt");
}
@TestMetadata("objects.kt")
public void testObjects() throws Exception {
runTest("compiler/testData/codegen/box/sealed/objects.kt");