Skip to content

Commit ea017d7

Browse files
authored
Backport PR #4730 (#5390)
* Backport PR #4730 that fix issue #4689 * Test files for each Verion One and Two * Simplify per review comment - use defer and matches! * Changes per reviewer comments for reducing indentations
1 parent a67d909 commit ea017d7

File tree

5 files changed

+639
-4
lines changed

5 files changed

+639
-4
lines changed

src/types.rs

+39-4
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,28 @@ fn join_bounds_inner(
941941
ast::GenericBound::Trait(..) => last_line_extendable(s),
942942
};
943943

944+
// Whether a GenericBound item is a PathSegment segment that includes internal array
945+
// that contains more than one item
946+
let is_item_with_multi_items_array = |item: &ast::GenericBound| match item {
947+
ast::GenericBound::Trait(ref poly_trait_ref, ..) => {
948+
let segments = &poly_trait_ref.trait_ref.path.segments;
949+
if segments.len() > 1 {
950+
true
951+
} else {
952+
if let Some(args_in) = &segments[0].args {
953+
matches!(
954+
args_in.deref(),
955+
ast::GenericArgs::AngleBracketed(bracket_args)
956+
if bracket_args.args.len() > 1
957+
)
958+
} else {
959+
false
960+
}
961+
}
962+
}
963+
_ => false,
964+
};
965+
944966
let result = items.iter().enumerate().try_fold(
945967
(String::new(), None, false),
946968
|(strs, prev_trailing_span, prev_extendable), (i, item)| {
@@ -1035,10 +1057,23 @@ fn join_bounds_inner(
10351057
},
10361058
)?;
10371059

1038-
if !force_newline
1039-
&& items.len() > 1
1040-
&& (result.0.contains('\n') || result.0.len() > shape.width)
1041-
{
1060+
// Whether retry the function with forced newline is needed:
1061+
// Only if result is not already multiline and did not exceed line width,
1062+
// and either there is more than one item;
1063+
// or the single item is of type `Trait`,
1064+
// and any of the internal arrays contains more than one item;
1065+
let retry_with_force_newline =
1066+
if force_newline || (!result.0.contains('\n') && result.0.len() <= shape.width) {
1067+
false
1068+
} else {
1069+
if items.len() > 1 {
1070+
true
1071+
} else {
1072+
is_item_with_multi_items_array(&items[0])
1073+
}
1074+
};
1075+
1076+
if retry_with_force_newline {
10421077
join_bounds_inner(context, shape, items, need_indent, true)
10431078
} else {
10441079
Some(result.0)

tests/source/issue-4689/one.rs

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

tests/source/issue-4689/two.rs

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

0 commit comments

Comments
 (0)