Skip to content

Commit 6b1c0bc

Browse files
compiler-errorsytmimi
authored andcommitted
Format trailing where clauses in rustfmt
1 parent cedb7b5 commit 6b1c0bc

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed

src/items.rs

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,9 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
16801680
where_predicates_split,
16811681
} = *ty_alias_kind;
16821682
let ty_opt = ty.as_ref();
1683+
let rhs_hi = ty
1684+
.as_ref()
1685+
.map_or(where_clauses.0.1.hi(), |ty| ty.span.hi());
16831686
let (ident, vis) = match visitor_kind {
16841687
Item(i) => (i.ident, &i.vis),
16851688
AssocTraitItem(i) | AssocImplItem(i) => (i.ident, &i.vis),
@@ -1702,17 +1705,23 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
17021705
match (visitor_kind, &op_ty) {
17031706
(Item(_) | AssocTraitItem(_) | ForeignItem(_), Some(op_bounds)) => {
17041707
let op = OpaqueType { bounds: op_bounds };
1705-
rewrite_ty(rw_info, Some(bounds), Some(&op), vis)
1708+
rewrite_ty(rw_info, Some(bounds), Some(&op), rhs_hi, vis)
17061709
}
17071710
(Item(_) | AssocTraitItem(_) | ForeignItem(_), None) => {
1708-
rewrite_ty(rw_info, Some(bounds), ty_opt, vis)
1711+
rewrite_ty(rw_info, Some(bounds), ty_opt, rhs_hi, vis)
17091712
}
17101713
(AssocImplItem(_), _) => {
17111714
let result = if let Some(op_bounds) = op_ty {
17121715
let op = OpaqueType { bounds: op_bounds };
1713-
rewrite_ty(rw_info, Some(bounds), Some(&op), &DEFAULT_VISIBILITY)
1716+
rewrite_ty(
1717+
rw_info,
1718+
Some(bounds),
1719+
Some(&op),
1720+
rhs_hi,
1721+
&DEFAULT_VISIBILITY,
1722+
)
17141723
} else {
1715-
rewrite_ty(rw_info, Some(bounds), ty_opt, vis)
1724+
rewrite_ty(rw_info, Some(bounds), ty_opt, rhs_hi, vis)
17161725
}?;
17171726
match defaultness {
17181727
ast::Defaultness::Default(..) => Some(format!("default {result}")),
@@ -1726,6 +1735,8 @@ fn rewrite_ty<R: Rewrite>(
17261735
rw_info: &TyAliasRewriteInfo<'_, '_>,
17271736
generic_bounds_opt: Option<&ast::GenericBounds>,
17281737
rhs: Option<&R>,
1738+
// the span of the end of the RHS (or the end of the generics, if there is no RHS)
1739+
rhs_hi: BytePos,
17291740
vis: &ast::Visibility,
17301741
) -> Option<String> {
17311742
let mut result = String::with_capacity(128);
@@ -1742,9 +1753,6 @@ fn rewrite_ty<R: Rewrite>(
17421753
.where_clause
17431754
.predicates
17441755
.split_at(where_predicates_split);
1745-
if !after_where_predicates.is_empty() {
1746-
return None;
1747-
}
17481756
result.push_str(&format!("{}type ", format_visibility(context, vis)));
17491757
let ident_str = rewrite_ident(context, ident);
17501758

@@ -1773,7 +1781,7 @@ fn rewrite_ty<R: Rewrite>(
17731781
if rhs.is_none() {
17741782
option.suppress_comma();
17751783
}
1776-
let where_clause_str = rewrite_where_clause(
1784+
let before_where_clause_str = rewrite_where_clause(
17771785
context,
17781786
before_where_predicates,
17791787
where_clauses.0.1,
@@ -1785,14 +1793,20 @@ fn rewrite_ty<R: Rewrite>(
17851793
generics.span.hi(),
17861794
option,
17871795
)?;
1788-
result.push_str(&where_clause_str);
1796+
result.push_str(&before_where_clause_str);
17891797

1790-
if let Some(ty) = rhs {
1791-
// If there's a where clause, add a newline before the assignment. Otherwise just add a
1792-
// space.
1793-
let has_where = !before_where_predicates.is_empty();
1794-
if has_where {
1798+
let mut result = if let Some(ty) = rhs {
1799+
// If there are any where clauses, add a newline before the assignment.
1800+
// If there is a before where clause, do not indent, but if there is
1801+
// only an after where clause, additionally indent the type.
1802+
if !before_where_predicates.is_empty() {
17951803
result.push_str(&indent.to_string_with_newline(context.config));
1804+
} else if !after_where_predicates.is_empty() {
1805+
result.push_str(
1806+
&indent
1807+
.block_indent(context.config)
1808+
.to_string_with_newline(context.config),
1809+
);
17961810
} else {
17971811
result.push(' ');
17981812
}
@@ -1806,7 +1820,7 @@ fn rewrite_ty<R: Rewrite>(
18061820
Some(comment_span)
18071821
if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) =>
18081822
{
1809-
let comment_shape = if has_where {
1823+
let comment_shape = if !before_where_predicates.is_empty() {
18101824
Shape::indented(indent, context.config)
18111825
} else {
18121826
Shape::indented(indent, context.config)
@@ -1825,12 +1839,36 @@ fn rewrite_ty<R: Rewrite>(
18251839
_ => format!("{result}="),
18261840
};
18271841

1828-
// 1 = `;`
1829-
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1830-
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape).map(|s| s + ";")
1842+
// 1 = `;` unless there's a trailing where clause
1843+
let shape = if after_where_predicates.is_empty() {
1844+
Shape::indented(indent, context.config).sub_width(1)?
1845+
} else {
1846+
Shape::indented(indent, context.config)
1847+
};
1848+
rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape)?
18311849
} else {
1832-
Some(format!("{result};"))
1850+
result
1851+
};
1852+
1853+
if !after_where_predicates.is_empty() {
1854+
let option = WhereClauseOption::new(true, WhereClauseSpace::Newline);
1855+
let after_where_clause_str = rewrite_where_clause(
1856+
context,
1857+
after_where_predicates,
1858+
where_clauses.1.1,
1859+
context.config.brace_style(),
1860+
Shape::indented(indent, context.config),
1861+
false,
1862+
";",
1863+
None,
1864+
rhs_hi,
1865+
option,
1866+
)?;
1867+
result.push_str(&after_where_clause_str);
18331868
}
1869+
1870+
result += ";";
1871+
Some(result)
18341872
}
18351873

18361874
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type Foo
2+
where
3+
A: B,
4+
C: D,
5+
= E;
6+
7+
type Foo
8+
where
9+
A: B,
10+
C: D,
11+
= E
12+
where
13+
F: G,
14+
H: I;
15+
16+
type Foo
17+
= E
18+
where
19+
F: G,
20+
H: I;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type Foo
2+
where
3+
A: B,
4+
C: D,
5+
= E;
6+
7+
type Foo
8+
where
9+
A: B,
10+
C: D,
11+
= E
12+
where
13+
F: G,
14+
H: I;
15+
16+
type Foo
17+
= E
18+
where
19+
F: G,
20+
H: I;

0 commit comments

Comments
 (0)