'sealed' is now not 'final' by default + related code changes #KT-10266 Fixed

This commit is contained in:
Mikhail Glukhikh
2015-12-07 19:08:30 +03:00
parent 5b72afe8a0
commit c73f01927a
11 changed files with 85 additions and 13 deletions
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.descriptors.ClassKind;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.Modality;
import org.jetbrains.kotlin.diagnostics.Errors;
import org.jetbrains.kotlin.lexer.KtToken;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.BindingContext;
@@ -60,7 +59,7 @@ public final class WhenChecker {
if (type == null) return null;
ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(type);
if (classDescriptor == null) return null;
if (classDescriptor.getKind() != ClassKind.ENUM_CLASS || classDescriptor.getModality().isOverridable()) return null;
if (classDescriptor.getKind() != ClassKind.ENUM_CLASS) return null;
return classDescriptor;
}
@@ -488,8 +488,8 @@ public class BodyResolver {
trace.report(SINGLETON_IN_SUPERTYPE.on(typeReference));
}
}
else if (constructor.isFinal() && !allowedFinalSupertypes.contains(constructor)) {
if (classDescriptor.getModality() == Modality.SEALED) {
else if (!allowedFinalSupertypes.contains(constructor)) {
if (classDescriptor != null && classDescriptor.getModality() == Modality.SEALED) {
DeclarationDescriptor containingDescriptor = supertypeOwner.getContainingDeclaration();
while (containingDescriptor != null && containingDescriptor != classDescriptor) {
containingDescriptor = containingDescriptor.getContainingDeclaration();
@@ -501,7 +501,7 @@ public class BodyResolver {
trace.report(SEALED_SUPERTYPE_IN_LOCAL_CLASS.on(typeReference));
}
}
else {
else if (constructor.isFinal()) {
trace.report(FINAL_SUPERTYPE.on(typeReference));
}
}
@@ -614,10 +614,6 @@ public class DescriptorResolver {
) {
if (DeclarationsCheckerKt.checkNotEnumEntry(upperBound, trace)) return;
if (!TypeUtils.canHaveSubtypes(KotlinTypeChecker.DEFAULT, upperBoundType)) {
ClassifierDescriptor descriptor = upperBoundType.getConstructor().getDeclarationDescriptor();
if (descriptor instanceof ClassDescriptor) {
if (((ClassDescriptor) descriptor).getModality() == Modality.SEALED) return;
}
trace.report(FINAL_UPPER_BOUND.on(upperBound, upperBoundType));
}
if (DynamicTypesKt.isDynamic(upperBoundType)) {
@@ -345,7 +345,7 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
/*
* (a: SubjectType) is Type
*/
private void checkTypeCompatibility(
private static void checkTypeCompatibility(
@NotNull ExpressionTypingContext context,
@Nullable KotlinType type,
@NotNull KotlinType subjectType,
@@ -15,7 +15,7 @@ sealed class Sealed(val x: Int) {
fun foo(s: Sealed): Int {
return <!NO_ELSE_IN_WHEN!>when<!>(s) {
is Sealed.First -> 1
!is <!INCOMPATIBLE_TYPES!>Sealed.ITuple<!> -> 0
!is Sealed.ITuple -> 0
// else required
}
}
@@ -0,0 +1,10 @@
interface Parent
interface Child : Parent
sealed class Page : Parent {
object One : Page(), Child
object Two : Page(), Child
}
// Ok: page is a Parent so it can be easily a Child
fun test(page: Page): Boolean = page is Child
@@ -0,0 +1,36 @@
package
public fun test(/*0*/ page: Page): kotlin.Boolean
public interface Child : Parent {
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 Page : Parent {
private constructor Page()
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 object One : Page, Child {
private constructor One()
public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
}
public object Two : Page, Child {
private constructor Two()
public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public interface Parent {
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
}
@@ -0,0 +1,8 @@
sealed class My(open val x: Int?) {
init {
if (x != null) {
// Should be error: property is open
<!SMARTCAST_IMPOSSIBLE!>x<!>.hashCode()
}
}
}
@@ -0,0 +1,9 @@
package
public sealed class My {
private constructor My(/*0*/ x: kotlin.Int?)
public open val x: kotlin.Int?
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
}
@@ -14300,6 +14300,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/sealed/WhenOnEmptySealed.kt");
doTest(fileName);
}
@TestMetadata("WithInterface.kt")
public void testWithInterface() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/sealed/WithInterface.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/diagnostics/tests/secondaryConstructors")
@@ -15132,6 +15138,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("openInSealed.kt")
public void testOpenInSealed() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/openInSealed.kt");
doTest(fileName);
}
@TestMetadata("ownerDeclaresBothModifies.kt")
public void testOwnerDeclaresBothModifies() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/ownerDeclaresBothModifies.kt");
@@ -16,10 +16,12 @@
package org.jetbrains.kotlin.descriptors
enum class Modality private constructor(@get:JvmName("isOverridable") val isOverridable: Boolean) {
// For sealed classes, isOverridable is false but isOverridableByMembers is true
enum class Modality private constructor(val isOverridable: Boolean) {
// THE ORDER OF ENTRIES MATTERS HERE
FINAL(false),
SEALED(false),
// NB: class can be sealed but not function or property
SEALED(true),
OPEN(true),
ABSTRACT(true);