From 8c802dcd587d487387c99b057163becc827e0108 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 1 Jul 2021 12:27:29 +0800 Subject: [PATCH] Fix optimizer bug. --- CHANGELOG.md | 5 +++++ src/optimize.rs | 8 +++++++- tests/optimizer.rs | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1c9fac5..3680c038 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ---------------- diff --git a/src/optimize.rs b/src/optimize.rs index 7830efb7..22a67ac8 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -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) => { diff --git a/tests/optimizer.rs b/tests/optimizer.rs index 844577b5..dc25ab23 100644 --- a/tests/optimizer.rs +++ b/tests/optimizer.rs @@ -2,6 +2,25 @@ use rhai::{Engine, EvalAltResult, OptimizationLevel, INT}; +#[test] +fn test_optimizer() -> Result<(), Box> { + let mut engine = Engine::new(); + engine.set_optimization_level(OptimizationLevel::Full); + + assert_eq!( + engine.eval::( + " + fn foo(x) { print(x); return; } + fn foo2(x) { if x > 0 {} return; } + 42 + " + )?, + 42 + ); + + Ok(()) +} + #[test] fn test_optimizer_run() -> Result<(), Box> { fn run_test(engine: &mut Engine) -> Result<(), Box> {