From e1844714a859fd589b4827cedab25c368790fc94 Mon Sep 17 00:00:00 2001 From: xonx4l Date: Sun, 1 Mar 2026 11:55:24 +0530 Subject: [PATCH 1/2] Fix supertrait wrapping --- src/items.rs | 11 +++------ tests/source/issue-6809/supertraits.rs | 24 +++++++++++++++++++ .../issue-6809/supertraits_max_width_100.rs | 24 +++++++++++++++++++ tests/target/issue-6809/supertraits.rs | 24 +++++++++++++++++++ .../issue-6809/supertraits_max_width_100.rs | 24 +++++++++++++++++++ 5 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 tests/source/issue-6809/supertraits.rs create mode 100644 tests/source/issue-6809/supertraits_max_width_100.rs create mode 100644 tests/target/issue-6809/supertraits.rs create mode 100644 tests/target/issue-6809/supertraits_max_width_100.rs diff --git a/src/items.rs b/src/items.rs index 2c5fcf09c9f..85711cc53c6 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1191,14 +1191,9 @@ pub(crate) fn format_trait( return Err(RewriteError::Unknown); } - result = rewrite_assign_rhs_with( - context, - result + ":", - bounds, - shape, - &RhsAssignKind::Bounds, - RhsTactics::ForceNextLineWithoutIndent, - )?; + let lhs = result + ":"; + let bounds_shape = Shape::indented(offset, context.config); + result = rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, bounds_shape)?; } // Rewrite where-clause. diff --git a/tests/source/issue-6809/supertraits.rs b/tests/source/issue-6809/supertraits.rs new file mode 100644 index 00000000000..850086f2614 --- /dev/null +++ b/tests/source/issue-6809/supertraits.rs @@ -0,0 +1,24 @@ +// rustfmt-max_width: 50 + +// line below is 30 chars +pub(super) unsafe trait A: B { + fn foo(); +} + +// line below is 36 chars +pub unsafe trait Trait2: A + B + C { + fn foo(); +} + +// line below is 43 chars +pub trait TraitThree: Super1 + Super2 + X { + fn foo(); +} + +// line below is 47 chars +trait TraitNumberFour: Super1 + Super2 + Abcd { + fn foo(); +} + +// line below is correctly not wrapped +pub const THIS_LINE_IS_50_CHARS: usize = 12345678; diff --git a/tests/source/issue-6809/supertraits_max_width_100.rs b/tests/source/issue-6809/supertraits_max_width_100.rs new file mode 100644 index 00000000000..cf7468231d9 --- /dev/null +++ b/tests/source/issue-6809/supertraits_max_width_100.rs @@ -0,0 +1,24 @@ +// rustfmt-max_width: 100 + +// line below is 80 chars +pub(super) unsafe trait ALetsMakeThisLineLongerByExactlyFiftyCharacters1234: B { + fn foo(); +} + +// line below is 86 chars +pub unsafe trait Trait2LetsMakeThisLineLongerByExactlyFiftyCharacters1234: A + B + C { + fn foo(); +} + +// line below is 93 chars +pub trait TraitThreeLetsMakeThisLineLongerByExactlyFiftyCharacters1234: Super1 + Super2 + X { + fn foo(); +} + +// line below is 97 chars +trait TraitNumberFourLetsMakeThisLineLongerByExactlyFiftyCharacters1234: Super1 + Super2 + Abcd { + fn foo(); +} + +// line below is correctly not wrapped +pub const THIS_LINE_IS_100_CHARS: &str = "12345678901234567890123456789012345678901234567890123456"; diff --git a/tests/target/issue-6809/supertraits.rs b/tests/target/issue-6809/supertraits.rs new file mode 100644 index 00000000000..850086f2614 --- /dev/null +++ b/tests/target/issue-6809/supertraits.rs @@ -0,0 +1,24 @@ +// rustfmt-max_width: 50 + +// line below is 30 chars +pub(super) unsafe trait A: B { + fn foo(); +} + +// line below is 36 chars +pub unsafe trait Trait2: A + B + C { + fn foo(); +} + +// line below is 43 chars +pub trait TraitThree: Super1 + Super2 + X { + fn foo(); +} + +// line below is 47 chars +trait TraitNumberFour: Super1 + Super2 + Abcd { + fn foo(); +} + +// line below is correctly not wrapped +pub const THIS_LINE_IS_50_CHARS: usize = 12345678; diff --git a/tests/target/issue-6809/supertraits_max_width_100.rs b/tests/target/issue-6809/supertraits_max_width_100.rs new file mode 100644 index 00000000000..cf7468231d9 --- /dev/null +++ b/tests/target/issue-6809/supertraits_max_width_100.rs @@ -0,0 +1,24 @@ +// rustfmt-max_width: 100 + +// line below is 80 chars +pub(super) unsafe trait ALetsMakeThisLineLongerByExactlyFiftyCharacters1234: B { + fn foo(); +} + +// line below is 86 chars +pub unsafe trait Trait2LetsMakeThisLineLongerByExactlyFiftyCharacters1234: A + B + C { + fn foo(); +} + +// line below is 93 chars +pub trait TraitThreeLetsMakeThisLineLongerByExactlyFiftyCharacters1234: Super1 + Super2 + X { + fn foo(); +} + +// line below is 97 chars +trait TraitNumberFourLetsMakeThisLineLongerByExactlyFiftyCharacters1234: Super1 + Super2 + Abcd { + fn foo(); +} + +// line below is correctly not wrapped +pub const THIS_LINE_IS_100_CHARS: &str = "12345678901234567890123456789012345678901234567890123456"; From 8f019b2837c6364aa44aafcb92e5fb08024eff8e Mon Sep 17 00:00:00 2001 From: xonx4l Date: Tue, 3 Mar 2026 16:45:13 +0530 Subject: [PATCH 2/2] Gate Supertrait behind StyleEdition::2027 --- src/items.rs | 6 +++++- tests/source/issue-6809/supertraits_max_width_100.rs | 1 + tests/target/issue-6809/supertraits_max_width_100.rs | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index 85711cc53c6..746fe4b9fe8 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1190,9 +1190,13 @@ pub(crate) fn format_trait( if contains_comment(snippet) { return Err(RewriteError::Unknown); } + let bounds_shape = if context.config.style_edition() >= StyleEdition::Edition2027 { + Shape::indented(offset, context.config) + } else { + Shape::indented(offset, context.config).sub_width(result.len(), ident.span)? + }; let lhs = result + ":"; - let bounds_shape = Shape::indented(offset, context.config); result = rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, bounds_shape)?; } diff --git a/tests/source/issue-6809/supertraits_max_width_100.rs b/tests/source/issue-6809/supertraits_max_width_100.rs index cf7468231d9..222f89c5af8 100644 --- a/tests/source/issue-6809/supertraits_max_width_100.rs +++ b/tests/source/issue-6809/supertraits_max_width_100.rs @@ -1,3 +1,4 @@ +// rustfmt-style_edition: 2027 // rustfmt-max_width: 100 // line below is 80 chars diff --git a/tests/target/issue-6809/supertraits_max_width_100.rs b/tests/target/issue-6809/supertraits_max_width_100.rs index cf7468231d9..222f89c5af8 100644 --- a/tests/target/issue-6809/supertraits_max_width_100.rs +++ b/tests/target/issue-6809/supertraits_max_width_100.rs @@ -1,3 +1,4 @@ +// rustfmt-style_edition: 2027 // rustfmt-max_width: 100 // line below is 80 chars