fixed recursive type issue

This commit is contained in:
pca006132 2020-12-17 17:15:00 +08:00 committed by pca006132
parent 4a4ce14c47
commit 57c7104bf3
3 changed files with 18 additions and 0 deletions

View File

@ -32,6 +32,8 @@ def find_subst(ctx: dict[str, Type],
else:
if a not in b.constraints:
return f"{b} cannot take value of {a}"
if b in a.get_vars():
return "Recursive type is not supported"
sub[b.name] = a
return sub

View File

@ -54,5 +54,6 @@ try_case(TupleType([X, X]), TupleType([X, Y]), {})
try_case(TupleType([X, X]), TupleType([Y, Y]), {})
try_case(TupleType([X, Y]), TupleType([X, X]), {})
try_case(TupleType([X, Y]), X, {})
try_case(TupleType([i32, Y]), X, {})

View File

@ -2,6 +2,9 @@ class Type:
def __eq__(self, other):
return False
def get_vars(self):
return []
class BotType:
def __eq__(self, other):
@ -38,6 +41,9 @@ class TypeVariable(Type):
def __eq__(self, other):
return isinstance(other, TypeVariable) and self.name == other.name
def get_vars(self):
return [self]
class ClassType(Type):
name: str
@ -83,6 +89,15 @@ class ParametricType(Type):
return False
return True
def get_vars(self):
result = []
for p in self.params:
vars = p.get_vars()
for v in vars:
if v not in result:
result.append(v)
return result
class ListType(ParametricType):
def __init__(self, param: Type):
super().__init__([param])