refactor(eval): make CatchableErrorKind::Throw hold a NixString

The messages we can throw are not necessarily UTF-8 strings. The
to_string() in there did store the result of the Display impl, which is
a quoted string.

Change-Id: I65a77ccc7f2d62ff06a2a9458cdb7e7292f132b0
Reviewed-on: https://cl.snix.dev/c/snix/+/30489
Tested-by: besadii
Autosubmit: Florian Klink <flokli@flokli.de>
Reviewed-by: Bence Nemes <nemes.bence1@gmail.com>
This commit is contained in:
Florian Klink 2025-05-07 15:19:31 +03:00 committed by clbot
parent bbc1efdb0e
commit 7eb15f8123
3 changed files with 7 additions and 16 deletions

View file

@ -1405,11 +1405,10 @@ mod pure_builtins {
if message.is_catchable() { if message.is_catchable() {
return Ok(message); return Ok(message);
} }
// TODO(sterni): coerces to string
// TODO(sterni): coerce to string
// We do not care about the context here explicitly. // We do not care about the context here explicitly.
Ok(Value::from(CatchableErrorKind::Throw( Ok(Value::from(CatchableErrorKind::Throw(message.to_str()?)))
message.to_contextful_str()?.to_string().into(),
)))
} }
#[builtin("toString")] #[builtin("toString")]

View file

@ -39,7 +39,7 @@ use crate::{SourceCode, Value};
#[derive(thiserror::Error, Clone, Debug)] #[derive(thiserror::Error, Clone, Debug)]
pub enum CatchableErrorKind { pub enum CatchableErrorKind {
#[error("error thrown: {0}")] #[error("error thrown: {0}")]
Throw(Box<str>), Throw(NixString),
#[error("assertion failed")] #[error("assertion failed")]
AssertionFailed, AssertionFailed,

View file

@ -18,11 +18,7 @@ fn test_source_builtin() {
); );
let value = result.value.unwrap(); let value = result.value.unwrap();
assert!( assert_matches!(value, Value::Integer(i) if i == 42);
matches!(value, Value::Integer(42)),
"expected the integer 42, but got {}",
value,
);
} }
#[test] #[test]
@ -32,11 +28,7 @@ fn skip_broken_bytecode() {
.evaluate(/* code = */ "x", None); .evaluate(/* code = */ "x", None);
assert_eq!(result.errors.len(), 1); assert_eq!(result.errors.len(), 1);
assert_matches!(result.errors[0].kind, ErrorKind::UnknownStaticVariable);
assert!(matches!(
result.errors[0].kind,
ErrorKind::UnknownStaticVariable
));
} }
/// Checks that deep forcing happens in lexicographic key order /// Checks that deep forcing happens in lexicographic key order
@ -58,6 +50,6 @@ fn key_order_deep_force() {
assert_matches!( assert_matches!(
&result.errors[0].kind, &result.errors[0].kind,
ErrorKind::CatchableError(CatchableErrorKind::Throw(s)) if s.as_ref() == "\"aaa\"" ErrorKind::CatchableError(CatchableErrorKind::Throw(s)) if s == &NixString::from("aaa")
); );
} }