Skip to content

Commit

Permalink
Fix try block optimizer bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Apr 3, 2024
1 parent 99c0539 commit af64584
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------
Expand Down
8 changes: 6 additions & 2 deletions src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ..) => {
Expand Down

0 comments on commit af64584

Please sign in to comment.