refactor: share the negative-index normalize and clamp sequence, and name the insert scratch locals
- 主要語言
- Rust
- 星號
- 22
- 分支
- 6
- 平均合併
- 1 天 3 小時
- 30 天內合併 PR
- 6
描述
## Summary
#121 fixed `list.insert` by adding a negative-index normalize-and-clamp sequence to its match arm. That sequence is semantically identical to one the slice arm already has, and the arm addresses its scratch locals by raw offset rather than by name. Both are worth cleaning up in one follow-up. #121 has merged, so this is ready to pick up.
## Current state
- The slice arm carries a `normalize_and_clamp` closure that normalizes a negative bound by adding the length, then applies `max(0)` and `min(length)`: [src/compiler/expression.rs:5954](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L5954)
- The `insert` arm emits the same three `select` steps inline, 23 instruction emissions duplicating that logic: [src/compiler/expression.rs:9878](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L9878)
- The same arm writes `ctx.temp_local + 6` about fifteen times and `ctx.temp_local + 2` several times, with the meaning carried only by a trailing comment on the first use: [src/compiler/expression.rs:9857](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L9857)
- Neighbouring helpers in the same file bind names first, for example `let newlen = ctx.temp_local + 6;` at [src/compiler/expression.rs:355](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L355) and `let cap = ctx.temp_local + 11;` at [src/compiler/expression.rs:1296](https://github.com/anistark/waspy/blob/2141159/src/compiler/expression.rs#L1296)
## Proposed change
- Lift the closure to a free `fn emit_normalize_and_clamp(func, index_local, len_local)` and call it from both the slice arm and the `insert` arm.
- Bind `let index = ctx.temp_local + 6;` and `let length = ctx.temp_local + 2;` at the top of the `insert` arm and use those names throughout, matching the style of the surrounding helpers.
## Acceptance criteria
- One helper emits the normalize-and-clamp sequence, and both call sites use it.
- The `insert` arm addresses its scratch locals by name, and the helper's doc comment records which scratch offsets it owns.
- The existing suite passes unchanged, including `list_insert_honours_position` and the slice tests. This is a pure refactor, so no test needs new expectations.
## References
- Follow-up to #121, which introduced the duplicated sequence.
貢獻指南
評估
這個 Issue 還沒有評估資料。