Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,15 +1190,14 @@ 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)?
};

result = rewrite_assign_rhs_with(
context,
result + ":",
bounds,
shape,
&RhsAssignKind::Bounds,
RhsTactics::ForceNextLineWithoutIndent,
)?;
let lhs = result + ":";
result = rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, bounds_shape)?;
}

// Rewrite where-clause.
Expand Down
24 changes: 24 additions & 0 deletions tests/source/issue-6809/supertraits.rs
Copy link
Contributor

Choose a reason for hiding this comment

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

You should consider naming this file supertraits_max_width_50.rs for consistency with supertraits_max_width_100.rs

Original file line number Diff line number Diff line change
@@ -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;
Comment on lines +23 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, I'm not sure why we have an example with const here since that doesn't demonstrate trait formatting.

25 changes: 25 additions & 0 deletions tests/source/issue-6809/supertraits_max_width_100.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// rustfmt-style_edition: 2027
// 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();
}
Comment on lines +19 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a test case where the trait definition with super traits is exactly max_width and another where we are exactly max_width + 1 to show that we wrap once we're over the max width.


// line below is correctly not wrapped
pub const THIS_LINE_IS_100_CHARS: &str = "12345678901234567890123456789012345678901234567890123456";
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why this is here. This PR is focusing on traits, not const items.

24 changes: 24 additions & 0 deletions tests/target/issue-6809/supertraits.rs
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 25 additions & 0 deletions tests/target/issue-6809/supertraits_max_width_100.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// rustfmt-style_edition: 2027
// 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!


// 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";
Loading