Skip to content

fix panics at newtype_index Step impl - #164891

Closed
illiasheshyn wants to merge 1 commit into
rust-lang:mainfrom
illiasheshyn:rustc-index-step-overflow
Closed

illiasheshyn wants to merge 1 commit into
rust-lang:mainfrom
illiasheshyn:rustc-index-step-overflow

Conversation

@illiasheshyn

Copy link
Copy Markdown

forward_checked (#69659) only checked for usize wrapping, allowing stepping past MAX and panicking at from_usize. It now returns None when exceeding the maximum value.
Similarly, forward_overflowing and backward_overflowing (#155114) exceeded MAX and wrapped past zero respectively, reaching the same from_usize assertion. Both now saturate, following precedents at library/core/src/iter/range.rs (e.g., impl Step for char). Regression tests have been added

LLM disclosure: I've used an LLM to summarise existing github issues, discover the bug and review the solution. The tests were generated by an LLM and edited by me. The solution was chosen and written by me based on existing Step implementations.

- `forward_checked` prevents stepping past `MAX`, which led to panic at
`from_usize`, instead of returning `None`
- `forward_overflowing` and `backward_overflowing` saturate at 0 and
`MAX` instead of wrapping at `usize` boundaries
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 25, 2026
@rustbot

rustbot commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @oli-obk (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 18 candidates

@oli-obk

oli-obk commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

considering we never hit those as ICEs, I would rather keep them or make them explicit. I don't think anyone is using those newtype indices in a fallible way that would be relevant here.

@oli-obk oli-obk closed this Oct 26, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants