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

Issue-189: rhs should use indent of last line of lhs
  • Loading branch information
johnhuichen committed Aug 31, 2024
1 parent 4489061 commit 519ffc4
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 7 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
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2024-08-17"
channel = "nightly-2024-08-08"
components = ["llvm-tools", "rustc-dev"]
28 changes: 27 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,12 +2057,38 @@ fn rewrite_assignment(
let lhs_shape = shape.sub_width(operator_str.len() + 1)?;
let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str);

let lhs_lines: Vec<&str> = lhs_str.split("\n").collect();

let mut rhs_shape = shape.clone();

for line in lhs_lines.into_iter().rev() {
let mut indent_width = 0;
let mut first_char = ' ';
for char in line.chars() {
if char != ' ' {
first_char = char;
break;
} else {
indent_width += 1;
}
}

if first_char != '/' {
let indent = Indent::from_width(&context.config, indent_width);
rhs_shape = Shape::indented(indent, &context.config);
break;
}
}

println!("config={:?}", context.config.max_width());
println!("old shape={shape:?}");
println!("new shape={rhs_shape:?}");
rewrite_assign_rhs(
context,
lhs_str,
rhs,
&RhsAssignKind::Expr(&rhs.kind, rhs.span),
shape,
rhs_shape,
)
}

Expand Down
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
14 changes: 14 additions & 0 deletions tests/source/issue-189.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-style_edition: 2024

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info = Some(ExtraInfo {
parent,
count: count as u16,
children: children.into_boxed_slice(),
});
}
}
33 changes: 33 additions & 0 deletions tests/source/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>;
24 changes: 24 additions & 0 deletions tests/target/issue-189.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// rustfmt-style_edition: 2024

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info = Some(ExtraInfo {
parent,
count: count as u16,
children: children.into_boxed_slice(),
});
}
}

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info =
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
}
}
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 519ffc4

Please sign in to comment.