Delegation is only allowed if the target type is a trait

This commit is contained in:
Andrey Breslav
2011-09-27 10:25:24 +04:00
parent c6c87ec3c8
commit 0cb2765ab0
3 changed files with 23 additions and 0 deletions
@@ -141,6 +141,7 @@ public interface Errors {
SimpleDiagnosticFactory MANY_CLASS_OBJECTS = SimpleDiagnosticFactory.create(ERROR, "Only one class object is allowed per class");
SimpleDiagnosticFactory CLASS_OBJECT_NOT_ALLOWED = SimpleDiagnosticFactory.create(ERROR, "A class object is not allowed here");
SimpleDiagnosticFactory DELEGATION_IN_TRAIT = SimpleDiagnosticFactory.create(ERROR, "Traits cannot use delegation");
SimpleDiagnosticFactory DELEGATION_NOT_TO_TRAIT = SimpleDiagnosticFactory.create(ERROR, "Only traits can be delegated to");
SimpleDiagnosticFactory NO_CONSTRUCTOR = SimpleDiagnosticFactory.create(ERROR, "This class does not have a constructor");
SimpleDiagnosticFactory NOT_A_CLASS = SimpleDiagnosticFactory.create(ERROR, "Not a class");
SimpleDiagnosticFactory ILLEGAL_ESCAPE_SEQUENCE = SimpleDiagnosticFactory.create(ERROR, "Illegal escape sequence");
@@ -136,6 +136,15 @@ public class BodyResolver {
}
JetType supertype = context.getTrace().getBindingContext().get(BindingContext.TYPE, specifier.getTypeReference());
recordSupertype(specifier.getTypeReference(), supertype);
if (supertype != null) {
DeclarationDescriptor declarationDescriptor = supertype.getConstructor().getDeclarationDescriptor();
if (declarationDescriptor instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor;
if (classDescriptor.getKind() != ClassKind.TRAIT) {
context.getTrace().report(DELEGATION_NOT_TO_TRAIT.on(specifier.getTypeReference()));
}
}
}
JetExpression delegateExpression = specifier.getDelegateExpression();
if (delegateExpression != null) {
JetScope scope = scopeForConstructor == null
@@ -0,0 +1,13 @@
open class Foo() {
}
class Barrr() : <!DELEGATION_NOT_TO_TRAIT!>Foo<!> by Foo() {}
trait T {}
class Br(t : T) : T by t {}
open enum class E() {}
class Test2(e : E) : <!DELEGATION_NOT_TO_TRAIT!>E<!> by e {}