Skip to content

Prevent ranges from getting collapsed in patterns#6871

Merged
ytmimi merged 8 commits into
rust-lang:mainfrom
ytmimi:issue_6869
May 30, 2026
Merged

Prevent ranges from getting collapsed in patterns#6871
ytmimi merged 8 commits into
rust-lang:mainfrom
ytmimi:issue_6869

Conversation

@ytmimi
Copy link
Copy Markdown
Contributor

@ytmimi ytmimi commented Apr 17, 2026

Fixes #6869

Expression rewriting handled this correctly, but there was distinct logic implemented for patterns. I've extracted the range formatting logic used by expressions into a common rewrite_range function and reused that when rewriting ranges in patterns and types. Was able to remove a nice amount of code after these changes were done.

PR is broken up into logical commits. Feel free to review the PR as a whole or commit by commit.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Apr 17, 2026
@ytmimi
Copy link
Copy Markdown
Contributor Author

ytmimi commented Apr 17, 2026

Just to be sure these changes wouldn't impact things I ran the Diff-Check --edition=2021 --style-edition=2021. Things seem to be working as expected.

Copy link
Copy Markdown
Contributor

@matthewhughes934 matthewhughes934 left a comment

Choose a reason for hiding this comment

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

Comment thread src/types.rs Outdated
Comment thread src/range.rs Outdated
@rustbot

This comment has been minimized.

@jieyouxu jieyouxu self-assigned this Apr 23, 2026
@jieyouxu jieyouxu added the F-impacts-stable-default-format Expected formatting impact: affects stable default format configuration (caution) label May 28, 2026
Copy link
Copy Markdown
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Thanks, this looks good to me. This impacts stable default format but previously this was working code -> broken code on ranges in patterns. AFAICT this shouldn't affect other cases (though it's possible there are edge cases that I failed to consider and that DiffCheck is only run on a limited set of repos, and that this code pattern is comparatively rare to hit in practice).

View changes since this review

@jieyouxu jieyouxu added the pr-ready-to-merge Status: PR is largely ready for merge, waiting for secondary review / last nits label May 28, 2026
Comment thread tests/target/issue_6869.rs
@jieyouxu
Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: 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 May 28, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 28, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@ytmimi
Copy link
Copy Markdown
Contributor Author

ytmimi commented May 28, 2026

Thanks, this looks good to me. This impacts stable default format but previously this was working code -> broken code on ranges in patterns. AFAICT this shouldn't affect other cases (though it's possible there are edge cases that I failed to consider and that DiffCheck is only run on a limited set of repos, and that this code pattern is comparatively rare to hit in practice).

I think we should be good here. Especially since the formatting rustfmt produced broke the code. I also wouldn't expect this to impact any other stable formatting because I feel like we have enough range formatting coverage in the test suite, but just to be sure I'll expand on the tests I add here.

ytmimi added 8 commits May 29, 2026 23:44
Move the range rewrite logic from `src/exrp.rs` to `src/range.rs`. There are a
few different places where we rewrite ranges so centralizing the logic in a
single location is ideal.
Cleans up the syntax a little. Was suggested in the PR review.
Reuse the range rewrite logic that we abstracted into `src/range.rs`.
Reuse the range rewrite logic that we abstracted into `src/range.rs`.
Now that we've centralized the logic into `rewrite_range` we no longer need a
dedicated `rewrite_range_pat` function. Getting rid of that also lets us remove
some other unnecessary code which is nice.
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 30, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@ytmimi
Copy link
Copy Markdown
Contributor Author

ytmimi commented May 30, 2026

@rustbot ready

@jieyouxu when you have a chance can you take another look. I added a lot of different test cases with both the spaces_around_ranges and float_literal_trailing_zero option. Both options are unstable and don't impact the default formatting for either float_literal_trailing_zero=Preserve and spaces_around_ranges=false so I think these changes are okay.

@rustbot rustbot added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. labels May 30, 2026
@ytmimi
Copy link
Copy Markdown
Contributor Author

ytmimi commented May 30, 2026

Running the Diff-Check for good measure, but as you mentioned we might not hit the bug case in real code. Will still be good to check and make sure that the formatting produced by the main branch matches this issue_6869 branch.

@jieyouxu
Copy link
Copy Markdown
Member

New DiffCheck looks clean 👍, though as discussed, it won't be too representative, but still better than nothing :)

Copy link
Copy Markdown
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Thanks, the new tests looks good 👍

View changes since this review

@ytmimi ytmimi merged commit b3b6e28 into rust-lang:main May 30, 2026
30 checks passed
@rustbot rustbot added release-notes Needs an associated changelog entry and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels May 30, 2026
@ytmimi ytmimi deleted the issue_6869 branch May 30, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-impacts-stable-default-format Expected formatting impact: affects stable default format configuration (caution) pr-ready-to-merge Status: PR is largely ready for merge, waiting for secondary review / last nits release-notes Needs an associated changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatting float ranges may result in invalid code

4 participants