From af64584a9bad86baa407dc00d1dc32ac92c139d0 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 3 Apr 2024 19:19:57 +0800 Subject: [PATCH] Fix try block optimizer bug. --- CHANGELOG.md | 1 + src/optimizer.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aad485184..4c73eda46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Bug fixes * The `optimize` command in `rhai-repl` now works properly and cycles through `None`->`Simple`->`Full`. * `Engine::call_fn_XXX` no longer return errors unnecessarily wrapped in `EvalAltResult::ErrorInFunctionCall`. * Some tests that panic on 32-bit architecture are fixed (thanks [`@alexanderkjall`](https://github.com/alexanderkjall) [851](https://github.com/rhaiscript/rhai/issues/851)). +* The optimizer no longer goes into an infinite loop when optimizing a `try` statement with an empty body. Deprecated API's ---------------- diff --git a/src/optimizer.rs b/src/optimizer.rs index 5a52e6033..7e412f3f3 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -798,8 +798,12 @@ fn optimize_stmt(stmt: &mut Stmt, state: &mut OptimizerState, preserve_result: b Stmt::TryCatch(x, ..) if x.body.iter().all(Stmt::is_pure) => { // If try block is pure, there will never be any exceptions state.set_dirty(); - *x.body.statements_mut() = - optimize_stmt_block(x.body.take_statements(), state, false, true, false); + let statements = x.body.take_statements(); + let block = StmtBlock::new_with_span( + optimize_stmt_block(statements, state, false, true, false), + x.body.span(), + ); + *stmt = Stmt::Block(block.into()); } // try { try_block } catch ( var ) { catch_block } Stmt::TryCatch(x, ..) => {