Skip to content

Use saturating arithmetics for adjust_max_width - #7152

Merged
ytmimi merged 3 commits into
rust-lang:mainfrom
jieyouxu:struct-variant-width
Sep 29, 2026
Merged

ytmimi merged 3 commits into
rust-lang:mainfrom
jieyouxu:struct-variant-width

Conversation

@jieyouxu

@jieyouxu jieyouxu commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #7147. That was introduced in #6651.

The problem

Previously in adjust_max_width we had

    // overflows whenever value < -delta
    let adjust = |value: usize| (value as isize + delta) as usize;

this will overflow (panic in debug, wraparound in release) if value < -delta. For struct_variant_width, we'd bail with a warning

`struct_variant_width` cannot have a value that exceeds `max_width`. `struct_variant_width` will be set to the same value as `max_width`

Solution

Properly use saturating arithmetics for {adjust_max_width, reduce_max_width, increase_max_width}.

Also fix wrong doc comment for increase_max_width.

Notes for the reviewer


  • I did not use an LLM to create a change in this PR.
  • I used an LLM to create a change in this PR, and I have explained below how it was used.

I used an LLM to co-review my changes, but did not use it to create changes.

Instead of overflows, or arguably worse, bitwise reinterpretations. They
tend to give us garbage (and massive) indentations.
@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Sep 29, 2026
@jieyouxu

jieyouxu commented Sep 29, 2026 •

Copy link
Copy Markdown
Member Author

Unfortunately I think this made it to 1.100 beta if I got my release cycle right, so we may need/want to consider a 1.100 beta backport

rustfmt +beta --version
rustfmt 1.11.0-beta (e3feeb59cd 2026-09-27)

r? @ytmimi (or another rustfmt reviewer)

@jieyouxu jieyouxu added X-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) X-impacts-stable-options Expected formatting impact: impacts stable options (caution) X-stability-guarantee-exempt Feature: impacts stable, but explicitly exempt from format stability guarantees. See README A-line-width Area: line width interactions SO-struct_variant_width Stable option: struct_variant_width and removed X-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) X-impacts-stable-options Expected formatting impact: impacts stable options (caution) X-stability-guarantee-exempt Feature: impacts stable, but explicitly exempt from format stability guarantees. See README labels Sep 29, 2026
@jieyouxu jieyouxu changed the title fix(config): use saturating arithmetics for adjust_max_width Use saturating arithmetics for adjust_max_width Sep 29, 2026
@ytmimi
ytmimi added this pull request to the merge queue Sep 29, 2026
Merged via the queue into rust-lang:main with commit 677b954 Sep 29, 2026
33 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 Sep 29, 2026
@ytmimi

ytmimi commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

@jieyouxu Yeah, let's try to get a beta backport out for this since it made it into 1.100

@jieyouxu
jieyouxu deleted the struct-variant-width branch September 30, 2026 03:24
@jieyouxu jieyouxu added beta-nominated Nominated for backporting to the beta channel. beta-accepted Accepted for backporting to the beta channel. labels Sep 30, 2026
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Sep 30, 2026
rustfmt subtree update

Subtree update of `rustfmt` to rust-lang/rustfmt@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang/rustfmt#7115 (config search behavior)
* rust-lang/rustfmt#7095 (potentially breaking)
* rust-lang/rustfmt#6396
* rust-lang/rustfmt#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang/rustfmt#7152

---

r? @ytmimi
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Sep 30, 2026
rustfmt subtree update

Subtree update of `rustfmt` to rust-lang/rustfmt@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang/rustfmt#7115 (config search behavior)
* rust-lang/rustfmt#7095 (potentially breaking)
* rust-lang/rustfmt#6396
* rust-lang/rustfmt#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang/rustfmt#7152

---

r? @ytmimi
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Sep 30, 2026
rustfmt subtree update

Subtree update of `rustfmt` to rust-lang/rustfmt@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang/rustfmt#7115 (config search behavior)
* rust-lang/rustfmt#7095 (potentially breaking)
* rust-lang/rustfmt#6396
* rust-lang/rustfmt#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang/rustfmt#7152

---

r? @ytmimi
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 1, 2026
rustfmt subtree update

Subtree update of `rustfmt` to rust-lang/rustfmt@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang/rustfmt#7115 (config search behavior)
* rust-lang/rustfmt#7095 (potentially breaking)
* rust-lang/rustfmt#6396
* rust-lang/rustfmt#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang/rustfmt#7152

---

r? @ytmimi
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 1, 2026
[beta] rustfmt backport

Cherry-pick of commits from rust-lang/rustfmt#7152 to address rust-lang/rustfmt#7147. The commits are cherry-picked from the subtree push #163545.

Approved in [#t-rustfmt/backports > rustfmt#7152: beta-nominated](https://rust-lang.zulipchat.com/#narrow/channel/621384-t-rustfmt.2Fbackports/topic/rustfmt.237152.3A.20beta-nominated/with/627991509).

I suppose this can be rolled up into a combined beta backport too. Anyway.

r? @ytmimi
@ytmimi ytmimi added this to the 1.100.0 milestone Oct 1, 2026
@ytmimi

ytmimi commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Added the 1.100.0 Milestone since this was backported to the 1.100.0 beta via rust-lang/rust#163547

rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 3, 2026
rustfmt subtree update



Subtree update of `rustfmt` to rust-lang/rustfmt@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang/rustfmt#7115 (config search behavior)
* rust-lang/rustfmt#7095 (potentially breaking)
* rust-lang/rustfmt#6396
* rust-lang/rustfmt#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang/rustfmt#7152

---

r? @ytmimi
@rustbot rustbot modified the milestones: 1.100.0, 1.101.0 Oct 3, 2026
@ytmimi ytmimi modified the milestones: 1.101.0, 1.100.0 Oct 3, 2026
github-actions Bot pushed a commit to rust-lang/stdarch that referenced this pull request Oct 5, 2026
rustfmt subtree update



Subtree update of `rustfmt` to rust-lang/rustfmt@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang/rustfmt#7115 (config search behavior)
* rust-lang/rustfmt#7095 (potentially breaking)
* rust-lang/rustfmt#6396
* rust-lang/rustfmt#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang/rustfmt#7152

---

r? @ytmimi
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Oct 5, 2026
rustfmt subtree update



Subtree update of `rustfmt` to rust-lang/rustfmt@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang/rustfmt#7115 (config search behavior)
* rust-lang/rustfmt#7095 (potentially breaking)
* rust-lang/rustfmt#6396
* rust-lang/rustfmt#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang/rustfmt#7152

---

r? @ytmimi
programskillforverification pushed a commit to programskillforverification/miri that referenced this pull request Oct 5, 2026
rustfmt subtree update



Subtree update of `rustfmt` to rust-lang/rustfmt@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang/rustfmt#7115 (config search behavior)
* rust-lang/rustfmt#7095 (potentially breaking)
* rust-lang/rustfmt#6396
* rust-lang/rustfmt#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang/rustfmt#7152

---

r? @ytmimi
AzureXuanVerse pushed a commit to AzureXuanVerse/rustfmt that referenced this pull request Oct 7, 2026
rustfmt subtree update



Subtree update of `rustfmt` to rust-lang@677b954.

Created using https://github.com/rust-lang/josh-sync.

### Relnotes worthy (1.101 cycle)

https://github.com/rust-lang/rustfmt/pulls?q=is%3Apr+state%3Aclosed+merged%3A2026-09-22..2026-09-30 (excluding the v1.11.0 release prep PR):

* rust-lang#7115 (config search behavior)
* rust-lang#7095 (potentially breaking)
* rust-lang#6396
* rust-lang#7152 (but that's a fix for beta-regression)

### Needs beta backport (targetting 1.100)

I will make a beta-targetting cherry-pick with the following PR

* rust-lang#7152

---

r? @ytmimi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-line-width Area: line width interactions beta-accepted Accepted for backporting to the beta channel. beta-nominated Nominated for backporting to the beta channel. release-notes Needs an associated changelog entry SO-struct_variant_width Stable option: struct_variant_width X-impacts-stable-options Expected formatting impact: impacts stable options (caution) X-stability-guarantee-exempt Feature: impacts stable, but explicitly exempt from format stability guarantees. See README

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect "struct_variant_width cannot have a value that exceeds max_width" when using macros

3 participants