Skip to content

Commit db4c4fd

Browse files
committed
Fix rustfmt for attributes in where predicates
1 parent 85cd0b8 commit db4c4fd

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

Diff for: src/tools/rustfmt/src/spanned.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,7 @@ impl Spanned for ast::FieldDef {
150150

151151
impl Spanned for ast::WherePredicate {
152152
fn span(&self) -> Span {
153-
match self.kind {
154-
ast::WherePredicateKind::BoundPredicate(ref p) => p.span,
155-
ast::WherePredicateKind::RegionPredicate(ref p) => p.span,
156-
ast::WherePredicateKind::EqPredicate(ref p) => p.span,
157-
}
153+
self.span
158154
}
159155
}
160156

Diff for: src/tools/rustfmt/src/types.rs

+34-5
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,9 @@ impl Rewrite for ast::WherePredicate {
462462
}
463463

464464
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
465-
let mut result = self.attrs.rewrite_result(context, shape)?;
466-
if !self.attrs.is_empty() {
467-
result.push(' ');
468-
}
465+
let attrs_str = self.attrs.rewrite_result(context, shape)?;
469466
// FIXME: dead spans?
470-
result += &match self.kind {
467+
let pred_str = &match self.kind {
471468
ast::WherePredicateKind::BoundPredicate(ast::WhereBoundPredicate {
472469
ref bound_generic_params,
473470
ref bounded_ty,
@@ -503,6 +500,38 @@ impl Rewrite for ast::WherePredicate {
503500
}
504501
};
505502

503+
let mut result = String::with_capacity(attrs_str.len() + pred_str.len() + 1);
504+
result.push_str(&attrs_str);
505+
let pred_start = self.kind.span().lo();
506+
let line_len = last_line_width(&attrs_str) + 1 + first_line_width(&pred_str);
507+
if let Some(last_attr) = self.attrs.last().filter(|last_attr| {
508+
contains_comment(context.snippet(mk_sp(last_attr.span.hi(), pred_start)))
509+
}) {
510+
result = combine_strs_with_missing_comments(
511+
context,
512+
&result,
513+
&pred_str,
514+
mk_sp(last_attr.span.hi(), pred_start),
515+
Shape {
516+
width: context.config.inline_attribute_width(),
517+
..shape
518+
},
519+
!last_attr.is_doc_comment(),
520+
)?;
521+
} else {
522+
if !self.attrs.is_empty() {
523+
if context.config.inline_attribute_width() < line_len
524+
|| self.attrs.len() > 1
525+
|| self.attrs.last().is_some_and(|a| a.is_doc_comment())
526+
{
527+
result.push_str(&shape.indent.to_string_with_newline(context.config));
528+
} else {
529+
result.push(' ');
530+
}
531+
}
532+
result.push_str(&pred_str);
533+
}
534+
506535
Ok(result)
507536
}
508537
}

Diff for: src/tools/rustfmt/tests/target/cfg_attribute_in_where.rs

+33-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// rustfmt-inline_attribute_width: 40
2+
13
#![crate_type = "lib"]
24
#![feature(cfg_attribute_in_where)]
35
use std::marker::PhantomData;
@@ -10,35 +12,54 @@ trait TraitB {}
1012

1113
trait A<T>
1214
where
13-
#[cfg = a] T: TraitA,
14-
#[cfg = b] T: TraitB,
15+
#[cfg = a_very_long_attribute_name]
16+
T: TraitA,
17+
#[cfg = another_very_long_attribute_name]
18+
T: TraitB,
1519
{
1620
type B<U>
1721
where
18-
#[cfg = a] U: TraitA,
19-
#[cfg = b] U: TraitB;
22+
#[cfg = a]
23+
// line comment after the attribute
24+
U: TraitA,
25+
#[cfg = b]
26+
/* block comment after the attribute */
27+
U: TraitB,
28+
#[cfg = a] // short
29+
U: TraitA,
30+
#[cfg = b] /* short */ U: TraitB;
2031

2132
fn foo<U>(&self)
2233
where
23-
#[cfg = a] U: TraitA,
24-
#[cfg = b] U: TraitB;
34+
/// line doc comment before the attribute
35+
U: TraitA,
36+
/** line doc block comment before the attribute */
37+
U: TraitB;
2538
}
2639

2740
impl<T> A<T> for T
2841
where
29-
#[cfg = a] T: TraitA,
30-
#[cfg = b] T: TraitB,
42+
#[doc = "line doc before the attribute"]
43+
T: TraitA,
44+
/** short doc */
45+
T: TraitB,
3146
{
3247
type B<U>
3348
= ()
3449
where
35-
#[cfg = a] U: TraitA,
36-
#[cfg = b] U: TraitB;
50+
#[doc = "short"] U: TraitA,
51+
#[doc = "short"]
52+
#[cfg = a]
53+
U: TraitB;
3754

3855
fn foo<U>(&self)
3956
where
40-
#[cfg = a] U: TraitA,
41-
#[cfg = b] U: TraitB,
57+
#[cfg = a]
58+
#[cfg = b]
59+
U: TraitA,
60+
/// line doc
61+
#[cfg = c]
62+
U: TraitB,
4263
{
4364
}
4465
}

0 commit comments

Comments
 (0)