Fix optimizer bug.

This commit is contained in:
Stephen Chung 2021-07-01 12:27:29 +08:00
parent a0f51a1a39
commit 8c802dcd58
3 changed files with 31 additions and 1 deletions

View File

@ -8,6 +8,11 @@ The official version `1.0`.
Almost the same version as `0.20.3` but with deprecated API's removed.
Bug fixes
---------
* Fixed infinite loop in certain script optimizations.
Breaking changes
----------------

View File

@ -297,8 +297,14 @@ fn optimize_stmt_block(
Stmt::Noop(pos)
};
}
// { ...; stmt; noop } -> done
[.., ref second_last_stmt, Stmt::Noop(_)]
if second_last_stmt.returns_value() => {}
if second_last_stmt.returns_value() =>
{
break
}
// { ...; stmt_that_returns; pure_non_value_stmt } -> { ...; stmt_that_returns; noop }
// { ...; stmt; pure_non_value_stmt } -> { ...; stmt }
[.., ref second_last_stmt, ref last_stmt]
if !last_stmt.returns_value() && is_pure(last_stmt) =>
{

View File

@ -2,6 +2,25 @@
use rhai::{Engine, EvalAltResult, OptimizationLevel, INT};
#[test]
fn test_optimizer() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.set_optimization_level(OptimizationLevel::Full);
assert_eq!(
engine.eval::<INT>(
"
fn foo(x) { print(x); return; }
fn foo2(x) { if x > 0 {} return; }
42
"
)?,
42
);
Ok(())
}
#[test]
fn test_optimizer_run() -> Result<(), Box<EvalAltResult>> {
fn run_test(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {