diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a2a0515..64e02268 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,12 +18,14 @@ Potentially breaking changes * The trait method `ModuleResolver::resolve_raw` (which is a low-level API) now takes a `&mut Scope` parameter. This is a breaking change because the signature is modified, but this trait method has a default and is rarely called/implemented in practice. * `Module::eval_ast_as_new_raw` (a low-level API) now takes a `&mut Scope` instead of the `Scope` parameter. This is a breaking change because the `&mut` is now required. +* `Engine::allow_loop_expressions` now correctly defaults to `true` (was erroneously `false` by default). Enhancements ------------ * The functions `min` and `max` are added for numbers. * Range cases in `switch` statements now also match floating-point and decimal values. In order to support this, however, small numeric ranges cases are no longer unrolled. +* Loading a module via `import` now gives the module access to the current scope, including variables and constants defined inside. Version 1.12.0 diff --git a/src/api/options.rs b/src/api/options.rs index 325dbbcb..057a9ac3 100644 --- a/src/api/options.rs +++ b/src/api/options.rs @@ -41,6 +41,7 @@ impl LangOptions { pub fn new() -> Self { Self::IF_EXPR | Self::SWITCH_EXPR + | Self::LOOP_EXPR | Self::STMT_EXPR | Self::LOOPING | Self::SHADOWING diff --git a/src/parser.rs b/src/parser.rs index fc9e8966..db75c099 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1254,7 +1254,9 @@ impl Engine { }; if forbidden { - return Err(PERR::WrongSwitchIntegerCase.into_err(expr.start_position())); + return Err( + PERR::WrongSwitchIntegerCase.into_err(expr.start_position()) + ); } } diff --git a/tests/looping.rs b/tests/looping.rs index a27a3555..e011eb6b 100644 --- a/tests/looping.rs +++ b/tests/looping.rs @@ -20,7 +20,7 @@ fn test_loop() -> Result<(), Box> { } } - return x; + x " )?, 21 @@ -53,3 +53,43 @@ fn test_loop() -> Result<(), Box> { Ok(()) } + +#[test] +fn test_loop_expression() -> Result<(), Box> { + let mut engine = Engine::new(); + + assert_eq!( + engine.eval::( + " + let x = 0; + + let value = while x < 10 { + if x % 5 == 0 { break 42; } + x += 1; + }; + + value + " + )?, + 42 + ); + + engine.set_allow_loop_expressions(false); + + assert!(engine + .eval::( + " + let x = 0; + + let value = while x < 10 { + if x % 5 == 0 { break 42; } + x += 1; + }; + + value + " + ) + .is_err()); + + Ok(()) +}