2022-12-31 13:32:35 +01:00
|
|
|
use std::error;
|
2022-09-06 22:56:40 +02:00
|
|
|
use std::io;
|
2022-09-13 20:11:07 +02:00
|
|
|
use std::path::PathBuf;
|
2022-10-04 17:27:49 +02:00
|
|
|
use std::rc::Rc;
|
2022-10-09 21:44:53 +02:00
|
|
|
use std::str::Utf8Error;
|
2023-01-15 12:52:37 +01:00
|
|
|
use std::string::FromUtf8Error;
|
2022-10-06 16:22:17 +02:00
|
|
|
use std::sync::Arc;
|
2022-10-22 16:20:11 +02:00
|
|
|
use std::{fmt::Debug, fmt::Display, num::ParseIntError};
|
2022-08-04 15:43:51 +02:00
|
|
|
|
2022-10-06 16:22:17 +02:00
|
|
|
use codemap::{File, Span};
|
2022-10-03 12:21:28 +02:00
|
|
|
use codemap_diagnostic::{ColorConfig, Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
|
2022-09-28 13:57:53 +02:00
|
|
|
use smol_str::SmolStr;
|
2023-01-15 12:52:37 +01:00
|
|
|
use xml::writer::Error as XmlError;
|
2022-09-12 15:12:43 +02:00
|
|
|
|
2023-01-16 11:05:45 +01:00
|
|
|
use crate::spans::ToSpan;
|
|
|
|
use crate::value::{CoercionKind, NixString};
|
2022-10-04 16:05:34 +02:00
|
|
|
use crate::{SourceCode, Value};
|
2022-09-12 15:12:43 +02:00
|
|
|
|
2023-09-10 07:02:56 +02:00
|
|
|
/// "CatchableErrorKind" errors -- those which can be detected by
|
|
|
|
/// `builtins.tryEval`.
|
|
|
|
///
|
|
|
|
/// Note: this type is deliberately *not* incorporated as a variant
|
|
|
|
/// of ErrorKind, because then Result<Value,ErrorKind> would have
|
|
|
|
/// redundant representations for catchable errors, which would make
|
|
|
|
/// it too easy to handle errors incorrectly:
|
|
|
|
///
|
|
|
|
/// - Ok(Value::Catchable(cek))
|
|
|
|
/// - Err(ErrorKind::ThisVariantDoesNotExist(cek))
|
|
|
|
///
|
|
|
|
/// Because CatchableErrorKind is not a variant of ErrorKind, you
|
|
|
|
/// will often see functions which return a type like:
|
|
|
|
///
|
|
|
|
/// Result<Result<T,CatchableErrorKind>,ErrorKind>
|
|
|
|
///
|
|
|
|
/// ... where T is any type other than Value. This is unfortunate,
|
|
|
|
/// because Rust's magic `?`-syntax does not work on nested Result
|
|
|
|
/// values like this.
|
|
|
|
///
|
|
|
|
/// TODO(amjoseph): investigate result<T,Either<CatchableErrorKind,ErrorKind>>
|
|
|
|
///
|
2023-09-09 09:10:06 +02:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum CatchableErrorKind {
|
2024-02-10 18:22:59 +01:00
|
|
|
Throw(Box<str>),
|
2023-09-09 09:10:06 +02:00
|
|
|
AssertionFailed,
|
2024-02-10 18:22:59 +01:00
|
|
|
UnimplementedFeature(Box<str>),
|
2023-09-09 09:10:06 +02:00
|
|
|
/// Resolving a user-supplied angle brackets path literal failed in some way.
|
2024-02-10 18:22:59 +01:00
|
|
|
NixPathResolution(Box<str>),
|
2023-09-09 09:10:06 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 22:48:47 +02:00
|
|
|
#[derive(Clone, Debug)]
|
2022-08-22 22:20:50 +02:00
|
|
|
pub enum ErrorKind {
|
2022-09-12 15:12:43 +02:00
|
|
|
/// These are user-generated errors through builtins.
|
|
|
|
Abort(String),
|
|
|
|
|
2022-11-10 00:47:07 +01:00
|
|
|
DivisionByZero,
|
|
|
|
|
2022-08-09 17:56:21 +02:00
|
|
|
DuplicateAttrsKey {
|
|
|
|
key: String,
|
|
|
|
},
|
|
|
|
|
2022-09-05 00:35:43 +02:00
|
|
|
/// Attempted to specify an invalid key type (e.g. integer) in a
|
|
|
|
/// dynamic attribute name.
|
2022-09-12 15:12:43 +02:00
|
|
|
InvalidAttributeName(Value),
|
2022-09-05 00:35:43 +02:00
|
|
|
|
2022-08-11 14:29:11 +02:00
|
|
|
AttributeNotFound {
|
|
|
|
name: String,
|
|
|
|
},
|
|
|
|
|
2022-09-06 20:58:28 +02:00
|
|
|
/// Attempted to index into a list beyond its boundaries.
|
2022-09-05 20:41:50 +02:00
|
|
|
IndexOutOfBounds {
|
2022-09-06 20:58:28 +02:00
|
|
|
index: i64,
|
2022-09-05 20:41:50 +02:00
|
|
|
},
|
|
|
|
|
2022-09-05 20:42:53 +02:00
|
|
|
/// Attempted to call `builtins.tail` on an empty list.
|
|
|
|
TailEmptyList,
|
|
|
|
|
2022-08-08 01:16:02 +02:00
|
|
|
TypeError {
|
|
|
|
expected: &'static str,
|
|
|
|
actual: &'static str,
|
|
|
|
},
|
2022-08-11 10:37:04 +02:00
|
|
|
|
|
|
|
Incomparable {
|
|
|
|
lhs: &'static str,
|
|
|
|
rhs: &'static str,
|
|
|
|
},
|
2022-08-12 17:12:28 +02:00
|
|
|
|
2022-10-21 00:52:36 +02:00
|
|
|
/// Resolving a user-supplied relative or home-relative path literal failed in some way.
|
|
|
|
RelativePathResolution(String),
|
2022-08-13 18:42:50 +02:00
|
|
|
|
2022-09-23 16:11:34 +02:00
|
|
|
/// Dynamic keys are not allowed in some scopes.
|
|
|
|
DynamicKeyInScope(&'static str),
|
2022-08-13 19:17:25 +02:00
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Unknown variable in statically known scope.
|
2022-08-22 22:48:47 +02:00
|
|
|
UnknownStaticVariable,
|
2022-08-15 00:13:17 +02:00
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Unknown variable in dynamic scope (with, rec, ...).
|
2022-08-15 00:13:17 +02:00
|
|
|
UnknownDynamicVariable(String),
|
2022-08-15 00:47:30 +02:00
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// User is defining the same variable twice at the same depth.
|
2022-09-12 15:12:43 +02:00
|
|
|
VariableAlreadyDefined(Span),
|
2022-08-27 16:16:46 +02:00
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Attempt to call something that is not callable.
|
2022-09-21 00:10:22 +02:00
|
|
|
NotCallable(&'static str),
|
2022-08-24 01:26:58 +02:00
|
|
|
|
2022-09-05 00:30:58 +02:00
|
|
|
/// Infinite recursion encountered while forcing thunks.
|
2023-03-12 22:30:27 +01:00
|
|
|
InfiniteRecursion {
|
|
|
|
first_force: Span,
|
2023-03-12 22:56:03 +01:00
|
|
|
suspended_at: Option<Span>,
|
|
|
|
content_span: Option<Span>,
|
2023-03-12 22:30:27 +01:00
|
|
|
},
|
2022-08-29 17:33:02 +02:00
|
|
|
|
2022-08-15 00:47:30 +02:00
|
|
|
ParseErrors(Vec<rnix::parser::ParseError>),
|
2022-08-16 14:53:35 +02:00
|
|
|
|
2023-03-12 16:06:11 +01:00
|
|
|
/// An error occured while executing some native code (e.g. a
|
|
|
|
/// builtin), and needs to be chained up.
|
2023-03-12 19:49:29 +01:00
|
|
|
NativeError {
|
|
|
|
gen_type: &'static str,
|
|
|
|
err: Box<Error>,
|
|
|
|
},
|
2023-03-12 16:06:11 +01:00
|
|
|
|
|
|
|
/// An error occured while executing Tvix bytecode, but needs to
|
|
|
|
/// be chained up.
|
|
|
|
BytecodeError(Box<Error>),
|
2022-09-11 22:12:02 +02:00
|
|
|
|
2022-09-13 15:37:19 +02:00
|
|
|
/// Given type can't be coerced to a string in the respective context
|
|
|
|
NotCoercibleToString {
|
|
|
|
from: &'static str,
|
|
|
|
kind: CoercionKind,
|
|
|
|
},
|
|
|
|
|
2022-09-13 20:11:07 +02:00
|
|
|
/// The given string doesn't represent an absolute path
|
|
|
|
NotAnAbsolutePath(PathBuf),
|
|
|
|
|
2022-09-18 18:06:11 +02:00
|
|
|
/// An error occurred when parsing an integer
|
|
|
|
ParseIntError(ParseIntError),
|
|
|
|
|
2022-09-28 13:57:53 +02:00
|
|
|
// Errors specific to nested attribute sets and merges thereof.
|
|
|
|
/// Nested attributes can not be merged with an inherited value.
|
|
|
|
UnmergeableInherit {
|
|
|
|
name: SmolStr,
|
|
|
|
},
|
|
|
|
|
2022-09-28 14:08:11 +02:00
|
|
|
/// Nested attributes can not be merged with values that are not
|
|
|
|
/// literal attribute sets.
|
|
|
|
UnmergeableValue,
|
|
|
|
|
2022-10-04 17:27:49 +02:00
|
|
|
/// Parse errors occured while importing a file.
|
|
|
|
ImportParseError {
|
|
|
|
path: PathBuf,
|
2022-10-06 16:22:17 +02:00
|
|
|
file: Arc<File>,
|
2022-10-04 17:27:49 +02:00
|
|
|
errors: Vec<rnix::parser::ParseError>,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Compilation errors occured while importing a file.
|
|
|
|
ImportCompilerError {
|
|
|
|
path: PathBuf,
|
|
|
|
errors: Vec<Error>,
|
|
|
|
},
|
|
|
|
|
2022-09-06 22:56:40 +02:00
|
|
|
/// I/O errors
|
|
|
|
IO {
|
|
|
|
path: Option<PathBuf>,
|
|
|
|
error: Rc<io::Error>,
|
|
|
|
},
|
|
|
|
|
2024-01-11 18:50:16 +01:00
|
|
|
/// Errors parsing JSON, or serializing as JSON.
|
|
|
|
JsonError(String),
|
2022-10-10 06:32:57 +02:00
|
|
|
|
2023-03-04 00:12:06 +01:00
|
|
|
/// Nix value that can not be serialised to JSON.
|
|
|
|
NotSerialisableToJson(&'static str),
|
|
|
|
|
2023-01-24 15:54:29 +01:00
|
|
|
/// Errors converting TOML to a value
|
|
|
|
FromTomlError(String),
|
|
|
|
|
2022-10-13 05:53:03 +02:00
|
|
|
/// An unexpected argument was supplied to a function that takes formal parameters
|
|
|
|
UnexpectedArgument {
|
|
|
|
arg: NixString,
|
|
|
|
formals_span: Span,
|
|
|
|
},
|
|
|
|
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 23:25:52 +01:00
|
|
|
/// Invalid UTF-8 was encoutered somewhere
|
|
|
|
Utf8,
|
|
|
|
|
2023-01-15 12:52:37 +01:00
|
|
|
/// Errors while serialising to XML.
|
|
|
|
Xml(Rc<XmlError>),
|
|
|
|
|
2023-01-16 11:05:45 +01:00
|
|
|
/// Variant for errors that bubble up to eval from other Tvix
|
|
|
|
/// components.
|
|
|
|
TvixError(Rc<dyn error::Error>),
|
|
|
|
|
2022-10-22 16:20:11 +02:00
|
|
|
/// Variant for code paths that are known bugs in Tvix (usually
|
|
|
|
/// issues with the compiler/VM interaction).
|
|
|
|
TvixBug {
|
|
|
|
msg: &'static str,
|
|
|
|
metadata: Option<Rc<dyn Debug>>,
|
|
|
|
},
|
|
|
|
|
2022-09-11 22:12:02 +02:00
|
|
|
/// Tvix internal warning for features triggered by users that are
|
|
|
|
/// not actually implemented yet, and without which eval can not
|
|
|
|
/// proceed.
|
|
|
|
NotImplemented(&'static str),
|
2023-01-16 18:02:33 +01:00
|
|
|
|
|
|
|
/// Internal variant which should disappear during error construction.
|
|
|
|
WithContext {
|
|
|
|
context: String,
|
|
|
|
underlying: Box<ErrorKind>,
|
|
|
|
},
|
2023-12-26 01:30:05 +01:00
|
|
|
|
|
|
|
/// Unexpected context string
|
|
|
|
UnexpectedContext,
|
2022-08-08 01:16:02 +02:00
|
|
|
}
|
2022-08-04 15:43:51 +02:00
|
|
|
|
2022-12-31 13:32:35 +01:00
|
|
|
impl error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
|
|
match &self.kind {
|
2023-03-12 19:49:29 +01:00
|
|
|
ErrorKind::NativeError { err, .. } | ErrorKind::BytecodeError(err) => err.source(),
|
2022-12-31 13:32:35 +01:00
|
|
|
ErrorKind::ParseErrors(err) => err.first().map(|e| e as &dyn error::Error),
|
|
|
|
ErrorKind::ParseIntError(err) => Some(err),
|
|
|
|
ErrorKind::ImportParseError { errors, .. } => {
|
|
|
|
errors.first().map(|e| e as &dyn error::Error)
|
|
|
|
}
|
|
|
|
ErrorKind::ImportCompilerError { errors, .. } => {
|
|
|
|
errors.first().map(|e| e as &dyn error::Error)
|
|
|
|
}
|
|
|
|
ErrorKind::IO { error, .. } => Some(error.as_ref()),
|
2023-01-15 12:52:37 +01:00
|
|
|
ErrorKind::Xml(error) => Some(error.as_ref()),
|
2023-01-16 11:05:45 +01:00
|
|
|
ErrorKind::TvixError(error) => Some(error.as_ref()),
|
2022-12-31 13:32:35 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 18:06:11 +02:00
|
|
|
impl From<ParseIntError> for ErrorKind {
|
|
|
|
fn from(e: ParseIntError) -> Self {
|
|
|
|
Self::ParseIntError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 21:44:53 +02:00
|
|
|
impl From<Utf8Error> for ErrorKind {
|
|
|
|
fn from(_: Utf8Error) -> Self {
|
|
|
|
Self::NotImplemented("FromUtf8Error not handled: https://b.tvl.fyi/issues/189")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 12:52:37 +01:00
|
|
|
impl From<FromUtf8Error> for ErrorKind {
|
|
|
|
fn from(_: FromUtf8Error) -> Self {
|
|
|
|
Self::NotImplemented("FromUtf8Error not handled: https://b.tvl.fyi/issues/189")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 23:25:52 +01:00
|
|
|
impl From<bstr::Utf8Error> for ErrorKind {
|
|
|
|
fn from(_: bstr::Utf8Error) -> Self {
|
|
|
|
Self::Utf8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bstr::FromUtf8Error> for ErrorKind {
|
|
|
|
fn from(_value: bstr::FromUtf8Error) -> Self {
|
|
|
|
Self::Utf8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 12:52:37 +01:00
|
|
|
impl From<XmlError> for ErrorKind {
|
|
|
|
fn from(err: XmlError) -> Self {
|
|
|
|
Self::Xml(Rc::new(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 22:56:40 +02:00
|
|
|
impl From<io::Error> for ErrorKind {
|
|
|
|
fn from(e: io::Error) -> Self {
|
|
|
|
ErrorKind::IO {
|
|
|
|
path: None,
|
|
|
|
error: Rc::new(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 06:32:57 +02:00
|
|
|
impl From<serde_json::Error> for ErrorKind {
|
|
|
|
fn from(err: serde_json::Error) -> Self {
|
|
|
|
// Can't just put the `serde_json::Error` in the ErrorKind since it doesn't impl `Clone`
|
2024-01-11 18:50:16 +01:00
|
|
|
Self::JsonError(err.to_string())
|
2022-10-10 06:32:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 15:54:29 +01:00
|
|
|
impl From<toml::de::Error> for ErrorKind {
|
|
|
|
fn from(err: toml::de::Error) -> Self {
|
|
|
|
Self::FromTomlError(format!("error in TOML serialization: {err}"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 22:48:47 +02:00
|
|
|
#[derive(Clone, Debug)]
|
2022-08-22 22:20:50 +02:00
|
|
|
pub struct Error {
|
2023-02-13 08:58:39 +01:00
|
|
|
pub kind: ErrorKind,
|
|
|
|
pub span: Span,
|
|
|
|
pub contexts: Vec<String>,
|
2024-02-20 09:29:30 +01:00
|
|
|
pub source: SourceCode,
|
2023-01-16 18:02:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
2024-02-20 09:29:30 +01:00
|
|
|
pub fn new(mut kind: ErrorKind, span: Span, source: SourceCode) -> Self {
|
2023-01-16 18:02:33 +01:00
|
|
|
let mut contexts = vec![];
|
|
|
|
while let ErrorKind::WithContext {
|
|
|
|
context,
|
|
|
|
underlying,
|
|
|
|
} = kind
|
|
|
|
{
|
|
|
|
kind = *underlying;
|
|
|
|
contexts.push(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error {
|
|
|
|
kind,
|
|
|
|
span,
|
|
|
|
contexts,
|
2024-02-20 09:29:30 +01:00
|
|
|
source,
|
2023-01-16 18:02:33 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-22 22:20:50 +02:00
|
|
|
}
|
|
|
|
|
2023-01-10 00:06:57 +01:00
|
|
|
impl Display for ErrorKind {
|
2022-08-04 15:43:51 +02:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2023-01-10 00:06:57 +01:00
|
|
|
match &self {
|
2022-10-08 20:28:16 +02:00
|
|
|
ErrorKind::Abort(msg) => write!(f, "evaluation aborted: {}", msg),
|
|
|
|
|
2022-11-10 00:47:07 +01:00
|
|
|
ErrorKind::DivisionByZero => write!(f, "division by zero"),
|
|
|
|
|
2022-10-08 20:28:16 +02:00
|
|
|
ErrorKind::DuplicateAttrsKey { key } => {
|
|
|
|
write!(f, "attribute key '{}' already defined", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::InvalidAttributeName(val) => write!(
|
|
|
|
f,
|
|
|
|
"found attribute name '{}' of type '{}', but attribute names must be strings",
|
|
|
|
val,
|
|
|
|
val.type_of()
|
|
|
|
),
|
|
|
|
|
|
|
|
ErrorKind::AttributeNotFound { name } => write!(
|
|
|
|
f,
|
|
|
|
"attribute with name '{}' could not be found in the set",
|
|
|
|
name
|
|
|
|
),
|
|
|
|
|
|
|
|
ErrorKind::IndexOutOfBounds { index } => {
|
|
|
|
write!(f, "list index '{}' is out of bounds", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::TailEmptyList => write!(f, "'tail' called on an empty list"),
|
|
|
|
|
|
|
|
ErrorKind::TypeError { expected, actual } => write!(
|
|
|
|
f,
|
|
|
|
"expected value of type '{}', but found a '{}'",
|
|
|
|
expected, actual
|
|
|
|
),
|
|
|
|
|
|
|
|
ErrorKind::Incomparable { lhs, rhs } => {
|
|
|
|
write!(f, "can not compare a {} with a {}", lhs, rhs)
|
|
|
|
}
|
|
|
|
|
2023-09-10 07:02:56 +02:00
|
|
|
ErrorKind::RelativePathResolution(err) => {
|
2022-10-21 00:52:36 +02:00
|
|
|
write!(f, "could not resolve path: {}", err)
|
|
|
|
}
|
2022-10-08 20:28:16 +02:00
|
|
|
|
|
|
|
ErrorKind::DynamicKeyInScope(scope) => {
|
|
|
|
write!(f, "dynamically evaluated keys are not allowed in {}", scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::UnknownStaticVariable => write!(f, "variable not found"),
|
|
|
|
|
|
|
|
ErrorKind::UnknownDynamicVariable(name) => write!(
|
|
|
|
f,
|
|
|
|
r#"variable '{}' could not be found
|
|
|
|
|
|
|
|
Note that this occured within a `with`-expression. The problem may be related
|
|
|
|
to a missing value in the attribute set(s) included via `with`."#,
|
|
|
|
name
|
|
|
|
),
|
|
|
|
|
|
|
|
ErrorKind::VariableAlreadyDefined(_) => write!(f, "variable has already been defined"),
|
|
|
|
|
|
|
|
ErrorKind::NotCallable(other_type) => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"only functions and builtins can be called, but this is a '{}'",
|
|
|
|
other_type
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-12 22:30:27 +01:00
|
|
|
ErrorKind::InfiniteRecursion { .. } => write!(f, "infinite recursion encountered"),
|
2022-10-08 20:28:16 +02:00
|
|
|
|
|
|
|
// Errors themselves ignored here & handled in Self::spans instead
|
|
|
|
ErrorKind::ParseErrors(_) => write!(f, "failed to parse Nix code:"),
|
|
|
|
|
2023-03-12 19:49:29 +01:00
|
|
|
ErrorKind::NativeError { gen_type, .. } => {
|
|
|
|
write!(f, "while evaluating this as native code ({})", gen_type)
|
|
|
|
}
|
|
|
|
|
2023-03-12 16:06:11 +01:00
|
|
|
ErrorKind::BytecodeError(_) => write!(f, "while evaluating this Nix code"),
|
2022-10-08 20:28:16 +02:00
|
|
|
|
|
|
|
ErrorKind::NotCoercibleToString { kind, from } => {
|
2023-12-13 14:52:07 +01:00
|
|
|
let kindly = if kind.strong { "strongly" } else { "weakly" };
|
2022-10-08 20:28:16 +02:00
|
|
|
|
|
|
|
let hint = if *from == "set" {
|
|
|
|
", missing a `__toString` or `outPath` attribute"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
write!(f, "cannot ({kindly}) coerce {from} to a string{hint}")
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::NotAnAbsolutePath(given) => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"string '{}' does not represent an absolute path",
|
|
|
|
given.to_string_lossy()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::ParseIntError(err) => {
|
|
|
|
write!(f, "invalid integer: {}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::UnmergeableInherit { name } => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"cannot merge a nested attribute set into the inherited entry '{}'",
|
|
|
|
name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::UnmergeableValue => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"nested attribute sets or keys can only be merged with literal attribute sets"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errors themselves ignored here & handled in Self::spans instead
|
|
|
|
ErrorKind::ImportParseError { path, .. } => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"parse errors occured while importing '{}'",
|
|
|
|
path.to_string_lossy()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-11 00:04:19 +02:00
|
|
|
ErrorKind::ImportCompilerError { path, .. } => {
|
|
|
|
writeln!(
|
2022-10-08 20:28:16 +02:00
|
|
|
f,
|
2022-10-11 00:04:19 +02:00
|
|
|
"compiler errors occured while importing '{}'",
|
2022-10-08 20:28:16 +02:00
|
|
|
path.to_string_lossy()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-06 22:56:40 +02:00
|
|
|
ErrorKind::IO { path, error } => {
|
|
|
|
write!(f, "I/O error: ")?;
|
|
|
|
if let Some(path) = path {
|
|
|
|
write!(f, "{}: ", path.display())?;
|
|
|
|
}
|
|
|
|
write!(f, "{error}")
|
|
|
|
}
|
|
|
|
|
2024-01-11 18:50:16 +01:00
|
|
|
ErrorKind::JsonError(msg) => {
|
|
|
|
write!(f, "Error converting JSON to a Nix value or back: {msg}")
|
2022-10-10 06:32:57 +02:00
|
|
|
}
|
|
|
|
|
2023-03-04 00:12:06 +01:00
|
|
|
ErrorKind::NotSerialisableToJson(_type) => {
|
|
|
|
write!(f, "a {} cannot be converted to JSON", _type)
|
|
|
|
}
|
|
|
|
|
2023-01-24 15:54:29 +01:00
|
|
|
ErrorKind::FromTomlError(msg) => {
|
|
|
|
write!(f, "Error converting TOML to a Nix value: {msg}")
|
|
|
|
}
|
|
|
|
|
2022-10-13 05:53:03 +02:00
|
|
|
ErrorKind::UnexpectedArgument { arg, .. } => {
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 23:25:52 +01:00
|
|
|
write!(f, "Unexpected argument `{arg}` supplied to function",)
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::Utf8 => {
|
|
|
|
write!(f, "Invalid UTF-8 in string")
|
2022-10-13 05:53:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-15 12:52:37 +01:00
|
|
|
ErrorKind::Xml(error) => write!(f, "failed to serialise to XML: {error}"),
|
|
|
|
|
2023-01-16 11:05:45 +01:00
|
|
|
ErrorKind::TvixError(inner_error) => {
|
|
|
|
write!(f, "{inner_error}")
|
|
|
|
}
|
|
|
|
|
2022-10-22 16:20:11 +02:00
|
|
|
ErrorKind::TvixBug { msg, metadata } => {
|
|
|
|
write!(f, "Tvix bug: {}", msg)?;
|
|
|
|
|
|
|
|
if let Some(metadata) = metadata {
|
|
|
|
write!(f, "; metadata: {:?}", metadata)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-08 20:28:16 +02:00
|
|
|
ErrorKind::NotImplemented(feature) => {
|
|
|
|
write!(f, "feature not yet implemented in Tvix: {}", feature)
|
|
|
|
}
|
2023-01-16 18:02:33 +01:00
|
|
|
|
|
|
|
ErrorKind::WithContext { .. } => {
|
|
|
|
panic!("internal ErrorKind::WithContext variant leaked")
|
|
|
|
}
|
2023-12-26 01:30:05 +01:00
|
|
|
|
|
|
|
ErrorKind::UnexpectedContext => {
|
|
|
|
write!(f, "unexpected context string")
|
|
|
|
}
|
2022-10-08 20:28:16 +02:00
|
|
|
}
|
2022-08-04 15:43:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 00:06:57 +01:00
|
|
|
impl Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-02-20 12:06:36 +01:00
|
|
|
write!(f, "{}", self.kind)
|
2023-01-10 00:06:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 15:43:51 +02:00
|
|
|
pub type EvalResult<T> = Result<T, Error>;
|
2022-09-12 15:12:43 +02:00
|
|
|
|
2022-10-06 16:22:17 +02:00
|
|
|
/// Human-readable names for rnix syntaxes.
|
|
|
|
fn name_for_syntax(syntax: &rnix::SyntaxKind) -> &'static str {
|
|
|
|
match syntax {
|
|
|
|
rnix::SyntaxKind::TOKEN_COMMENT => "a comment",
|
|
|
|
rnix::SyntaxKind::TOKEN_WHITESPACE => "whitespace",
|
|
|
|
rnix::SyntaxKind::TOKEN_ASSERT => "`assert`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_ELSE => "`else`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_IN => "`in`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_IF => "`if`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_INHERIT => "`inherit`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_LET => "`let`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_OR => "`or`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_REC => "`rec`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_THEN => "`then`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_WITH => "`with`-keyword",
|
|
|
|
rnix::SyntaxKind::TOKEN_L_BRACE => "{",
|
|
|
|
rnix::SyntaxKind::TOKEN_R_BRACE => "}",
|
|
|
|
rnix::SyntaxKind::TOKEN_L_BRACK => "[",
|
|
|
|
rnix::SyntaxKind::TOKEN_R_BRACK => "]",
|
|
|
|
rnix::SyntaxKind::TOKEN_ASSIGN => "=",
|
|
|
|
rnix::SyntaxKind::TOKEN_AT => "@",
|
|
|
|
rnix::SyntaxKind::TOKEN_COLON => ":",
|
|
|
|
rnix::SyntaxKind::TOKEN_COMMA => "`,`",
|
|
|
|
rnix::SyntaxKind::TOKEN_DOT => ".",
|
|
|
|
rnix::SyntaxKind::TOKEN_ELLIPSIS => "...",
|
|
|
|
rnix::SyntaxKind::TOKEN_QUESTION => "?",
|
|
|
|
rnix::SyntaxKind::TOKEN_SEMICOLON => ";",
|
|
|
|
rnix::SyntaxKind::TOKEN_L_PAREN => "(",
|
|
|
|
rnix::SyntaxKind::TOKEN_R_PAREN => ")",
|
|
|
|
rnix::SyntaxKind::TOKEN_CONCAT => "++",
|
|
|
|
rnix::SyntaxKind::TOKEN_INVERT => "!",
|
|
|
|
rnix::SyntaxKind::TOKEN_UPDATE => "//",
|
|
|
|
rnix::SyntaxKind::TOKEN_ADD => "+",
|
|
|
|
rnix::SyntaxKind::TOKEN_SUB => "-",
|
|
|
|
rnix::SyntaxKind::TOKEN_MUL => "*",
|
|
|
|
rnix::SyntaxKind::TOKEN_DIV => "/",
|
|
|
|
rnix::SyntaxKind::TOKEN_AND_AND => "&&",
|
|
|
|
rnix::SyntaxKind::TOKEN_EQUAL => "==",
|
|
|
|
rnix::SyntaxKind::TOKEN_IMPLICATION => "->",
|
|
|
|
rnix::SyntaxKind::TOKEN_LESS => "<",
|
|
|
|
rnix::SyntaxKind::TOKEN_LESS_OR_EQ => "<=",
|
|
|
|
rnix::SyntaxKind::TOKEN_MORE => ">",
|
|
|
|
rnix::SyntaxKind::TOKEN_MORE_OR_EQ => ">=",
|
|
|
|
rnix::SyntaxKind::TOKEN_NOT_EQUAL => "!=",
|
|
|
|
rnix::SyntaxKind::TOKEN_OR_OR => "||",
|
|
|
|
rnix::SyntaxKind::TOKEN_FLOAT => "a float",
|
|
|
|
rnix::SyntaxKind::TOKEN_IDENT => "an identifier",
|
|
|
|
rnix::SyntaxKind::TOKEN_INTEGER => "an integer",
|
|
|
|
rnix::SyntaxKind::TOKEN_INTERPOL_END => "}",
|
|
|
|
rnix::SyntaxKind::TOKEN_INTERPOL_START => "${",
|
|
|
|
rnix::SyntaxKind::TOKEN_PATH => "a path",
|
|
|
|
rnix::SyntaxKind::TOKEN_URI => "a literal URI",
|
|
|
|
rnix::SyntaxKind::TOKEN_STRING_CONTENT => "content of a string",
|
|
|
|
rnix::SyntaxKind::TOKEN_STRING_END => "\"",
|
|
|
|
rnix::SyntaxKind::TOKEN_STRING_START => "\"",
|
|
|
|
|
|
|
|
rnix::SyntaxKind::NODE_APPLY => "a function application",
|
|
|
|
rnix::SyntaxKind::NODE_ASSERT => "an assertion",
|
|
|
|
rnix::SyntaxKind::NODE_ATTRPATH => "an attribute path",
|
|
|
|
rnix::SyntaxKind::NODE_DYNAMIC => "a dynamic identifier",
|
|
|
|
|
|
|
|
rnix::SyntaxKind::NODE_IDENT => "an identifier",
|
|
|
|
rnix::SyntaxKind::NODE_IF_ELSE => "an `if`-expression",
|
|
|
|
rnix::SyntaxKind::NODE_SELECT => "a `select`-expression",
|
|
|
|
rnix::SyntaxKind::NODE_INHERIT => "inherited values",
|
|
|
|
rnix::SyntaxKind::NODE_INHERIT_FROM => "inherited values",
|
|
|
|
rnix::SyntaxKind::NODE_STRING => "a string",
|
|
|
|
rnix::SyntaxKind::NODE_INTERPOL => "an interpolation",
|
|
|
|
rnix::SyntaxKind::NODE_LAMBDA => "a function",
|
|
|
|
rnix::SyntaxKind::NODE_IDENT_PARAM => "a function parameter",
|
|
|
|
rnix::SyntaxKind::NODE_LEGACY_LET => "a legacy `let`-expression",
|
|
|
|
rnix::SyntaxKind::NODE_LET_IN => "a `let`-expression",
|
|
|
|
rnix::SyntaxKind::NODE_LIST => "a list",
|
|
|
|
rnix::SyntaxKind::NODE_BIN_OP => "a binary operator",
|
|
|
|
rnix::SyntaxKind::NODE_PAREN => "a parenthesised expression",
|
|
|
|
rnix::SyntaxKind::NODE_PATTERN => "a function argument pattern",
|
|
|
|
rnix::SyntaxKind::NODE_PAT_BIND => "an argument pattern binding",
|
|
|
|
rnix::SyntaxKind::NODE_PAT_ENTRY => "an argument pattern entry",
|
|
|
|
rnix::SyntaxKind::NODE_ROOT => "a Nix expression",
|
|
|
|
rnix::SyntaxKind::NODE_ATTR_SET => "an attribute set",
|
|
|
|
rnix::SyntaxKind::NODE_ATTRPATH_VALUE => "an attribute set entry",
|
|
|
|
rnix::SyntaxKind::NODE_UNARY_OP => "a unary operator",
|
|
|
|
rnix::SyntaxKind::NODE_LITERAL => "a literal value",
|
|
|
|
rnix::SyntaxKind::NODE_WITH => "a `with`-expression",
|
|
|
|
rnix::SyntaxKind::NODE_PATH => "a path",
|
|
|
|
rnix::SyntaxKind::NODE_HAS_ATTR => "`?`-operator",
|
|
|
|
|
|
|
|
// TODO(tazjin): unsure what these variants are, lets crash!
|
|
|
|
rnix::SyntaxKind::NODE_ERROR => todo!("NODE_ERROR found, tell tazjin!"),
|
|
|
|
rnix::SyntaxKind::TOKEN_ERROR => todo!("TOKEN_ERROR found, tell tazjin!"),
|
|
|
|
_ => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct the string representation for a list of expected parser tokens.
|
|
|
|
fn expected_syntax(one_of: &[rnix::SyntaxKind]) -> String {
|
|
|
|
match one_of.len() {
|
|
|
|
0 => "nothing".into(),
|
|
|
|
1 => format!("'{}'", name_for_syntax(&one_of[0])),
|
|
|
|
_ => {
|
|
|
|
let mut out: String = "one of: ".into();
|
|
|
|
let end = one_of.len() - 1;
|
|
|
|
|
|
|
|
for (idx, item) in one_of.iter().enumerate() {
|
|
|
|
if idx != 0 {
|
|
|
|
out.push_str(", ");
|
|
|
|
} else if idx == end {
|
|
|
|
out.push_str(", or ");
|
|
|
|
};
|
|
|
|
|
|
|
|
out.push_str(name_for_syntax(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process a list of parse errors into a set of span labels, annotating parse
|
|
|
|
/// errors.
|
|
|
|
fn spans_for_parse_errors(file: &File, errors: &[rnix::parser::ParseError]) -> Vec<SpanLabel> {
|
|
|
|
// rnix has a tendency to emit some identical errors more than once, but
|
|
|
|
// they do not enhance the user experience necessarily, so we filter them
|
|
|
|
// out
|
|
|
|
let mut had_eof = false;
|
|
|
|
|
|
|
|
errors
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(idx, err)| {
|
|
|
|
let (span, label): (Span, String) = match err {
|
|
|
|
rnix::parser::ParseError::Unexpected(range) => (
|
|
|
|
range.span_for(file),
|
|
|
|
"found an unexpected syntax element here".into(),
|
|
|
|
),
|
|
|
|
|
|
|
|
rnix::parser::ParseError::UnexpectedExtra(range) => (
|
|
|
|
range.span_for(file),
|
|
|
|
"found unexpected extra elements at the root of the expression".into(),
|
|
|
|
),
|
|
|
|
|
|
|
|
rnix::parser::ParseError::UnexpectedWanted(found, range, wanted) => {
|
|
|
|
let span = range.span_for(file);
|
|
|
|
(
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"found '{}', but expected {}",
|
2022-12-20 15:22:56 +01:00
|
|
|
name_for_syntax(found),
|
|
|
|
expected_syntax(wanted),
|
2022-10-06 16:22:17 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
rnix::parser::ParseError::UnexpectedEOF => {
|
|
|
|
if had_eof {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
had_eof = true;
|
|
|
|
|
|
|
|
(
|
|
|
|
file.span,
|
|
|
|
"code ended unexpectedly while the parser still expected more".into(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
rnix::parser::ParseError::UnexpectedEOFWanted(wanted) => {
|
|
|
|
had_eof = true;
|
|
|
|
|
|
|
|
(
|
|
|
|
file.span,
|
|
|
|
format!(
|
|
|
|
"code ended unexpectedly, but wanted {}",
|
2022-12-20 15:22:56 +01:00
|
|
|
expected_syntax(wanted)
|
2022-10-06 16:22:17 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
rnix::parser::ParseError::DuplicatedArgs(range, name) => (
|
|
|
|
range.span_for(file),
|
|
|
|
format!(
|
|
|
|
"the function argument pattern '{}' was bound more than once",
|
|
|
|
name
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
rnix::parser::ParseError::RecursionLimitExceeded => (
|
|
|
|
file.span,
|
2022-12-20 15:22:56 +01:00
|
|
|
"this code exceeds the parser's recursion limit, please report a Tvix bug"
|
|
|
|
.to_string(),
|
2022-10-06 16:22:17 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
// TODO: can rnix even still throw this? it's semantic!
|
|
|
|
rnix::parser::ParseError::UnexpectedDoubleBind(range) => (
|
|
|
|
range.span_for(file),
|
|
|
|
"this pattern was bound more than once".into(),
|
|
|
|
),
|
|
|
|
|
|
|
|
// The error enum is marked as `#[non_exhaustive]` in rnix,
|
|
|
|
// which disables the compiler error for missing a variant. This
|
|
|
|
// feature makes it possible for users to miss critical updates
|
|
|
|
// of enum variants for a more exciting runtime experience.
|
|
|
|
new => todo!("new parse error variant: {}", new),
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(SpanLabel {
|
|
|
|
span,
|
|
|
|
label: Some(label),
|
|
|
|
style: if idx == 0 {
|
|
|
|
SpanStyle::Primary
|
|
|
|
} else {
|
|
|
|
SpanStyle::Secondary
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2022-09-12 15:12:43 +02:00
|
|
|
impl Error {
|
2024-02-20 09:38:33 +01:00
|
|
|
pub fn fancy_format_str(&self) -> String {
|
2022-09-12 15:12:43 +02:00
|
|
|
let mut out = vec![];
|
2024-02-20 09:38:33 +01:00
|
|
|
Emitter::vec(&mut out, Some(&*self.source.codemap())).emit(&self.diagnostics());
|
2022-09-12 15:12:43 +02:00
|
|
|
String::from_utf8_lossy(&out).to_string()
|
|
|
|
}
|
|
|
|
|
2022-10-03 12:21:28 +02:00
|
|
|
/// Render a fancy, human-readable output of this error and print
|
|
|
|
/// it to stderr.
|
2024-02-20 09:38:33 +01:00
|
|
|
pub fn fancy_format_stderr(&self) {
|
|
|
|
Emitter::stderr(ColorConfig::Auto, Some(&*self.source.codemap())).emit(&self.diagnostics());
|
2022-10-03 12:21:28 +02:00
|
|
|
}
|
|
|
|
|
2022-09-12 15:12:43 +02:00
|
|
|
/// Create the optional span label displayed as an annotation on
|
|
|
|
/// the underlined span of the error.
|
|
|
|
fn span_label(&self) -> Option<String> {
|
2022-10-06 16:35:15 +02:00
|
|
|
let label = match &self.kind {
|
|
|
|
ErrorKind::DuplicateAttrsKey { .. } => "in this attribute set",
|
|
|
|
ErrorKind::InvalidAttributeName(_) => "in this attribute set",
|
2023-09-10 07:02:56 +02:00
|
|
|
ErrorKind::RelativePathResolution(_) => "in this path literal",
|
2022-10-13 05:53:03 +02:00
|
|
|
ErrorKind::UnexpectedArgument { .. } => "in this function call",
|
2023-12-26 01:30:05 +01:00
|
|
|
ErrorKind::UnexpectedContext => "in this string",
|
2022-10-06 16:35:15 +02:00
|
|
|
|
|
|
|
// The spans for some errors don't have any more descriptive stuff
|
|
|
|
// in them, or we don't utilise it yet.
|
2023-09-10 07:02:56 +02:00
|
|
|
ErrorKind::Abort(_)
|
2022-10-06 16:35:15 +02:00
|
|
|
| ErrorKind::AttributeNotFound { .. }
|
|
|
|
| ErrorKind::IndexOutOfBounds { .. }
|
|
|
|
| ErrorKind::TailEmptyList
|
|
|
|
| ErrorKind::TypeError { .. }
|
|
|
|
| ErrorKind::Incomparable { .. }
|
2022-11-10 00:47:07 +01:00
|
|
|
| ErrorKind::DivisionByZero
|
2022-10-06 16:35:15 +02:00
|
|
|
| ErrorKind::DynamicKeyInScope(_)
|
|
|
|
| ErrorKind::UnknownStaticVariable
|
|
|
|
| ErrorKind::UnknownDynamicVariable(_)
|
|
|
|
| ErrorKind::VariableAlreadyDefined(_)
|
|
|
|
| ErrorKind::NotCallable(_)
|
2023-03-12 22:30:27 +01:00
|
|
|
| ErrorKind::InfiniteRecursion { .. }
|
2022-10-06 16:35:15 +02:00
|
|
|
| ErrorKind::ParseErrors(_)
|
2023-03-12 19:49:29 +01:00
|
|
|
| ErrorKind::NativeError { .. }
|
2023-03-12 16:06:11 +01:00
|
|
|
| ErrorKind::BytecodeError(_)
|
2022-10-06 16:35:15 +02:00
|
|
|
| ErrorKind::NotCoercibleToString { .. }
|
|
|
|
| ErrorKind::NotAnAbsolutePath(_)
|
|
|
|
| ErrorKind::ParseIntError(_)
|
|
|
|
| ErrorKind::UnmergeableInherit { .. }
|
|
|
|
| ErrorKind::UnmergeableValue
|
|
|
|
| ErrorKind::ImportParseError { .. }
|
|
|
|
| ErrorKind::ImportCompilerError { .. }
|
2022-09-06 22:56:40 +02:00
|
|
|
| ErrorKind::IO { .. }
|
2024-01-11 18:50:16 +01:00
|
|
|
| ErrorKind::JsonError(_)
|
2023-03-04 00:12:06 +01:00
|
|
|
| ErrorKind::NotSerialisableToJson(_)
|
2023-01-24 15:54:29 +01:00
|
|
|
| ErrorKind::FromTomlError(_)
|
2023-01-15 12:52:37 +01:00
|
|
|
| ErrorKind::Xml(_)
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 23:25:52 +01:00
|
|
|
| ErrorKind::Utf8
|
2023-01-16 11:05:45 +01:00
|
|
|
| ErrorKind::TvixError(_)
|
2022-10-22 16:20:11 +02:00
|
|
|
| ErrorKind::TvixBug { .. }
|
2023-01-16 18:02:33 +01:00
|
|
|
| ErrorKind::NotImplemented(_)
|
|
|
|
| ErrorKind::WithContext { .. } => return None,
|
2022-10-06 16:35:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Some(label.into())
|
2022-09-12 15:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the unique error code for this variant which can be
|
|
|
|
/// used to refer users to documentation.
|
|
|
|
fn code(&self) -> &'static str {
|
|
|
|
match self.kind {
|
|
|
|
ErrorKind::Abort(_) => "E002",
|
|
|
|
ErrorKind::InvalidAttributeName { .. } => "E004",
|
|
|
|
ErrorKind::AttributeNotFound { .. } => "E005",
|
|
|
|
ErrorKind::TypeError { .. } => "E006",
|
|
|
|
ErrorKind::Incomparable { .. } => "E007",
|
2022-09-23 16:11:34 +02:00
|
|
|
ErrorKind::DynamicKeyInScope(_) => "E009",
|
2022-09-12 15:12:43 +02:00
|
|
|
ErrorKind::UnknownStaticVariable => "E010",
|
|
|
|
ErrorKind::UnknownDynamicVariable(_) => "E011",
|
|
|
|
ErrorKind::VariableAlreadyDefined(_) => "E012",
|
2022-09-21 00:10:22 +02:00
|
|
|
ErrorKind::NotCallable(_) => "E013",
|
2023-03-12 22:30:27 +01:00
|
|
|
ErrorKind::InfiniteRecursion { .. } => "E014",
|
2022-09-12 15:12:43 +02:00
|
|
|
ErrorKind::ParseErrors(_) => "E015",
|
|
|
|
ErrorKind::DuplicateAttrsKey { .. } => "E016",
|
2022-09-13 15:37:19 +02:00
|
|
|
ErrorKind::NotCoercibleToString { .. } => "E018",
|
2022-09-05 20:41:50 +02:00
|
|
|
ErrorKind::IndexOutOfBounds { .. } => "E019",
|
2022-09-13 20:11:07 +02:00
|
|
|
ErrorKind::NotAnAbsolutePath(_) => "E020",
|
2022-09-18 18:06:11 +02:00
|
|
|
ErrorKind::ParseIntError(_) => "E021",
|
2022-09-05 20:42:53 +02:00
|
|
|
ErrorKind::TailEmptyList { .. } => "E023",
|
2022-09-28 13:57:53 +02:00
|
|
|
ErrorKind::UnmergeableInherit { .. } => "E024",
|
2022-09-28 14:08:11 +02:00
|
|
|
ErrorKind::UnmergeableValue => "E025",
|
2022-10-04 17:27:49 +02:00
|
|
|
ErrorKind::ImportParseError { .. } => "E027",
|
|
|
|
ErrorKind::ImportCompilerError { .. } => "E028",
|
2022-09-06 22:56:40 +02:00
|
|
|
ErrorKind::IO { .. } => "E029",
|
2024-01-11 18:50:16 +01:00
|
|
|
ErrorKind::JsonError { .. } => "E030",
|
2022-10-13 05:53:03 +02:00
|
|
|
ErrorKind::UnexpectedArgument { .. } => "E031",
|
2022-10-21 00:52:36 +02:00
|
|
|
ErrorKind::RelativePathResolution(_) => "E032",
|
2022-11-10 00:47:07 +01:00
|
|
|
ErrorKind::DivisionByZero => "E033",
|
2023-01-15 12:52:37 +01:00
|
|
|
ErrorKind::Xml(_) => "E034",
|
2023-01-24 15:54:29 +01:00
|
|
|
ErrorKind::FromTomlError(_) => "E035",
|
2023-03-04 00:12:06 +01:00
|
|
|
ErrorKind::NotSerialisableToJson(_) => "E036",
|
2023-12-26 01:30:05 +01:00
|
|
|
ErrorKind::UnexpectedContext => "E037",
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 23:25:52 +01:00
|
|
|
ErrorKind::Utf8 => "E038",
|
2022-10-04 17:27:49 +02:00
|
|
|
|
2023-01-16 11:05:45 +01:00
|
|
|
// Special error code for errors from other Tvix
|
|
|
|
// components. We may want to introduce a code namespacing
|
|
|
|
// system to have these errors pass codes through.
|
|
|
|
ErrorKind::TvixError(_) => "E997",
|
|
|
|
|
2022-10-22 16:20:11 +02:00
|
|
|
// Special error code that is not part of the normal
|
|
|
|
// ordering.
|
|
|
|
ErrorKind::TvixBug { .. } => "E998",
|
|
|
|
|
2022-10-04 17:27:49 +02:00
|
|
|
// Placeholder error while Tvix is under construction.
|
2022-09-12 15:12:43 +02:00
|
|
|
ErrorKind::NotImplemented(_) => "E999",
|
2022-10-04 11:29:02 +02:00
|
|
|
|
2023-03-12 16:06:11 +01:00
|
|
|
// Chained errors should yield the code of the innermost
|
|
|
|
// error.
|
2023-03-12 19:49:29 +01:00
|
|
|
ErrorKind::NativeError { ref err, .. } | ErrorKind::BytecodeError(ref err) => {
|
|
|
|
err.code()
|
|
|
|
}
|
2023-01-16 18:02:33 +01:00
|
|
|
|
|
|
|
ErrorKind::WithContext { .. } => {
|
|
|
|
panic!("internal ErrorKind::WithContext variant leaked")
|
|
|
|
}
|
2022-09-12 15:12:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 09:38:33 +01:00
|
|
|
fn spans(&self) -> Vec<SpanLabel> {
|
2023-01-16 18:02:33 +01:00
|
|
|
let mut spans = match &self.kind {
|
2022-10-06 16:22:17 +02:00
|
|
|
ErrorKind::ImportParseError { errors, file, .. } => {
|
2022-12-20 15:22:56 +01:00
|
|
|
spans_for_parse_errors(file, errors)
|
2022-10-06 16:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorKind::ParseErrors(errors) => {
|
2024-02-20 09:38:33 +01:00
|
|
|
let file = self.source.get_file(self.span);
|
2022-10-06 16:22:17 +02:00
|
|
|
spans_for_parse_errors(&file, errors)
|
|
|
|
}
|
|
|
|
|
2022-10-13 05:53:03 +02:00
|
|
|
ErrorKind::UnexpectedArgument { formals_span, .. } => {
|
|
|
|
vec![
|
|
|
|
SpanLabel {
|
|
|
|
label: self.span_label(),
|
|
|
|
span: self.span,
|
|
|
|
style: SpanStyle::Primary,
|
|
|
|
},
|
|
|
|
SpanLabel {
|
|
|
|
label: Some("the accepted arguments".into()),
|
|
|
|
span: *formals_span,
|
|
|
|
style: SpanStyle::Secondary,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2023-03-12 22:56:03 +01:00
|
|
|
ErrorKind::InfiniteRecursion {
|
|
|
|
first_force,
|
|
|
|
suspended_at,
|
|
|
|
content_span,
|
|
|
|
} => {
|
|
|
|
let mut spans = vec![];
|
|
|
|
|
|
|
|
if let Some(content_span) = content_span {
|
|
|
|
spans.push(SpanLabel {
|
|
|
|
label: Some("this lazily-evaluated code".into()),
|
|
|
|
span: *content_span,
|
2023-03-12 22:30:27 +01:00
|
|
|
style: SpanStyle::Secondary,
|
2023-03-12 22:56:03 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(suspended_at) = suspended_at {
|
|
|
|
spans.push(SpanLabel {
|
|
|
|
label: Some("which was instantiated here".into()),
|
|
|
|
span: *suspended_at,
|
|
|
|
style: SpanStyle::Secondary,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
spans.push(SpanLabel {
|
|
|
|
label: Some("was first requested to be evaluated here".into()),
|
|
|
|
span: *first_force,
|
|
|
|
style: SpanStyle::Secondary,
|
|
|
|
});
|
|
|
|
|
|
|
|
spans.push(SpanLabel {
|
|
|
|
label: Some("but then requested again here during its own evaluation".into()),
|
|
|
|
span: self.span,
|
|
|
|
style: SpanStyle::Primary,
|
|
|
|
});
|
|
|
|
|
|
|
|
spans
|
2023-03-12 22:30:27 +01:00
|
|
|
}
|
|
|
|
|
2022-10-06 16:22:17 +02:00
|
|
|
// All other errors pretty much have the same shape.
|
|
|
|
_ => {
|
|
|
|
vec![SpanLabel {
|
|
|
|
label: self.span_label(),
|
|
|
|
span: self.span,
|
|
|
|
style: SpanStyle::Primary,
|
|
|
|
}]
|
|
|
|
}
|
2023-01-16 18:02:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for ctx in &self.contexts {
|
|
|
|
spans.push(SpanLabel {
|
|
|
|
label: Some(format!("while {}", ctx)),
|
|
|
|
span: self.span,
|
|
|
|
style: SpanStyle::Secondary,
|
|
|
|
});
|
2022-10-06 16:22:17 +02:00
|
|
|
}
|
2023-01-16 18:02:33 +01:00
|
|
|
|
|
|
|
spans
|
2022-10-06 16:22:17 +02:00
|
|
|
}
|
2022-09-12 15:12:43 +02:00
|
|
|
|
2022-10-11 00:04:19 +02:00
|
|
|
/// Create the primary diagnostic for a given error.
|
2024-02-20 09:38:33 +01:00
|
|
|
fn diagnostic(&self) -> Diagnostic {
|
2022-09-12 15:12:43 +02:00
|
|
|
Diagnostic {
|
|
|
|
level: Level::Error,
|
2022-10-08 20:28:16 +02:00
|
|
|
message: self.to_string(),
|
2024-02-20 09:38:33 +01:00
|
|
|
spans: self.spans(),
|
2022-09-12 15:12:43 +02:00
|
|
|
code: Some(self.code().into()),
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 00:04:19 +02:00
|
|
|
|
|
|
|
/// Return the primary diagnostic and all further associated diagnostics (if
|
|
|
|
/// any) of an error.
|
2024-02-20 09:38:33 +01:00
|
|
|
fn diagnostics(&self) -> Vec<Diagnostic> {
|
2022-10-11 00:04:19 +02:00
|
|
|
match &self.kind {
|
|
|
|
ErrorKind::ImportCompilerError { errors, .. } => {
|
2024-02-20 09:38:33 +01:00
|
|
|
let mut out = vec![self.diagnostic()];
|
|
|
|
out.extend(errors.iter().map(|e| e.diagnostic()));
|
2022-10-11 00:04:19 +02:00
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2023-03-12 16:06:11 +01:00
|
|
|
// When encountering either of these error kinds, we are dealing
|
|
|
|
// with the top of an error chain.
|
|
|
|
//
|
|
|
|
// An error chain creates a list of diagnostics which provide trace
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
// We don't know how deep this chain is, so we avoid recursing in
|
|
|
|
// this function while unrolling the chain.
|
2023-03-12 19:49:29 +01:00
|
|
|
ErrorKind::NativeError { err: next, .. } | ErrorKind::BytecodeError(next) => {
|
2023-03-12 16:06:11 +01:00
|
|
|
// Accumulated diagnostics to return.
|
|
|
|
let mut diagnostics: Vec<Diagnostic> = vec![];
|
|
|
|
|
|
|
|
// The next (inner) error to add to the diagnostics, after this
|
|
|
|
// one.
|
|
|
|
let mut next = *next.clone();
|
|
|
|
|
|
|
|
// Diagnostic message for *this* error.
|
|
|
|
let mut this_message = self.to_string();
|
|
|
|
|
|
|
|
// Primary span for *this* error.
|
|
|
|
let mut this_span = self.span;
|
|
|
|
|
|
|
|
// Diagnostic spans for *this* error.
|
2024-02-20 09:38:33 +01:00
|
|
|
let mut this_spans = self.spans();
|
2023-03-12 16:06:11 +01:00
|
|
|
|
|
|
|
loop {
|
|
|
|
if is_new_span(
|
|
|
|
this_span,
|
|
|
|
diagnostics.last().and_then(|last| last.spans.last()),
|
|
|
|
) {
|
|
|
|
diagnostics.push(Diagnostic {
|
|
|
|
level: Level::Note,
|
|
|
|
message: this_message,
|
|
|
|
spans: this_spans,
|
|
|
|
code: None, // only the top-level error has one
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this_message = next.to_string();
|
|
|
|
this_span = next.span;
|
2024-02-20 09:38:33 +01:00
|
|
|
this_spans = next.spans();
|
2023-03-12 16:06:11 +01:00
|
|
|
|
|
|
|
match next.kind {
|
2023-03-12 19:49:29 +01:00
|
|
|
ErrorKind::NativeError { err: inner, .. }
|
|
|
|
| ErrorKind::BytecodeError(inner) => {
|
2023-03-12 16:06:11 +01:00
|
|
|
next = *inner;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => {
|
2024-02-20 09:38:33 +01:00
|
|
|
diagnostics.extend(next.diagnostics());
|
2023-03-12 16:06:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diagnostics
|
|
|
|
}
|
|
|
|
|
2024-02-20 09:38:33 +01:00
|
|
|
_ => vec![self.diagnostic()],
|
2022-10-11 00:04:19 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-12 15:12:43 +02:00
|
|
|
}
|
2023-01-16 18:02:33 +01:00
|
|
|
|
2023-03-12 16:06:11 +01:00
|
|
|
// Check if this error is in a different span from its immediate ancestor.
|
|
|
|
fn is_new_span(this_span: Span, parent: Option<&SpanLabel>) -> bool {
|
|
|
|
match parent {
|
|
|
|
None => true,
|
|
|
|
Some(parent) => parent.span != this_span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 18:02:33 +01:00
|
|
|
// Convenience methods to add context on other types.
|
|
|
|
pub trait AddContext {
|
|
|
|
/// Add context to the error-carrying type.
|
|
|
|
fn context<S: Into<String>>(self, ctx: S) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddContext for ErrorKind {
|
|
|
|
fn context<S: Into<String>>(self, ctx: S) -> Self {
|
|
|
|
ErrorKind::WithContext {
|
|
|
|
context: ctx.into(),
|
|
|
|
underlying: Box::new(self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> AddContext for Result<T, ErrorKind> {
|
|
|
|
fn context<S: Into<String>>(self, ctx: S) -> Self {
|
|
|
|
self.map_err(|kind| kind.context(ctx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> AddContext for Result<T, Error> {
|
|
|
|
fn context<S: Into<String>>(self, ctx: S) -> Self {
|
|
|
|
self.map_err(|err| Error {
|
|
|
|
kind: err.kind.context(ctx),
|
|
|
|
..err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|