Skip to content

Commit 0bbfdea

Browse files
committed
Backport PR rust-lang#4730 that fix issue rust-lang#4689
1 parent a67d909 commit 0bbfdea

File tree

3 files changed

+320
-4
lines changed

3 files changed

+320
-4
lines changed

src/types.rs

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

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

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

tests/source/issue-4689.rs

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
pub trait SomeTrait:
128+
Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
129+
+ Eqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
130+
{
131+
}
132+
133+
trait B = where
134+
for<'b> &'b Self: Send
135+
+ Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
136+
+ Copyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy;

tests/target/issue-4689.rs

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
pub trait SomeTrait:
131+
Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
132+
+ Eqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
133+
{
134+
}
135+
136+
trait B = where
137+
for<'b> &'b Self: Send
138+
+ Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
139+
+ Copyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy;

0 commit comments

Comments
 (0)