Skip to content

Commit

Permalink
better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
shiinamiyuki committed Oct 23, 2024
1 parent 62eb535 commit 85cdbb1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
10 changes: 6 additions & 4 deletions luisa_lang/hir/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion luisa_lang/hir/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 3 additions & 2 deletions luisa_lang/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 85cdbb1

Please sign in to comment.