Skip to content

Rework smallest_range_containing to handle duplicates - #160198

Open
scottmcm wants to merge 1 commit into
rust-lang:mainfrom
scottmcm:redo-smallest_range_containing
Open

Rework smallest_range_containing to handle duplicates#160198
scottmcm wants to merge 1 commit into
rust-lang:mainfrom
scottmcm:redo-smallest_range_containing

Conversation

@scottmcm

Copy link
Copy Markdown
Member

@theemathas pointed out that this method I added in #159509 is implicitly assuming that there are no duplicates in the input. That's not a problem for its one use today -- an enum whose layout matters can't have duplicate discriminants -- but it could be a sharp edge in future, so this PR reworks it to handle duplicate values fine.

As a bonus, as I tried a couple different approaches (from just asserting to deduping to more) I found this rephrasing that I think is clearer. In particular, while the .iter().copied().cycle().skip(1) I'd written works, it's definitely not something that you look at and think "oh, obviously". I think this version using .array_windows::<2>() is easier to follow and splitting the wraparound and non-wraparound cases also simplifies the min_by_key lambda.

No changes to any layouts from this -- it just refactors this function.

@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 Jul 30, 2026
@rustbot

rustbot commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: codegen, compiler
  • codegen, compiler expanded to 74 candidates
  • Random selection from 17 candidates

@nnethercote nnethercote left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new code makes sense. Some observations from someone who doesn't know anything about enum representation stuff...

  • WrappingRange is a really weird type.
  • smallest_range_containing is a really weird operation on a really weird type.
  • The docs for smallest_range_containing contain only one case where the WrappingRange actually wraps, which seems low because that's trickier than the non-wrapping case.

View changes since this review

@nnethercote

Copy link
Copy Markdown
Contributor

r=me if you want it, after considering the comment above.

@scottmcm scottmcm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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