Commit a1a9860c authored by Tom Niget's avatar Tom Niget

Fix inheritance unification

parent 4a3f371d
...@@ -40,6 +40,9 @@ class BaseType(ABC): ...@@ -40,6 +40,9 @@ class BaseType(ABC):
for p in cur.get_parents(): for p in cur.get_parents():
queue.put(p) queue.put(p)
def inherits_from(self, other: "BaseType") -> bool:
return other in self.iter_hierarchy_recursive()
def resolve(self) -> "BaseType": def resolve(self) -> "BaseType":
return self return self
...@@ -219,25 +222,27 @@ class TypeOperator(BaseType, ABC): ...@@ -219,25 +222,27 @@ class TypeOperator(BaseType, ABC):
return other.unify_internal(self) return other.unify_internal(self)
if self.is_protocol and not other.is_protocol: if self.is_protocol and not other.is_protocol:
return other.matches_protocol(self) # TODO: doesn't print the correct type in the error message return other.matches_protocol(self) # TODO: doesn't print the correct type in the error message
if len(self.args) < len(other.args):
return other.unify_internal(self)
assert self.is_protocol == other.is_protocol assert self.is_protocol == other.is_protocol
if type(self) != type(other): if type(self) != type(other): # and ((TY_NONE not in {self, other}) or isinstance(({self, other} - {TY_NONE}).pop(), UnionType)):
for parent in other.get_parents(): if self.inherits_from(other) or other.inherits_from(self):
try: return
self.unify(parent) # for parent in other.get_parents():
except TypeMismatchError: # try:
pass # self.unify(parent)
else: # except TypeMismatchError:
return # pass
for parent in self.get_parents(): # else:
try: # return
parent.unify(other) # for parent in self.get_parents():
except TypeMismatchError: # try:
pass # parent.unify(other)
else: # except TypeMismatchError:
return # pass
# else:
# return
raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE) raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE)
if len(self.args) < len(other.args):
return other.unify_internal(self)
if len(self.args) == 0: if len(self.args) == 0:
if self.name != other.name: if self.name != other.name:
raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE) raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment