Skip to content

Commit e79db51

Browse files
committed
Issue-5892: Type alias generic rewrite follows the same rule as trait
rewrite
1 parent 4489061 commit e79db51

File tree

8 files changed

+146
-6
lines changed

8 files changed

+146
-6
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ tests/cargo-fmt/**/target
2323
.idea/
2424
.vscode/
2525
*~
26+
27+
# nvim local dap settings
28+
.nvim-dap.lua

src/expr.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -2057,12 +2057,38 @@ fn rewrite_assignment(
20572057
let lhs_shape = shape.sub_width(operator_str.len() + 1)?;
20582058
let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str);
20592059

2060+
let lhs_lines: Vec<&str> = lhs_str.split("\n").collect();
2061+
2062+
let mut rhs_shape = shape.clone();
2063+
2064+
for line in lhs_lines.into_iter().rev() {
2065+
let mut indent_width = 0;
2066+
let mut first_char = ' ';
2067+
for char in line.chars() {
2068+
if char != ' ' {
2069+
first_char = char;
2070+
break;
2071+
} else {
2072+
indent_width += 1;
2073+
}
2074+
}
2075+
2076+
if first_char != '/' {
2077+
let indent = Indent::from_width(&context.config, indent_width);
2078+
rhs_shape = Shape::indented(indent, &context.config);
2079+
break;
2080+
}
2081+
}
2082+
2083+
println!("config={:?}", context.config.max_width());
2084+
println!("old shape={shape:?}");
2085+
println!("new shape={rhs_shape:?}");
20602086
rewrite_assign_rhs(
20612087
context,
20622088
lhs_str,
20632089
rhs,
20642090
&RhsAssignKind::Expr(&rhs.kind, rhs.span),
2065-
shape,
2091+
rhs_shape,
20662092
)
20672093
}
20682094

src/items.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -1768,10 +1768,27 @@ fn rewrite_ty<R: Rewrite>(
17681768

17691769
if let Some(bounds) = generic_bounds_opt {
17701770
if !bounds.is_empty() {
1771-
// 2 = `: `
1772-
let shape = Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
1773-
let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?;
1774-
result.push_str(&type_bounds);
1771+
match context.config.style_edition() {
1772+
style_edition if style_edition < StyleEdition::Edition2024 => {
1773+
// 2 = `: `
1774+
let shape =
1775+
Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
1776+
let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?;
1777+
result.push_str(&type_bounds);
1778+
}
1779+
_ => {
1780+
let shape =
1781+
Shape::indented(indent, context.config).offset_left(result.len())?;
1782+
result = rewrite_assign_rhs_with(
1783+
context,
1784+
result.clone() + ":",
1785+
bounds,
1786+
shape,
1787+
&RhsAssignKind::Bounds,
1788+
RhsTactics::ForceNextLineWithoutIndent,
1789+
)?;
1790+
}
1791+
}
17751792
}
17761793
}
17771794

src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ fn join_bounds_inner(
12111211
// or the single item is of type `Trait`,
12121212
// and any of the internal arrays contains more than one item;
12131213
let retry_with_force_newline = match context.config.style_edition() {
1214-
style_edition @ _ if style_edition <= StyleEdition::Edition2021 => {
1214+
style_edition if style_edition <= StyleEdition::Edition2021 => {
12151215
!force_newline
12161216
&& items.len() > 1
12171217
&& (result.0.contains('\n') || result.0.len() > shape.width)

tests/source/issue-189.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-style_edition: 2024
2+
3+
impl SomeType {
4+
fn method(&mut self) {
5+
self.array[array_index as usize]
6+
.as_mut()
7+
.expect("thing must exist")
8+
.extra_info = Some(ExtraInfo {
9+
parent,
10+
count: count as u16,
11+
children: children.into_boxed_slice(),
12+
});
13+
}
14+
}

tests/source/issue-5892.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// rustfmt-style_edition: 2024
2+
3+
type AAAAAAAAAAAAA:
4+
BBBBBBBBBBBBBBB<
5+
CCCCCCCCCCCCCCCCC,
6+
DDDDDDDDDDDDDDDDD,
7+
EEEEEEEEEEEEEEEEE,
8+
FFFFFFFFFFFFFFFFF,
9+
GGGGGGGGGGGGGGGGG,
10+
HHHHHHHHHHHHHHHHH,
11+
IIIIIIIIIIIIIIIII,
12+
>;
13+
14+
type AAAAAAAAAAAAA:
15+
BBBBBBBBBBBBBBB<
16+
CCCCCCCCCCCCCCCCC,
17+
DDDDDDDDDDDDDDDDD,
18+
EEEEEEEEEEEEEEEEE,
19+
FFFFFFFFFFFFFFFFF,
20+
GGGGGGGGGGGGGGGGG,
21+
HHHHHHHHHHHHHHHHH,
22+
IIIIIIIIIIIIIIIII,
23+
> + Eq
24+
+ PartialEq;
25+
26+
// previous error: maximum length exceeded
27+
type SomeType:
28+
BBBBBBBBBBBBBBB<CCCCCCCCCCCCCCCCC, DDDDDDDDDDDDDDDDD, EEEEEEEEEEEEEEEEE, FFFFFFFFFFFFFFFFF>
29+
+ AAAAAAAAAAAAA;
30+
31+
// previous error: maximum length exceeded
32+
type SomeType:
33+
BBBBBBBBBBBBBBB<CCCCCCCCCCCCCCCCC, DDDDDDDDDDDDDDDDD, EEEEEEEEEEEEEEEEE, FFFFFFFFFFFFFFFFF>;

tests/target/issue-189.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-style_edition: 2024
2+
3+
impl SomeType {
4+
fn method(&mut self) {
5+
self.array[array_index as usize]
6+
.as_mut()
7+
.expect("thing must exist")
8+
.extra_info = Some(ExtraInfo {
9+
parent,
10+
count: count as u16,
11+
children: children.into_boxed_slice(),
12+
});
13+
}
14+
}

tests/target/issue-5892.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// rustfmt-style_edition: 2024
2+
3+
type AAAAAAAAAAAAA:
4+
BBBBBBBBBBBBBBB<
5+
CCCCCCCCCCCCCCCCC,
6+
DDDDDDDDDDDDDDDDD,
7+
EEEEEEEEEEEEEEEEE,
8+
FFFFFFFFFFFFFFFFF,
9+
GGGGGGGGGGGGGGGGG,
10+
HHHHHHHHHHHHHHHHH,
11+
IIIIIIIIIIIIIIIII,
12+
>;
13+
14+
type AAAAAAAAAAAAA:
15+
BBBBBBBBBBBBBBB<
16+
CCCCCCCCCCCCCCCCC,
17+
DDDDDDDDDDDDDDDDD,
18+
EEEEEEEEEEEEEEEEE,
19+
FFFFFFFFFFFFFFFFF,
20+
GGGGGGGGGGGGGGGGG,
21+
HHHHHHHHHHHHHHHHH,
22+
IIIIIIIIIIIIIIIII,
23+
> + Eq
24+
+ PartialEq;
25+
26+
// previous error: maximum length exceeded
27+
type SomeType:
28+
BBBBBBBBBBBBBBB<CCCCCCCCCCCCCCCCC, DDDDDDDDDDDDDDDDD, EEEEEEEEEEEEEEEEE, FFFFFFFFFFFFFFFFF>
29+
+ AAAAAAAAAAAAA;
30+
31+
// previous error: maximum length exceeded
32+
type SomeType:
33+
BBBBBBBBBBBBBBB<CCCCCCCCCCCCCCCCC, DDDDDDDDDDDDDDDDD, EEEEEEEEEEEEEEEEE, FFFFFFFFFFFFFFFFF>;

0 commit comments

Comments
 (0)