Skip to content

Commit 879b592

Browse files
committed
Fix for issue 4689 - indentation on multiline single generic bound
1 parent 509ac2e commit 879b592

File tree

3 files changed

+342
-4
lines changed

3 files changed

+342
-4
lines changed

src/formatting/types.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,24 @@ fn join_bounds_inner(
942942
ast::GenericBound::Trait(..) => last_line_extendable(s),
943943
};
944944

945+
// Whether a PathSegment segment includes internal array containing more than one item
946+
let is_segment_with_multi_items_array = |seg: &ast::PathSegment| {
947+
if let Some(args_in) = &seg.args {
948+
match &**args_in {
949+
ast::AngleBracketed(args) => {
950+
if args.args.len() > 1 {
951+
true
952+
} else {
953+
false
954+
}
955+
}
956+
_ => false,
957+
}
958+
} else {
959+
false
960+
}
961+
};
962+
945963
let result = items.iter().enumerate().try_fold(
946964
(String::new(), None, false),
947965
|(strs, prev_trailing_span, prev_extendable), (i, item)| {
@@ -1036,10 +1054,33 @@ fn join_bounds_inner(
10361054
},
10371055
)?;
10381056

1039-
if !force_newline
1040-
&& items.len() > 1
1041-
&& (result.0.contains('\n') || result.0.len() > shape.width)
1042-
{
1057+
// Whether retry the function with forced newline is needed:
1058+
// Only if result is not already multiline and did not exceed line width,
1059+
// and either there is more than one item;
1060+
// or the single item is of type `Trait`,
1061+
// and any of the internal arrays contains more than one item;
1062+
let retry_with_force_newline =
1063+
if force_newline || (!result.0.contains('\n') && result.0.len() <= shape.width) {
1064+
false
1065+
} else {
1066+
if items.len() > 1 {
1067+
true
1068+
} else {
1069+
match items[0] {
1070+
ast::GenericBound::Trait(ref poly_trait_ref, ..) => {
1071+
let segments = &poly_trait_ref.trait_ref.path.segments;
1072+
if segments.len() > 1 {
1073+
true
1074+
} else {
1075+
is_segment_with_multi_items_array(&segments[0])
1076+
}
1077+
}
1078+
_ => false,
1079+
}
1080+
}
1081+
};
1082+
1083+
if retry_with_force_newline {
10431084
join_bounds_inner(context, shape, items, need_indent, true)
10441085
} else {
10451086
Some(result.0)

tests/source/issue-4689.rs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Based on the issue description
2+
pub trait PrettyPrinter<'tcx>:
3+
Printer<
4+
'tcx,
5+
Error = fmt::Error,
6+
Path = Self,
7+
Region = Self,
8+
Type = Self,
9+
DynExistential = Self,
10+
Const = Self,
11+
>
12+
{
13+
//
14+
}
15+
pub trait PrettyPrinter<'tcx>:
16+
Printer<
17+
'tcx,
18+
Error = fmt::Error,
19+
Path = Self,
20+
Region = Self,
21+
Type = Self,
22+
DynExistential = Self,
23+
Const = Self,
24+
> + fmt::Write
25+
{
26+
//
27+
}
28+
pub trait PrettyPrinter<'tcx>:
29+
Printer<
30+
'tcx,
31+
Error = fmt::Error,
32+
Path = Self,
33+
Region = Self,
34+
Type = Self,
35+
DynExistential = Self,
36+
Const = Self,
37+
> + fmt::Write1 + fmt::Write2
38+
{
39+
//
40+
}
41+
pub trait PrettyPrinter<'tcx>:
42+
fmt::Write + Printer<
43+
'tcx,
44+
Error = fmt::Error,
45+
Path = Self,
46+
Region = Self,
47+
Type = Self,
48+
DynExistential = Self,
49+
Const = Self,
50+
>
51+
{
52+
//
53+
}
54+
pub trait PrettyPrinter<'tcx>:
55+
fmt::Write + Printer1<
56+
'tcx,
57+
Error = fmt::Error,
58+
Path = Self,
59+
Region = Self,
60+
Type = Self,
61+
DynExistential = Self,
62+
Const = Self,
63+
> + Printer2<
64+
'tcx,
65+
Error = fmt::Error,
66+
Path = Self,
67+
Region = Self,
68+
Type = Self,
69+
DynExistential = Self,
70+
Const = Self,
71+
>
72+
{
73+
//
74+
}
75+
76+
// Some test cases to ensure other cases formatting were not changed
77+
fn f() -> Box<
78+
FnMut() -> Thing<
79+
WithType = LongItemName,
80+
Error = LONGLONGLONGLONGLONGONGEvenLongerErrorNameLongerLonger,
81+
>,
82+
> {
83+
}
84+
fn f() -> Box<
85+
FnMut() -> Thing<
86+
WithType = LongItemName,
87+
Error = LONGLONGLONGLONGLONGONGEvenLongerErrorNameLongerLonger,
88+
> + fmt::Write1
89+
+ fmt::Write2,
90+
> {
91+
}
92+
93+
fn foo<F>(foo2: F)
94+
where
95+
F: Fn(
96+
// this comment is deleted
97+
)
98+
{
99+
}
100+
fn foo<F>(foo2: F)
101+
where
102+
F: Fn(
103+
// this comment is deleted
104+
) + fmt::Write
105+
{
106+
}
107+
108+
fn elaborate_bounds<F>(mut mk_cand: F)
109+
where
110+
F: for<> FnMut(
111+
&mut ProbeContext<>,
112+
ty::PolyTraitRefffffffffffffffffffffffffffffffff<>,
113+
tyyyyyyyyyyyyyyyyyyyyy::AssociatedItem,
114+
),
115+
{
116+
}
117+
fn elaborate_bounds<F>(mut mk_cand: F)
118+
where
119+
F: for<> FnMut(
120+
&mut ProbeContext<>,
121+
ty::PolyTraitRefffffffffffffffffffffffffffffffff<>,
122+
tyyyyyyyyyyyyyyyyyyyyy::AssociatedItem,
123+
) + fmt::Write,
124+
{
125+
}
126+
127+
fn build_sorted_static_get_entry_names(
128+
mut entries: entryyyyyyyy,
129+
) -> (
130+
impl Fn(
131+
AlphabeticalTraversal,
132+
Seconddddddddddddddddddddddddddddddddddd
133+
) -> Parammmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
134+
+ Sendddddddddddddddddddddddddddddddddddddddddddd
135+
) {
136+
}
137+
138+
pub trait SomeTrait:
139+
Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
140+
+ Eqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
141+
{
142+
}
143+
144+
trait B = where
145+
for<'b> &'b Self: Send
146+
+ Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
147+
+ Copyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy;

tests/target/issue-4689.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Based on the issue description
2+
pub trait PrettyPrinter<'tcx>:
3+
Printer<
4+
'tcx,
5+
Error = fmt::Error,
6+
Path = Self,
7+
Region = Self,
8+
Type = Self,
9+
DynExistential = Self,
10+
Const = Self,
11+
>
12+
{
13+
//
14+
}
15+
pub trait PrettyPrinter<'tcx>:
16+
Printer<
17+
'tcx,
18+
Error = fmt::Error,
19+
Path = Self,
20+
Region = Self,
21+
Type = Self,
22+
DynExistential = Self,
23+
Const = Self,
24+
> + fmt::Write
25+
{
26+
//
27+
}
28+
pub trait PrettyPrinter<'tcx>:
29+
Printer<
30+
'tcx,
31+
Error = fmt::Error,
32+
Path = Self,
33+
Region = Self,
34+
Type = Self,
35+
DynExistential = Self,
36+
Const = Self,
37+
> + fmt::Write1
38+
+ fmt::Write2
39+
{
40+
//
41+
}
42+
pub trait PrettyPrinter<'tcx>:
43+
fmt::Write
44+
+ Printer<
45+
'tcx,
46+
Error = fmt::Error,
47+
Path = Self,
48+
Region = Self,
49+
Type = Self,
50+
DynExistential = Self,
51+
Const = Self,
52+
>
53+
{
54+
//
55+
}
56+
pub trait PrettyPrinter<'tcx>:
57+
fmt::Write
58+
+ Printer1<
59+
'tcx,
60+
Error = fmt::Error,
61+
Path = Self,
62+
Region = Self,
63+
Type = Self,
64+
DynExistential = Self,
65+
Const = Self,
66+
> + Printer2<
67+
'tcx,
68+
Error = fmt::Error,
69+
Path = Self,
70+
Region = Self,
71+
Type = Self,
72+
DynExistential = Self,
73+
Const = Self,
74+
>
75+
{
76+
//
77+
}
78+
79+
// Some test cases to ensure other cases formatting were not changed
80+
fn f() -> Box<
81+
FnMut() -> Thing<
82+
WithType = LongItemName,
83+
Error = LONGLONGLONGLONGLONGONGEvenLongerErrorNameLongerLonger,
84+
>,
85+
> {
86+
}
87+
fn f() -> Box<
88+
FnMut() -> Thing<
89+
WithType = LongItemName,
90+
Error = LONGLONGLONGLONGLONGONGEvenLongerErrorNameLongerLonger,
91+
> + fmt::Write1
92+
+ fmt::Write2,
93+
> {
94+
}
95+
96+
fn foo<F>(foo2: F)
97+
where
98+
F: Fn(
99+
// this comment is deleted
100+
),
101+
{
102+
}
103+
fn foo<F>(foo2: F)
104+
where
105+
F: Fn(
106+
// this comment is deleted
107+
) + fmt::Write,
108+
{
109+
}
110+
111+
fn elaborate_bounds<F>(mut mk_cand: F)
112+
where
113+
F: FnMut(
114+
&mut ProbeContext,
115+
ty::PolyTraitRefffffffffffffffffffffffffffffffff,
116+
tyyyyyyyyyyyyyyyyyyyyy::AssociatedItem,
117+
),
118+
{
119+
}
120+
fn elaborate_bounds<F>(mut mk_cand: F)
121+
where
122+
F: FnMut(
123+
&mut ProbeContext,
124+
ty::PolyTraitRefffffffffffffffffffffffffffffffff,
125+
tyyyyyyyyyyyyyyyyyyyyy::AssociatedItem,
126+
) + fmt::Write,
127+
{
128+
}
129+
130+
fn build_sorted_static_get_entry_names(
131+
mut entries: entryyyyyyyy,
132+
) -> (
133+
impl Fn(
134+
AlphabeticalTraversal,
135+
Seconddddddddddddddddddddddddddddddddddd,
136+
) -> Parammmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
137+
+ Sendddddddddddddddddddddddddddddddddddddddddddd
138+
) {
139+
}
140+
141+
pub trait SomeTrait:
142+
Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
143+
+ Eqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
144+
{
145+
}
146+
147+
trait B = where
148+
for<'b> &'b Self: Send
149+
+ Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
150+
+ Copyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy;

0 commit comments

Comments
 (0)