refactor(interpreter): extract handle_loop_control_flow helper#891
refactor(interpreter): extract handle_loop_control_flow helper#891
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors the bash interpreter’s loop execution logic by extracting shared break/continue/return propagation and errexit_suppressed tracking into a centralized helper, reducing duplication across loop command implementations.
Changes:
- Introduces
LoopActionandhandle_loop_control_flow()to centralize loop control-flow handling. - Updates
execute_for,execute_arithmetic_for, andexecute_condition_loopto use the new helper. - Preserves loop-level
errexit_suppressedpropagation vialast_errexit_suppressed.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| LoopAction::Return(ExecResult { | ||
| stdout: stdout.to_string(), | ||
| stderr: stderr.to_string(), | ||
| exit_code, | ||
| control_flow: ControlFlow::Break(n - 1), | ||
| ..Default::default() | ||
| }) |
There was a problem hiding this comment.
handle_loop_control_flow builds an ExecResult for propagated break/continue/return by cloning the aggregated stdout/stderr via to_string(). Previously these early-return paths moved the existing Strings, so this introduces an O(n) copy and extra memory proportional to accumulated output. Consider changing the helper to return only the decremented ControlFlow (or an action + control-flow value) so callers can return ExecResult { stdout, stderr, ... } by moving the existing buffers, or accept &mut String and use std::mem::take on return paths.
| /// Handle control flow from a loop body iteration result. | ||
| /// | ||
| /// Tracks `errexit_suppressed` from the result. Returns: | ||
| /// - `LoopAction::Continue` — proceed to next iteration |
There was a problem hiding this comment.
The docstring says LoopAction::Continue means “proceed to next iteration”, but at least in execute_arithmetic_for it intentionally falls through to run the step expression before the next iteration. Consider rewording this to something like “continue loop normally” to avoid implying it skips post-body logic.
| /// - `LoopAction::Continue` — proceed to next iteration | |
| /// - `LoopAction::Continue` — continue the loop normally (no break/return) |
43f8372 to
3b61c38
Compare
Summary
handle_loop_control_flow()helper withLoopActionenum to centralize break/continue/return propagation anderrexit_suppressedtrackingTest plan
cargo test --all-featurespassescargo fmt --checkandcargo clippycleanCloses #881