Gradualizer icon indicating copy to clipboard operation
Gradualizer copied to clipboard

Tracing errors out of the AST

Open zuiderkwast opened this issue 6 years ago • 1 comments

A trace from the top level form to the exact point where a type error is detected by collecting all the nested expressions, by backwards-recursively catching and rethrowing it with a level of context added. Pseudo code:

some_code_checing_expr(Expr) ->
    try
        check_all_sub_expressions(Expr)
    catch
        throw:{type_error, ..., Context, ...}:Stacktrace ->
            erlang:raise(throw, {type_error, ..., [Expr | Context], ...}, Stacktrace)
    end.

This can then for example be reported as "The variable X in the binary operation X + Y in the function f(X, Y) -> X + Y has type atom() but the expected type is number()".

This is not very readable though. It's better to show pretty-printed source code, with the inner-most expression highlighted, underlined, etc. If the context is given in number of lines, we can just include as many levels as we find fits, by checking the annotations while traversing the nested expression.

zuiderkwast avatar Oct 20 '19 22:10 zuiderkwast

Yes, this would be nice to have. GHC (the Haskell compiler) does this to some extent. It would require some care to make sure that it's readable.

josefs avatar Oct 21 '19 14:10 josefs