Skip to content

Commit

Permalink
fix: check for error on def meta command (#381)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arthurpaulino authored Nov 9, 2024
1 parent be00d58 commit 6e52855
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/core/cli/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ impl<F: PrimeField32, C1: Chipset<F>, C2: Chipset<F>> MetaCmd<F, C1, C2> {
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)
Expand Down Expand Up @@ -1536,3 +1539,34 @@ pub(crate) fn meta_cmds<C1: Chipset<F>, C2: Chipset<F>>() -> MetaCmdsMap<F, C1,
assert_eq!(meta_cmds.len(), META_SYMBOLS.len());
meta_cmds
}

#[cfg(test)]
mod test {
use camino::{Utf8Path, Utf8PathBuf};
use once_cell::sync::OnceCell;

use crate::core::state::user_sym;

use super::{MetaCmd, Repl};

static DUMMY_PATH: OnceCell<Utf8PathBuf> = 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
);
}
}

0 comments on commit 6e52855

Please sign in to comment.