Prohibit extending kotlin.Enum directly

#KT-7773 Fixed
This commit is contained in:
Dmitry Petrov
2017-05-24 16:17:28 +03:00
parent b9e45bcd54
commit 902d3af280
7 changed files with 62 additions and 3 deletions
@@ -49,9 +49,7 @@ import java.util.List;
import java.util.Map;
import static org.jetbrains.kotlin.diagnostics.PositioningStrategies.*;
import static org.jetbrains.kotlin.diagnostics.Severity.ERROR;
import static org.jetbrains.kotlin.diagnostics.Severity.INFO;
import static org.jetbrains.kotlin.diagnostics.Severity.WARNING;
import static org.jetbrains.kotlin.diagnostics.Severity.*;
/**
* For error messages, see DefaultErrorMessages and IdeErrorMessages.
@@ -272,6 +270,8 @@ public interface Errors {
DiagnosticFactory0<KtNullableType> NULLABLE_SUPERTYPE = DiagnosticFactory0.create(ERROR, NULLABLE_TYPE);
DiagnosticFactory0<KtTypeReference> DYNAMIC_SUPERTYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtTypeReference, ClassDescriptor> CLASS_CANNOT_BE_EXTENDED_DIRECTLY = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<KtElement> MISSING_CONSTRUCTOR_KEYWORD = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NON_PRIVATE_CONSTRUCTOR_IN_ENUM = DiagnosticFactory0.create(ERROR);
@@ -541,6 +541,7 @@ public class DefaultErrorMessages {
MAP.put(SEALED_SUPERTYPE, "This type is sealed, so it can be inherited by only its own nested classes or objects");
MAP.put(SEALED_SUPERTYPE_IN_LOCAL_CLASS, "Local class cannot extend a sealed class");
MAP.put(SINGLETON_IN_SUPERTYPE, "Cannot inherit from a singleton");
MAP.put(CLASS_CANNOT_BE_EXTENDED_DIRECTLY, "Class {0} cannot be extended directly", NAME);
MAP.put(CYCLIC_CONSTRUCTOR_DELEGATION_CALL, "There's a cycle in the delegation calls chain");
MAP.put(CONSTRUCTOR_IN_OBJECT, "Constructors are not allowed for objects");
@@ -584,6 +584,9 @@ public class BodyResolver {
else if (ModalityKt.isFinalOrEnum(classDescriptor)) {
trace.report(FINAL_SUPERTYPE.on(typeReference));
}
else if (KotlinBuiltIns.isEnum(classDescriptor)) {
trace.report(CLASS_CANNOT_BE_EXTENDED_DIRECTLY.on(typeReference, classDescriptor));
}
}
}
}