feat(tvix/eval): implement initial fancy formatting for errors

This very closely follows the way it's done for warnings, but errors
have a lot more information available in some cases which we do not
surface yet.

Note also that due to requiring the `CodeMap`, this is not yet called
from eval.rs as the way that is threaded through needs to be
refactored, so only the method for reporting these errors as strings
is implemented so far.

Next steps for this will be to add a generic diagnostics module that
reduces some of the boilerplate for this between warnings & errors,
and which will also give us a good point in the future to switch to a
fancier diagnostics crate.

Change-Id: If6bb209f8e7a568d866e516a90335b9b2afbf66d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6534
Reviewed-by: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2022-09-12 16:12:43 +03:00 committed by tazjin
parent 7fd7a4465b
commit 0f59fe6601
3 changed files with 145 additions and 27 deletions

View file

@ -1114,21 +1114,17 @@ impl Compiler<'_, '_> {
self.scope_mut().poison(global_ident, depth);
}
let mut shadowed = false;
for other in self.scope().locals.iter().rev() {
if other.has_name(&name) && other.depth == depth {
shadowed = true;
self.emit_error(
self.span_for(node),
ErrorKind::VariableAlreadyDefined(other.span),
);
break;
}
}
if shadowed {
self.emit_error(
self.span_for(node),
ErrorKind::VariableAlreadyDefined(name.clone()),
);
}
let span = self.span_for(node);
self.scope_mut().declare_local(name, span)
}
@ -1264,7 +1260,7 @@ impl Compiler<'_, '_> {
N: AstNode<Language = rnix::NixLanguage>,
{
Error {
kind: ErrorKind::DynamicKeyInLet(node.syntax().clone()),
kind: ErrorKind::DynamicKeyInLet,
span: self.span_for(node),
}
}