Prohibit explicit super call for enum ctrs

This commit is contained in:
Denis Zharkov
2015-02-25 18:12:00 +03:00
parent 1703e16913
commit a872a51a19
9 changed files with 266 additions and 5 deletions
@@ -160,6 +160,7 @@ public interface Errors {
DiagnosticFactory0<JetDelegatorToSuperCall> SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetConstructorDelegationCall> PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INIT_KEYWORD_BEFORE_CLASS_INITIALIZER_EXPECTED = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<JetConstructorDelegationCall> DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR = DiagnosticFactory0.create(ERROR);
// Trait-specific
@@ -417,6 +417,7 @@ public class DefaultErrorMessages {
MAP.put(SECONDARY_CONSTRUCTOR_IN_OBJECT, "Constructors are not allowed for objects");
MAP.put(SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR, "Supertype initialization is impossible without primary constructor");
MAP.put(PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED, "Primary constructor call expected");
MAP.put(DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR, "Call to super is not allowed in enum constructor");
MAP.put(INIT_KEYWORD_BEFORE_CLASS_INITIALIZER_EXPECTED, "Expecting 'init' keyword before class initializer");
@@ -28,4 +28,8 @@ public class JetConstructorDelegationReferenceExpression extends JetExpressionIm
public boolean isThis() {
return findChildByType(JetTokens.THIS_KEYWORD) != null;
}
public boolean isEmpty() {
return getFirstChild() == null;
}
}
@@ -171,6 +171,10 @@ public class BodyResolver {
if (call == null || call.getCalleeExpression() == null) return;
JetType superClassType = DescriptorUtils.getSuperClassType(descriptor.getContainingDeclaration());
if (descriptor.getContainingDeclaration().getKind() == ClassKind.ENUM_CLASS && call.getCalleeExpression().isEmpty()) {
return;
}
OverloadResolutionResults<?> results = callResolver.resolveFunctionCall(
trace, scope,
CallMaker.makeCall(ReceiverValue.NO_RECEIVER, null, call),
@@ -568,8 +568,7 @@ public class DeclarationsChecker {
List<JetDelegationSpecifier> delegationSpecifiers = enumEntry.getDelegationSpecifiers();
ConstructorDescriptor constructor = enumClass.getUnsubstitutedPrimaryConstructor();
assert constructor != null;
if (!constructor.getValueParameters().isEmpty() && delegationSpecifiers.isEmpty()) {
if ((constructor == null || !constructor.getValueParameters().isEmpty()) && delegationSpecifiers.isEmpty()) {
trace.report(ENUM_ENTRY_SHOULD_BE_INITIALIZED.on(enumEntry, enumClass));
}
@@ -52,9 +52,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.kotlin.diagnostics.Errors.EXPECTED_PRIMARY_CONSTRUCTOR_DELEGATION_CALL;
import static org.jetbrains.kotlin.diagnostics.Errors.NOT_A_CLASS;
import static org.jetbrains.kotlin.diagnostics.Errors.NO_CONSTRUCTOR;
import static org.jetbrains.kotlin.diagnostics.Errors.*;
import static org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilPackage.recordScopeAndDataFlowInfo;
import static org.jetbrains.kotlin.resolve.calls.CallResolverUtil.ResolveArgumentsMode.RESOLVE_FUNCTION_ARGUMENTS;
import static org.jetbrains.kotlin.resolve.calls.CallResolverUtil.ResolveArgumentsMode.SHAPE_FUNCTION_ARGUMENTS;
@@ -297,6 +295,10 @@ public class CallResolver {
@NotNull JetConstructorDelegationReferenceExpression calleeExpression
) {
ClassDescriptor currentClassDescriptor = getClassDescriptorByConstructorContext(context);
if (currentClassDescriptor.getKind() == ClassKind.ENUM_CLASS && !calleeExpression.isThis()) {
context.trace.report(DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR.on((JetConstructorDelegationCall) calleeExpression.getParent()));
return checkArgumentTypesAndFail(context);
}
ClassDescriptor delegateClassDescriptor = calleeExpression.isThis() ? currentClassDescriptor :
DescriptorUtilPackage.getSuperClassOrAny(currentClassDescriptor);