From 6e52855eabff1dd7db743a8eb43fe09a794d5d0f Mon Sep 17 00:00:00 2001 From: Arthur Paulino Date: Fri, 8 Nov 2024 22:22:55 -0300 Subject: [PATCH] fix: check for error on `def` meta command (#381) The simplification of `def` erased the check for errors before binding the result to the symbol. This patch adds that check back. Further, include a test for the `def` meta command. We can use this approach to extend the tests for other meta commands. --- src/core/cli/meta.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/core/cli/meta.rs b/src/core/cli/meta.rs index 4a8f2cde..9592f9fb 100644 --- a/src/core/cli/meta.rs +++ b/src/core/cli/meta.rs @@ -239,6 +239,9 @@ impl, C2: Chipset> MetaCmd { let [&sym, &expr] = repl.take(args)?; Self::validate_binding_symbol(repl, &sym)?; let (val, _) = repl.reduce_aux(&expr)?; + if val.tag == Tag::Err { + bail!(repl.fmt(&val)); + } repl.memoize_dag(&val); repl.bind(sym, val); Ok(sym) @@ -1536,3 +1539,34 @@ pub(crate) fn meta_cmds, C2: Chipset>() -> MetaCmdsMap = OnceCell::new(); + fn dummy_path() -> &'static Utf8Path { + DUMMY_PATH.get_or_init(Utf8PathBuf::default) + } + + #[test] + fn test_def() { + let mut repl = Repl::new_native(); + let foo = repl.zstore.intern_symbol_no_lang(&user_sym("foo")); + let a = repl.zstore.intern_symbol_no_lang(&user_sym("a")); + let args = repl.zstore.intern_list([foo, a]); + assert!((MetaCmd::DEF.run)(&mut repl, &args, dummy_path()).is_err()); + + let a = repl.zstore.intern_char('a'); + let args = repl.zstore.intern_list([foo, a]); + assert_eq!( + (MetaCmd::DEF.run)(&mut repl, &args, dummy_path()).unwrap(), + foo + ); + } +}