From 32f0135f2873f01fbc470827d45b787767ebb7fd Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 21 Dec 2020 09:38:37 +0800 Subject: [PATCH] fixed bot type --- toy-impl/inference.py | 8 ++++++-- toy-impl/test_expr.py | 5 +++++ toy-impl/top_level.py | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/toy-impl/inference.py b/toy-impl/inference.py index 69bef9b..30157e9 100644 --- a/toy-impl/inference.py +++ b/toy-impl/inference.py @@ -18,11 +18,17 @@ def find_subst(ctx: dict[str, Type], if isinstance(a, TypeVariable) and a.name in ctx: a = ctx[a.name] + if isinstance(a, BotType): + return sub + if isinstance(b, TypeVariable): if b.name in sub: b = sub[b.name] else: 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 len(a.constraints) == 0: return f"{b} cannot take value of an unconstrained variable {a}" @@ -42,8 +48,6 @@ def find_subst(ctx: dict[str, Type], else: return f"{a} can take values other than {b}" - if isinstance(a, BotType): - return sub # TODO: virtual type is not handled currently # we need to access the class dictionary to handle this if type(a) == type(b): diff --git a/toy-impl/test_expr.py b/toy-impl/test_expr.py index e12e006..6caa5b2 100644 --- a/toy-impl/test_expr.py +++ b/toy-impl/test_expr.py @@ -95,8 +95,13 @@ class Foo: def __ge__(self, other: Foo) -> bool: pass + +def find(ls: list[I], x: I) -> int32: + pass """ ctx, _ = parse_top_level(ctx, ast.parse(test_classes)) test_expr('Foo(1) + Foo(1)', {}) test_expr('Foo(1) + Foo(1) < Foo(2) + Foo(3) < Foo(4)', {}) +test_expr('find([1, 2, 3], 1)', {}) +test_expr('find([], 1)', {}) diff --git a/toy-impl/top_level.py b/toy-impl/top_level.py index de77e61..c6dd59a 100644 --- a/toy-impl/top_level.py +++ b/toy-impl/top_level.py @@ -125,13 +125,13 @@ def parse_top_level(ctx: Context, module: ast.Module): function_stmts += parse_class(ctx, element) elif isinstance(element, ast.FunctionDef): name = element.name - if name in functions: + if name in ctx.functions: raise CustomError(f"Duplicated function name {name}") if name in ctx.types: raise CustomError(f"Function name {name} clashed with type name") args, result, var = parse_function(ctx, None, element) ctx.functions[name] = (args, result, var) - function_stmts += element + function_stmts.append(element) return ctx, function_stmts