From 3dbea40cae732ed9889b220becba309c925bd94c Mon Sep 17 00:00:00 2001 From: aph Date: Tue, 29 Jan 2013 22:34:37 +0000 Subject: [PATCH] Disallow __init__ in interfaces: fixes #52 --- checker.py | 15 ++++++++++++--- messages.py | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/checker.py b/checker.py index a6181f3fa8fd..100f8f4b8402 100644 --- a/checker.py +++ b/checker.py @@ -338,13 +338,22 @@ def infer_local_variable_type(self, x, y, z): Type visit_type_def(self, TypeDef defn): """Type check a type definition (class or interface).""" - typ = self.lookup(defn.name, GDEF).node + typ = (TypeInfo)self.lookup(defn.name, GDEF).node self.errors.set_type(defn.name, defn.is_interface) - self.check_unique_interface_implementations((TypeInfo)typ) - self.check_interface_errors((TypeInfo)typ) + self.check_unique_interface_implementations(typ) + self.check_interface_errors(typ) + self.check_no_constructor_if_interface(typ) self.accept(defn.defs) self.report_error_if_statements_in_class_body(defn.defs) self.errors.set_type(None, False) + + void check_no_constructor_if_interface(self, TypeInfo typ): + if not typ.is_interface: + return + ctor = typ.get_method('__init__') + if ctor is None: + return + self.msg.interface_has_constructor(ctor) void check_unique_interface_implementations(self, TypeInfo typ): """Check that each interface is implemented only once.""" diff --git a/messages.py b/messages.py index e3fe6aecb773..0ef0538a9efe 100644 --- a/messages.py +++ b/messages.py @@ -480,6 +480,9 @@ class MessageBuilder: else: return False + void interface_has_constructor(self, Context context): + self.fail('An interface must not define a constructor', context) + str capitalize(str s): """Capitalize the first character of a string."""