Fix bug with capturing undefined variable.
This commit is contained in:
parent
ea38185cac
commit
fc976172e7
@ -11,6 +11,7 @@ Bug fixes
|
||||
|
||||
* `switch` cases with conditions that evaluate to constant `()` no longer optimize to `false` (should raise a type error during runtime).
|
||||
* Fixes concatenation of BLOB's and strings, where the BLOB's should be interpreted as UTF-8 encoded strings.
|
||||
* Capturing an unknown variable in a closure no longer panics.
|
||||
|
||||
New features
|
||||
------------
|
||||
|
@ -1016,7 +1016,7 @@ impl Engine {
|
||||
|
||||
// Share statement
|
||||
#[cfg(not(feature = "no_closure"))]
|
||||
Stmt::Share(name, ..) => {
|
||||
Stmt::Share(name, pos) => {
|
||||
if let Some((index, ..)) = scope.get_index(name) {
|
||||
let val = scope.get_mut_by_index(index);
|
||||
|
||||
@ -1024,10 +1024,10 @@ impl Engine {
|
||||
// Replace the variable with a shared value.
|
||||
*val = std::mem::take(val).into_shared();
|
||||
}
|
||||
Ok(Dynamic::UNIT)
|
||||
} else {
|
||||
unreachable!("variable {} not found for sharing", name);
|
||||
Err(ERR::ErrorVariableNotFound(name.to_string(), *pos).into())
|
||||
}
|
||||
Ok(Dynamic::UNIT)
|
||||
}
|
||||
|
||||
_ => unreachable!("statement cannot be evaluated: {:?}", stmt),
|
||||
|
@ -42,21 +42,35 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
fn test_closures() -> Result<(), Box<EvalAltResult>> {
|
||||
let mut engine = Engine::new();
|
||||
let mut scope = Scope::new();
|
||||
|
||||
scope.push("x", 42 as INT);
|
||||
|
||||
assert!(matches!(
|
||||
*engine
|
||||
.compile_expression("let f = |x| {};")
|
||||
engine
|
||||
.compile_expression("|x| {}")
|
||||
.expect_err("should error")
|
||||
.0,
|
||||
.err_type(),
|
||||
ParseErrorType::BadInput(..)
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
engine.eval_with_scope::<INT>(
|
||||
&mut scope,
|
||||
"
|
||||
let f = || { x };
|
||||
f.call()
|
||||
",
|
||||
)?,
|
||||
42
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
"
|
||||
let foo = #{ x: 42 };
|
||||
let f = || { this.x };
|
||||
foo.call(f)
|
||||
foo.call(f)
|
||||
",
|
||||
)?,
|
||||
42
|
||||
|
Loading…
Reference in New Issue
Block a user