Skip to content

Fix supertrait wrapping when within max_width#6814

Open
xonx4l wants to merge 2 commits intorust-lang:mainfrom
xonx4l:fix-supertrait-wrapping-6809
Open

Fix supertrait wrapping when within max_width#6814
xonx4l wants to merge 2 commits intorust-lang:mainfrom
xonx4l:fix-supertrait-wrapping-6809

Conversation

@xonx4l
Copy link

@xonx4l xonx4l commented Mar 1, 2026

This PR fixes an issue where supertraits were being forced to a new line even when they fit within the configured max_width as mentioned in #6809

Proposed Solution:
Use the original indented Shape for trait bounds instead of a manually reduced one preventing the "double-width reduction" math error in format_trait. This allows rewrite_assign_rhs to correctly calculate available horizontal space, keeping bounds on the same line when they fit within max_width.

The Changes -
Updated format_trait in src/items.rs to use the original indented offset and full available width for the bounds_shape.
Added rewrite_assign_rhs to handle the width reduction naturally based on the actual length of the trait header (lhs).
Replaced the manual newline logic with the dynamic wrapping provided by RhsAssignKind::Bounds.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Mar 1, 2026
@jieyouxu jieyouxu added F-impacts-stable-default-format Expected formatting impact: affects stable default format configuration (caution) F-requires-next-style-edition Expected formatting impact: can only be done over a style edition (caution) F-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) labels Mar 1, 2026
@@ -0,0 +1,24 @@
// rustfmt-max_width: 100
Copy link
Member

Choose a reason for hiding this comment

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

Question (for other rustfmt maintainers): I believe max_width is a stable option with a default value of 100.

  • This impacts a stable option.
  • This impacts formatting of the default value of a stable option.
  • The stable code is already formatted.

I believe this fix needs to be gated behind a style-edition.

Copy link
Author

Choose a reason for hiding this comment

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

You are right . I think gating this behind StyleEdition::2024 will be good and won't regress formatting . Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

I think you will need to gate it behind StyleEdition::2027, StyleEdition::2024 is already the default inferred style edition if crate Rust edition is 2024.

Copy link
Author

Choose a reason for hiding this comment

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

Gated it behind StyleEdition::2027. Thanks!

@jieyouxu jieyouxu added SO-max_width Stable option: max_width B-next-style-edition-gate Blocked: can only be done over a style edition; needs a style edition gate 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 Mar 1, 2026
@jieyouxu jieyouxu removed the B-next-style-edition-gate Blocked: can only be done over a style edition; needs a style edition gate label Mar 3, 2026
@matthewhughes934
Copy link

To open a discussion: I also had a look into this issue and came up with another approach: c58a7c1 (but saw this was opened before creating a PR) that passes the test suite and so I don't think that change would require a version gate

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) F-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) F-requires-next-style-edition Expected formatting impact: can only be done over a style edition (caution) S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. SO-max_width Stable option: max_width

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants