From 85cdbb107b1332a092ce8d85b0149136171ced64 Mon Sep 17 00:00:00 2001 From: Xiaochun Tong Date: Wed, 23 Oct 2024 05:14:17 -0400 Subject: [PATCH] better error message --- luisa_lang/hir/defs.py | 10 ++++++---- luisa_lang/hir/infer.py | 4 +++- luisa_lang/lang.py | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/luisa_lang/hir/defs.py b/luisa_lang/hir/defs.py index fe6b329..8a76f04 100644 --- a/luisa_lang/hir/defs.py +++ b/luisa_lang/hir/defs.py @@ -417,14 +417,16 @@ def member(self, field: Any) -> Optional['Type']: class StructType(Type): name: str + display_name: str _fields: List[Tuple[str, Type]] _field_dict: Dict[str, Type] # _monomorphification_cache: Dict[Tuple['GenericParameter', Type | 'Value'], Type] - def __init__(self, name: str, fields: List[Tuple[str, Type]]) -> None: + def __init__(self, name: str, display_name: str, fields: List[Tuple[str, Type]]) -> None: super().__init__() self.name = name self._fields = fields + self.display_name = display_name self._field_dict = {name: ty for name, ty in fields} @property @@ -438,7 +440,7 @@ def align(self) -> int: return max(field.align() for _, field in self.fields) def __str__(self) -> str: - return self.name + return self.display_name @override def __eq__(self, value: object) -> bool: @@ -920,8 +922,8 @@ def __init__(self, node: Node | None, message: str) -> None: def __str__(self) -> str: if self.node is None: - return f"Type inference error: {self.message}" - return f"Type inference error at {self.node.span}: {self.message}" + return f"Type inference error:\n\t{self.message}" + return f"Type inference error at {self.node.span}:\n\t{self.message}" class TypeRule(ABC): diff --git a/luisa_lang/hir/infer.py b/luisa_lang/hir/infer.py index 1983d2a..ca28591 100644 --- a/luisa_lang/hir/infer.py +++ b/luisa_lang/hir/infer.py @@ -247,7 +247,7 @@ def _infer_call_helper( # traceback.print_exc() raise hir.TypeInferenceError( node, - f"Failed to resolve function template {f.name}: {e}") + f"Error during instantiating function template {f.name}: {e}") else: resolved_f = f.resolve(None) node.op = hir.Constant(resolved_f) @@ -306,6 +306,7 @@ def infer_binop(name: str, rname: str) -> Optional[Tuple[hir.Type, hir.FunctionL f"Operator {op} not defined for types {left} and {right}" ) except hir.TypeInferenceError as e: + e.node = expr raise e method_names = BINOP_TO_METHOD_NAMES[op] @@ -339,6 +340,7 @@ def helper(): def run_inference_on_function(func: hir.Function) -> None: + # print(f"Running type inference on function {func.name}") inferencer = FuncTypeInferencer(func) inferencer.infer() diff --git a/luisa_lang/lang.py b/luisa_lang/lang.py index e91d9fb..0797f04 100644 --- a/luisa_lang/lang.py +++ b/luisa_lang/lang.py @@ -57,9 +57,10 @@ def parsing_func(args: hir.FunctionTemplateResolvingArgs) -> hir.FunctionLike: raise hir.TypeInferenceError( None, f"type parameter {p} is not resolved") parsing_ctx.bound_type_vars[p.name] = mapping[p] - print(f'binding {p.name} = {mapping[p]}') + # print(f'binding {p.name} = {mapping[p]}') func_parser = parse.FuncParser(func_name, f, parsing_ctx, self_type) func_ir = func_parser.parse_body() + hir.run_inference_on_function(func_ir) return func_ir return hir.FunctionTemplate(func_name, params, parsing_func, is_generic) @@ -112,7 +113,7 @@ def get_ir_type(var_ty: VarType) -> hir.Type: for name, field in cls_info.fields.items(): fields.append((name, get_ir_type(field))) ir_ty = hir.StructType( - f'{cls.__name__}_{unique_hash(cls.__qualname__)}', fields) + f'{cls.__name__}_{unique_hash(cls.__qualname__)}', cls.__qualname__, fields) ctx.types[cls] = ir_ty for name, method in cls_info.methods.items():