Skip to content

Commit 52dd81e

Browse files
Implement use<> formatting in rustfmt
1 parent 1aaab8b commit 52dd81e

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
@@ -76,6 +76,7 @@ pub(crate) enum OverflowableItem<'a> {
7676
TuplePatField(&'a TuplePatField<'a>),
7777
Ty(&'a ast::Ty),
7878
Pat(&'a ast::Pat),
79+
PreciseCapturingArg(&'a ast::PreciseCapturingArg),
7980
}
8081

8182
impl<'a> Rewrite for OverflowableItem<'a> {
@@ -116,6 +117,7 @@ impl<'a> OverflowableItem<'a> {
116117
OverflowableItem::TuplePatField(pat) => f(*pat),
117118
OverflowableItem::Ty(ty) => f(*ty),
118119
OverflowableItem::Pat(pat) => f(*pat),
120+
OverflowableItem::PreciseCapturingArg(arg) => f(*arg),
119121
}
120122
}
121123

@@ -130,6 +132,9 @@ impl<'a> OverflowableItem<'a> {
130132
matches!(meta_item.kind, ast::MetaItemKind::Word)
131133
}
132134
},
135+
// FIXME: Why don't we consider `SegmentParam` to be simple?
136+
// FIXME: If we also fix `SegmentParam`, then we should apply the same
137+
// heuristic to `PreciseCapturingArg`.
133138
_ => false,
134139
}
135140
}
@@ -232,7 +237,15 @@ macro_rules! impl_into_overflowable_item_for_rustfmt_types {
232237
}
233238
}
234239

235-
impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, FieldDef, Ty, Pat);
240+
impl_into_overflowable_item_for_ast_node!(
241+
Expr,
242+
GenericParam,
243+
NestedMetaItem,
244+
FieldDef,
245+
Ty,
246+
Pat,
247+
PreciseCapturingArg
248+
);
236249
impl_into_overflowable_item_for_rustfmt_types!([MacroArg], [SegmentParam, TuplePatField]);
237250

238251
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
@@ -175,6 +175,17 @@ impl<'a> Rewrite for SegmentParam<'a> {
175175
}
176176
}
177177

178+
impl Rewrite for ast::PreciseCapturingArg {
179+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
180+
match self {
181+
ast::PreciseCapturingArg::Lifetime(lt) => lt.rewrite(context, shape),
182+
ast::PreciseCapturingArg::Arg(p, _) => {
183+
rewrite_path(context, PathContext::Type, &None, p, shape)
184+
}
185+
}
186+
}
187+
}
188+
178189
impl Rewrite for ast::AssocItemConstraint {
179190
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
180191
use ast::AssocItemConstraintKind::{Bound, Equality};
@@ -562,9 +573,10 @@ impl Rewrite for ast::GenericBound {
562573
.map(|s| format!("{constness}{asyncness}{polarity}{s}"))
563574
.map(|s| if has_paren { format!("({})", s) } else { s })
564575
}
576+
ast::GenericBound::Use(ref args, span) => {
577+
overflow::rewrite_with_angle_brackets(context, "use", args.iter(), shape, span)
578+
}
565579
ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),
566-
// FIXME(precise_capturing): Should implement formatting before stabilization.
567-
ast::GenericBound::Use(..) => None,
568580
}
569581
}
570582
}
@@ -929,9 +941,7 @@ fn rewrite_bare_fn(
929941
fn is_generic_bounds_in_order(generic_bounds: &[ast::GenericBound]) -> bool {
930942
let is_trait = |b: &ast::GenericBound| match b {
931943
ast::GenericBound::Outlives(..) => false,
932-
ast::GenericBound::Trait(..) => true,
933-
// FIXME(precise_capturing): This ordering fn should be reworked.
934-
ast::GenericBound::Use(..) => false,
944+
ast::GenericBound::Trait(..) | ast::GenericBound::Use(..) => true,
935945
};
936946
let is_lifetime = |b: &ast::GenericBound| !is_trait(b);
937947
let last_trait_index = generic_bounds.iter().rposition(is_trait);
@@ -965,9 +975,8 @@ fn join_bounds_inner(
965975
let generic_bounds_in_order = is_generic_bounds_in_order(items);
966976
let is_bound_extendable = |s: &str, b: &ast::GenericBound| match b {
967977
ast::GenericBound::Outlives(..) => true,
968-
ast::GenericBound::Trait(..) => last_line_extendable(s),
969-
// FIXME(precise_capturing): This ordering fn should be reworked.
970-
ast::GenericBound::Use(..) => true,
978+
// We treat `use<>` like a trait bound here.
979+
ast::GenericBound::Trait(..) | ast::GenericBound::Use(..) => last_line_extendable(s),
971980
};
972981

973982
// Whether a GenericBound item is a PathSegment segment that includes internal array
@@ -989,6 +998,7 @@ fn join_bounds_inner(
989998
}
990999
}
9911000
}
1001+
ast::GenericBound::Use(args, _) => args.len() > 1,
9921002
_ => false,
9931003
};
9941004

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)