Skip to content

Commit a426d6f

Browse files
Implement use<> formatting in rustfmt
1 parent c3d7fb3 commit a426d6f

File tree

5 files changed

+105
-9
lines changed

5 files changed

+105
-9
lines changed

src/tools/rustfmt/src/overflow.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub(crate) enum OverflowableItem<'a> {
8383
TuplePatField(&'a TuplePatField<'a>),
8484
Ty(&'a ast::Ty),
8585
Pat(&'a ast::Pat),
86+
PreciseCapturingArg(&'a ast::PreciseCapturingArg),
8687
}
8788

8889
impl<'a> Rewrite for OverflowableItem<'a> {
@@ -123,6 +124,7 @@ impl<'a> OverflowableItem<'a> {
123124
OverflowableItem::TuplePatField(pat) => f(*pat),
124125
OverflowableItem::Ty(ty) => f(*ty),
125126
OverflowableItem::Pat(pat) => f(*pat),
127+
OverflowableItem::PreciseCapturingArg(arg) => f(*arg),
126128
}
127129
}
128130

@@ -137,6 +139,9 @@ impl<'a> OverflowableItem<'a> {
137139
matches!(meta_item.kind, ast::MetaItemKind::Word)
138140
}
139141
},
142+
// FIXME: Why don't we consider `SegmentParam` to be simple?
143+
// FIXME: If we also fix `SegmentParam`, then we should apply the same
144+
// heuristic to `PreciseCapturingArg`.
140145
_ => false,
141146
}
142147
}
@@ -244,7 +249,15 @@ macro_rules! impl_into_overflowable_item_for_rustfmt_types {
244249
}
245250
}
246251

247-
impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, FieldDef, Ty, Pat);
252+
impl_into_overflowable_item_for_ast_node!(
253+
Expr,
254+
GenericParam,
255+
NestedMetaItem,
256+
FieldDef,
257+
Ty,
258+
Pat,
259+
PreciseCapturingArg
260+
);
248261
impl_into_overflowable_item_for_rustfmt_types!([MacroArg], [SegmentParam, TuplePatField]);
249262

250263
pub(crate) fn into_overflowable_list<'a, T>(

src/tools/rustfmt/src/spanned.rs

+9
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,12 @@ impl Spanned for ast::NestedMetaItem {
203203
self.span()
204204
}
205205
}
206+
207+
impl Spanned for ast::PreciseCapturingArg {
208+
fn span(&self) -> Span {
209+
match self {
210+
ast::PreciseCapturingArg::Lifetime(lt) => lt.ident.span,
211+
ast::PreciseCapturingArg::Arg(path, _) => path.span,
212+
}
213+
}
214+
}

src/tools/rustfmt/src/types.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ impl<'a> Rewrite for SegmentParam<'a> {
177177
}
178178
}
179179

180+
impl Rewrite for ast::PreciseCapturingArg {
181+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
182+
match self {
183+
ast::PreciseCapturingArg::Lifetime(lt) => lt.rewrite(context, shape),
184+
ast::PreciseCapturingArg::Arg(p, _) => {
185+
rewrite_path(context, PathContext::Type, &None, p, shape)
186+
}
187+
}
188+
}
189+
}
190+
180191
impl Rewrite for ast::AssocItemConstraint {
181192
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
182193
use ast::AssocItemConstraintKind::{Bound, Equality};
@@ -564,9 +575,10 @@ impl Rewrite for ast::GenericBound {
564575
.map(|s| format!("{constness}{asyncness}{polarity}{s}"))
565576
.map(|s| if has_paren { format!("({})", s) } else { s })
566577
}
578+
ast::GenericBound::Use(ref args, span) => {
579+
overflow::rewrite_with_angle_brackets(context, "use", args.iter(), shape, span)
580+
}
567581
ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),
568-
// FIXME(precise_capturing): Should implement formatting before stabilization.
569-
ast::GenericBound::Use(..) => None,
570582
}
571583
}
572584
}
@@ -933,9 +945,7 @@ fn rewrite_bare_fn(
933945
fn is_generic_bounds_in_order(generic_bounds: &[ast::GenericBound]) -> bool {
934946
let is_trait = |b: &ast::GenericBound| match b {
935947
ast::GenericBound::Outlives(..) => false,
936-
ast::GenericBound::Trait(..) => true,
937-
// FIXME(precise_capturing): This ordering fn should be reworked.
938-
ast::GenericBound::Use(..) => false,
948+
ast::GenericBound::Trait(..) | ast::GenericBound::Use(..) => true,
939949
};
940950
let is_lifetime = |b: &ast::GenericBound| !is_trait(b);
941951
let last_trait_index = generic_bounds.iter().rposition(is_trait);
@@ -969,9 +979,8 @@ fn join_bounds_inner(
969979
let generic_bounds_in_order = is_generic_bounds_in_order(items);
970980
let is_bound_extendable = |s: &str, b: &ast::GenericBound| match b {
971981
ast::GenericBound::Outlives(..) => true,
972-
ast::GenericBound::Trait(..) => last_line_extendable(s),
973-
// FIXME(precise_capturing): This ordering fn should be reworked.
974-
ast::GenericBound::Use(..) => true,
982+
// We treat `use<>` like a trait bound here.
983+
ast::GenericBound::Trait(..) | ast::GenericBound::Use(..) => last_line_extendable(s),
975984
};
976985

977986
// Whether a GenericBound item is a PathSegment segment that includes internal array
@@ -993,6 +1002,7 @@ fn join_bounds_inner(
9931002
}
9941003
}
9951004
}
1005+
ast::GenericBound::Use(args, _) => args.len() > 1,
9961006
_ => false,
9971007
};
9981008

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn hello() -> impl
2+
use<'a> + Sized {}
3+
4+
fn all_three() -> impl Sized + use<'a> + 'a;
5+
6+
fn pathological() -> impl use<'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a,
7+
'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a,
8+
'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a,
9+
'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a, 'a> + Sized {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
fn hello() -> impl use<'a> + Sized {}
2+
3+
fn all_three() -> impl Sized + use<'a> + 'a;
4+
5+
fn pathological() -> impl use<
6+
'a,
7+
'a,
8+
'a,
9+
'a,
10+
'a,
11+
'a,
12+
'a,
13+
'a,
14+
'a,
15+
'a,
16+
'a,
17+
'a,
18+
'a,
19+
'a,
20+
'a,
21+
'a,
22+
'a,
23+
'a,
24+
'a,
25+
'a,
26+
'a,
27+
'a,
28+
'a,
29+
'a,
30+
'a,
31+
'a,
32+
'a,
33+
'a,
34+
'a,
35+
'a,
36+
'a,
37+
'a,
38+
'a,
39+
'a,
40+
'a,
41+
'a,
42+
'a,
43+
'a,
44+
'a,
45+
'a,
46+
'a,
47+
'a,
48+
'a,
49+
'a,
50+
'a,
51+
'a,
52+
'a,
53+
'a,
54+
> + Sized {
55+
}

0 commit comments

Comments
 (0)