Skip to content

Commit

Permalink
Issue-5892: Type alias generic rewrite follows the same rule as trait
Browse files Browse the repository at this point in the history
rewrite
  • Loading branch information
johnhuichen committed Aug 25, 2024
1 parent 4489061 commit c801834
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ tests/cargo-fmt/**/target
.idea/
.vscode/
*~

# nvim local dap settings
.nvim-dap.lua
25 changes: 21 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1768,10 +1768,27 @@ fn rewrite_ty<R: Rewrite>(

if let Some(bounds) = generic_bounds_opt {
if !bounds.is_empty() {
// 2 = `: `
let shape = Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?;
result.push_str(&type_bounds);
match context.config.style_edition() {
style_edition if style_edition < StyleEdition::Edition2024 => {
// 2 = `: `
let shape =
Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?;
result.push_str(&type_bounds);
}
_ => {
let shape =
Shape::indented(indent, context.config).offset_left(result.len())?;
result = rewrite_assign_rhs_with(
context,
result.clone() + ":",
bounds,
shape,
&RhsAssignKind::Bounds,
RhsTactics::ForceNextLineWithoutIndent,
)?;
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ fn join_bounds_inner(
// or the single item is of type `Trait`,
// and any of the internal arrays contains more than one item;
let retry_with_force_newline = match context.config.style_edition() {
style_edition @ _ if style_edition <= StyleEdition::Edition2021 => {
style_edition if style_edition <= StyleEdition::Edition2021 => {
!force_newline
&& items.len() > 1
&& (result.0.contains('\n') || result.0.len() > shape.width)
Expand Down
35 changes: 35 additions & 0 deletions tests/source/issue-5892.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// rustfmt-style_edition: 2024

type AAAAAAAAAAAAA: BBBBBBBBBBBBBBB<
CCCCCCCCCCCCCCCCC,
DDDDDDDDDDDDDDDDD,
EEEEEEEEEEEEEEEEE,
FFFFFFFFFFFFFFFFF,
GGGGGGGGGGGGGGGGG,
HHHHHHHHHHHHHHHHH,
IIIIIIIIIIIIIIIII,
>;

type AAAAAAAAAAAAA: BBBBBBBBBBBBBBB<
CCCCCCCCCCCCCCCCC,
DDDDDDDDDDDDDDDDD,
EEEEEEEEEEEEEEEEE,
FFFFFFFFFFFFFFFFF,
GGGGGGGGGGGGGGGGG,
HHHHHHHHHHHHHHHHH,
IIIIIIIIIIIIIIIII,
> + Eq + PartialEq;

// previous error: maximum length exceeded
type SomeType: BBBBBBBBBBBBBBB<
CCCCCCCCCCCCCCCCC,
DDDDDDDDDDDDDDDDD,
EEEEEEEEEEEEEEEEE,
FFFFFFFFFFFFFFFFF> + AAAAAAAAAAAAA;

// previous error: maximum length exceeded
type SomeType: BBBBBBBBBBBBBBB<
CCCCCCCCCCCCCCCCC,
DDDDDDDDDDDDDDDDD,
EEEEEEEEEEEEEEEEE,
FFFFFFFFFFFFFFFFF>;
33 changes: 33 additions & 0 deletions tests/target/issue-5892.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// rustfmt-style_edition: 2024

type AAAAAAAAAAAAA:
BBBBBBBBBBBBBBB<
CCCCCCCCCCCCCCCCC,
DDDDDDDDDDDDDDDDD,
EEEEEEEEEEEEEEEEE,
FFFFFFFFFFFFFFFFF,
GGGGGGGGGGGGGGGGG,
HHHHHHHHHHHHHHHHH,
IIIIIIIIIIIIIIIII,
>;

type AAAAAAAAAAAAA:
BBBBBBBBBBBBBBB<
CCCCCCCCCCCCCCCCC,
DDDDDDDDDDDDDDDDD,
EEEEEEEEEEEEEEEEE,
FFFFFFFFFFFFFFFFF,
GGGGGGGGGGGGGGGGG,
HHHHHHHHHHHHHHHHH,
IIIIIIIIIIIIIIIII,
> + Eq
+ PartialEq;

// previous error: maximum length exceeded
type SomeType:
BBBBBBBBBBBBBBB<CCCCCCCCCCCCCCCCC, DDDDDDDDDDDDDDDDD, EEEEEEEEEEEEEEEEE, FFFFFFFFFFFFFFFFF>
+ AAAAAAAAAAAAA;

// previous error: maximum length exceeded
type SomeType:
BBBBBBBBBBBBBBB<CCCCCCCCCCCCCCCCC, DDDDDDDDDDDDDDDDD, EEEEEEEEEEEEEEEEE, FFFFFFFFFFFFFFFFF>;

0 comments on commit c801834

Please sign in to comment.