fixed bot type

This commit is contained in:
pca006132 2020-12-21 09:38:37 +08:00 committed by pca006132
parent 92c92f6a4c
commit 32f0135f28
3 changed files with 13 additions and 4 deletions

View File

@ -18,11 +18,17 @@ def find_subst(ctx: dict[str, Type],
if isinstance(a, TypeVariable) and a.name in ctx: if isinstance(a, TypeVariable) and a.name in ctx:
a = ctx[a.name] a = ctx[a.name]
if isinstance(a, BotType):
return sub
if isinstance(b, TypeVariable): if isinstance(b, TypeVariable):
if b.name in sub: if b.name in sub:
b = sub[b.name] b = sub[b.name]
else: else:
if len(b.constraints) > 0: if len(b.constraints) > 0:
# we cannot handle BotType correctly
# but that would not be a problem, as the user cannot get
# BotType in normal circumstances.
if isinstance(a, TypeVariable): if isinstance(a, TypeVariable):
if len(a.constraints) == 0: if len(a.constraints) == 0:
return f"{b} cannot take value of an unconstrained variable {a}" return f"{b} cannot take value of an unconstrained variable {a}"
@ -42,8 +48,6 @@ def find_subst(ctx: dict[str, Type],
else: else:
return f"{a} can take values other than {b}" return f"{a} can take values other than {b}"
if isinstance(a, BotType):
return sub
# TODO: virtual type is not handled currently # TODO: virtual type is not handled currently
# we need to access the class dictionary to handle this # we need to access the class dictionary to handle this
if type(a) == type(b): if type(a) == type(b):

View File

@ -95,8 +95,13 @@ class Foo:
def __ge__(self, other: Foo) -> bool: def __ge__(self, other: Foo) -> bool:
pass pass
def find(ls: list[I], x: I) -> int32:
pass
""" """
ctx, _ = parse_top_level(ctx, ast.parse(test_classes)) ctx, _ = parse_top_level(ctx, ast.parse(test_classes))
test_expr('Foo(1) + Foo(1)', {}) test_expr('Foo(1) + Foo(1)', {})
test_expr('Foo(1) + Foo(1) < Foo(2) + Foo(3) < Foo(4)', {}) test_expr('Foo(1) + Foo(1) < Foo(2) + Foo(3) < Foo(4)', {})
test_expr('find([1, 2, 3], 1)', {})
test_expr('find([], 1)', {})

View File

@ -125,13 +125,13 @@ def parse_top_level(ctx: Context, module: ast.Module):
function_stmts += parse_class(ctx, element) function_stmts += parse_class(ctx, element)
elif isinstance(element, ast.FunctionDef): elif isinstance(element, ast.FunctionDef):
name = element.name name = element.name
if name in functions: if name in ctx.functions:
raise CustomError(f"Duplicated function name {name}") raise CustomError(f"Duplicated function name {name}")
if name in ctx.types: if name in ctx.types:
raise CustomError(f"Function name {name} clashed with type name") raise CustomError(f"Function name {name} clashed with type name")
args, result, var = parse_function(ctx, None, element) args, result, var = parse_function(ctx, None, element)
ctx.functions[name] = (args, result, var) ctx.functions[name] = (args, result, var)
function_stmts += element function_stmts.append(element)
return ctx, function_stmts return ctx, function_stmts