snix/tvix/eval/src/errors.rs
Vincent Ambo 2662376941 feat(tvix/eval): carry optional SyntaxNode in error type
This starts paving the way for nicer, source-code based error
reporting.

Right now the code paths in the VM do not emit annotated errors, as we
do not yet preserve that structure from the compiler. However, error
emitting code paths in the compiler have been amended to include known
nodes.

Change-Id: I1b74410ffd891c40cd913361bd73c4336ec8aa5b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6235
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
2022-09-01 21:40:50 +00:00

58 lines
1.2 KiB
Rust

use std::fmt::Display;
#[derive(Debug)]
pub enum ErrorKind {
DuplicateAttrsKey {
key: String,
},
AttributeNotFound {
name: String,
},
TypeError {
expected: &'static str,
actual: &'static str,
},
Incomparable {
lhs: &'static str,
rhs: &'static str,
},
// Resolving a user-supplied path literal failed in some way.
PathResolution(String),
// Dynamic keys are not allowed in let.
DynamicKeyInLet(rnix::SyntaxNode),
// Unknown variable in statically known scope.
UnknownStaticVariable(rnix::ast::Ident),
// Unknown variable in dynamic scope (with, rec, ...).
UnknownDynamicVariable(String),
ParseErrors(Vec<rnix::parser::ParseError>),
AssertionFailed,
}
#[derive(Debug)]
pub struct Error {
pub node: Option<rnix::SyntaxNode>,
pub kind: ErrorKind,
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Error { node: None, kind }
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{:?}", self.kind)
}
}
pub type EvalResult<T> = Result<T, Error>;