Skip to content

Commit 8d0d4ce

Browse files
committed
AST: Refactor type alias where clauses
1 parent 6122397 commit 8d0d4ce

File tree

8 files changed

+73
-85
lines changed

8 files changed

+73
-85
lines changed

compiler/rustc_ast/src/ast.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,10 @@ impl Default for Generics {
403403
/// A where-clause in a definition.
404404
#[derive(Clone, Encodable, Decodable, Debug)]
405405
pub struct WhereClause {
406-
/// `true` if we ate a `where` token: this can happen
407-
/// if we parsed no predicates (e.g. `struct Foo where {}`).
408-
/// This allows us to pretty-print accurately.
406+
/// `true` if we ate a `where` token.
407+
///
408+
/// This can happen if we parsed no predicates, e.g., `struct Foo where {}`.
409+
/// This allows us to pretty-print accurately and provide correct suggestion diagnostics.
409410
pub has_where_token: bool,
410411
pub predicates: ThinVec<WherePredicate>,
411412
pub span: Span,
@@ -3017,18 +3018,29 @@ pub struct Trait {
30173018
///
30183019
/// If there is no where clause, then this is `false` with `DUMMY_SP`.
30193020
#[derive(Copy, Clone, Encodable, Decodable, Debug, Default)]
3020-
pub struct TyAliasWhereClause(pub bool, pub Span);
3021+
pub struct TyAliasWhereClause {
3022+
pub has_where_token: bool,
3023+
pub span: Span,
3024+
}
3025+
3026+
/// The span information for the two where clauses on a `TyAlias`.
3027+
#[derive(Copy, Clone, Encodable, Decodable, Debug, Default)]
3028+
pub struct TyAliasWhereClauses {
3029+
/// Before the equals sign.
3030+
pub before: TyAliasWhereClause,
3031+
/// After the equals sign.
3032+
pub after: TyAliasWhereClause,
3033+
/// The index in `TyAlias.generics.where_clause.predicates` that would split
3034+
/// into predicates from the where clause before the equals sign and the ones
3035+
/// from the where clause after the equals sign.
3036+
pub split: usize,
3037+
}
30213038

30223039
#[derive(Clone, Encodable, Decodable, Debug)]
30233040
pub struct TyAlias {
30243041
pub defaultness: Defaultness,
30253042
pub generics: Generics,
3026-
/// The span information for the two where clauses (before equals, after equals)
3027-
pub where_clauses: (TyAliasWhereClause, TyAliasWhereClause),
3028-
/// The index in `generics.where_clause.predicates` that would split into
3029-
/// predicates from the where clause before the equals and the predicates
3030-
/// from the where clause after the equals
3031-
pub where_predicates_split: usize,
3043+
pub where_clauses: TyAliasWhereClauses,
30323044
pub bounds: GenericBounds,
30333045
pub ty: Option<P<Ty>>,
30343046
}

compiler/rustc_ast/src/mut_visit.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1079,8 +1079,8 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
10791079
}) => {
10801080
visit_defaultness(defaultness, vis);
10811081
vis.visit_generics(generics);
1082-
vis.visit_span(&mut where_clauses.0.1);
1083-
vis.visit_span(&mut where_clauses.1.1);
1082+
vis.visit_span(&mut where_clauses.before.span);
1083+
vis.visit_span(&mut where_clauses.after.span);
10841084
visit_bounds(bounds, vis);
10851085
visit_opt(ty, |ty| vis.visit_ty(ty));
10861086
}
@@ -1163,8 +1163,8 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11631163
}) => {
11641164
visit_defaultness(defaultness, visitor);
11651165
visitor.visit_generics(generics);
1166-
visitor.visit_span(&mut where_clauses.0.1);
1167-
visitor.visit_span(&mut where_clauses.1.1);
1166+
visitor.visit_span(&mut where_clauses.before.span);
1167+
visitor.visit_span(&mut where_clauses.after.span);
11681168
visit_bounds(bounds, visitor);
11691169
visit_opt(ty, |ty| visitor.visit_ty(ty));
11701170
}
@@ -1257,8 +1257,8 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
12571257
}) => {
12581258
visit_defaultness(defaultness, visitor);
12591259
visitor.visit_generics(generics);
1260-
visitor.visit_span(&mut where_clauses.0.1);
1261-
visitor.visit_span(&mut where_clauses.1.1);
1260+
visitor.visit_span(&mut where_clauses.before.span);
1261+
visitor.visit_span(&mut where_clauses.after.span);
12621262
visit_bounds(bounds, visitor);
12631263
visit_opt(ty, |ty| visitor.visit_ty(ty));
12641264
}

compiler/rustc_ast_lowering/src/item.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,20 @@ pub(super) struct ItemLowerer<'a, 'hir> {
3333
/// clause if it exists.
3434
fn add_ty_alias_where_clause(
3535
generics: &mut ast::Generics,
36-
mut where_clauses: (TyAliasWhereClause, TyAliasWhereClause),
36+
mut where_clauses: TyAliasWhereClauses,
3737
prefer_first: bool,
3838
) {
3939
if !prefer_first {
40-
where_clauses = (where_clauses.1, where_clauses.0);
41-
}
42-
if where_clauses.0.0 || !where_clauses.1.0 {
43-
generics.where_clause.has_where_token = where_clauses.0.0;
44-
generics.where_clause.span = where_clauses.0.1;
45-
} else {
46-
generics.where_clause.has_where_token = where_clauses.1.0;
47-
generics.where_clause.span = where_clauses.1.1;
40+
(where_clauses.before, where_clauses.after) = (where_clauses.after, where_clauses.before);
4841
}
42+
let where_clause =
43+
if where_clauses.before.has_where_token || !where_clauses.after.has_where_token {
44+
where_clauses.before
45+
} else {
46+
where_clauses.after
47+
};
48+
generics.where_clause.has_where_token = where_clause.has_where_token;
49+
generics.where_clause.span = where_clause.span;
4950
}
5051

5152
impl<'a, 'hir> ItemLowerer<'a, 'hir> {

compiler/rustc_ast_passes/src/ast_validation.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ impl<'a> AstValidator<'a> {
139139
ty_alias: &TyAlias,
140140
) -> Result<(), errors::WhereClauseBeforeTypeAlias> {
141141
let before_predicates =
142-
ty_alias.generics.where_clause.predicates.split_at(ty_alias.where_predicates_split).0;
142+
ty_alias.generics.where_clause.predicates.split_at(ty_alias.where_clauses.split).0;
143143

144144
if ty_alias.ty.is_none() || before_predicates.is_empty() {
145145
return Ok(());
146146
}
147147

148148
let mut state = State::new();
149-
if !ty_alias.where_clauses.1.0 {
149+
if !ty_alias.where_clauses.after.has_where_token {
150150
state.space();
151151
state.word_space("where");
152152
} else {
@@ -161,13 +161,13 @@ impl<'a> AstValidator<'a> {
161161
state.print_where_predicate(p);
162162
}
163163

164-
let span = ty_alias.where_clauses.0.1;
164+
let span = ty_alias.where_clauses.before.span;
165165
Err(errors::WhereClauseBeforeTypeAlias {
166166
span,
167167
sugg: errors::WhereClauseBeforeTypeAliasSugg {
168168
left: span,
169169
snippet: state.s.eof(),
170-
right: ty_alias.where_clauses.1.1.shrink_to_hi(),
170+
right: ty_alias.where_clauses.after.span.shrink_to_hi(),
171171
},
172172
})
173173
}
@@ -457,8 +457,7 @@ impl<'a> AstValidator<'a> {
457457
fn check_foreign_ty_genericless(
458458
&self,
459459
generics: &Generics,
460-
before_where_clause: &TyAliasWhereClause,
461-
after_where_clause: &TyAliasWhereClause,
460+
where_clauses: &TyAliasWhereClauses,
462461
) {
463462
let cannot_have = |span, descr, remove_descr| {
464463
self.dcx().emit_err(errors::ExternTypesCannotHave {
@@ -473,14 +472,14 @@ impl<'a> AstValidator<'a> {
473472
cannot_have(generics.span, "generic parameters", "generic parameters");
474473
}
475474

476-
let check_where_clause = |where_clause: &TyAliasWhereClause| {
477-
if let TyAliasWhereClause(true, where_clause_span) = where_clause {
478-
cannot_have(*where_clause_span, "`where` clauses", "`where` clause");
475+
let check_where_clause = |where_clause: TyAliasWhereClause| {
476+
if where_clause.has_where_token {
477+
cannot_have(where_clause.span, "`where` clauses", "`where` clause");
479478
}
480479
};
481480

482-
check_where_clause(before_where_clause);
483-
check_where_clause(after_where_clause);
481+
check_where_clause(where_clauses.before);
482+
check_where_clause(where_clauses.after);
484483
}
485484

486485
fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) {
@@ -1121,9 +1120,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11211120
if let Err(err) = self.check_type_alias_where_clause_location(ty_alias) {
11221121
self.dcx().emit_err(err);
11231122
}
1124-
} else if where_clauses.1.0 {
1123+
} else if where_clauses.after.has_where_token {
11251124
self.dcx().emit_err(errors::WhereClauseAfterTypeAlias {
1126-
span: where_clauses.1.1,
1125+
span: where_clauses.after.span,
11271126
help: self.session.is_nightly_build().then_some(()),
11281127
});
11291128
}
@@ -1153,7 +1152,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11531152
self.check_defaultness(fi.span, *defaultness);
11541153
self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span));
11551154
self.check_type_no_bounds(bounds, "`extern` blocks");
1156-
self.check_foreign_ty_genericless(generics, &where_clauses.0, &where_clauses.1);
1155+
self.check_foreign_ty_genericless(generics, where_clauses);
11571156
self.check_foreign_item_ascii_only(fi.ident);
11581157
}
11591158
ForeignItemKind::Static(_, _, body) => {

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ impl<'a> State<'a> {
4343
defaultness,
4444
generics,
4545
where_clauses,
46-
where_predicates_split,
4746
bounds,
4847
ty,
4948
}) => {
5049
self.print_associated_type(
5150
ident,
5251
generics,
5352
*where_clauses,
54-
*where_predicates_split,
5553
bounds,
5654
ty.as_deref(),
5755
vis,
@@ -108,15 +106,14 @@ impl<'a> State<'a> {
108106
&mut self,
109107
ident: Ident,
110108
generics: &ast::Generics,
111-
where_clauses: (ast::TyAliasWhereClause, ast::TyAliasWhereClause),
112-
where_predicates_split: usize,
109+
where_clauses: ast::TyAliasWhereClauses,
113110
bounds: &ast::GenericBounds,
114111
ty: Option<&ast::Ty>,
115112
vis: &ast::Visibility,
116113
defaultness: ast::Defaultness,
117114
) {
118115
let (before_predicates, after_predicates) =
119-
generics.where_clause.predicates.split_at(where_predicates_split);
116+
generics.where_clause.predicates.split_at(where_clauses.split);
120117
self.head("");
121118
self.print_visibility(vis);
122119
self.print_defaultness(defaultness);
@@ -127,13 +124,13 @@ impl<'a> State<'a> {
127124
self.word_nbsp(":");
128125
self.print_type_bounds(bounds);
129126
}
130-
self.print_where_clause_parts(where_clauses.0.0, before_predicates);
127+
self.print_where_clause_parts(where_clauses.before.has_where_token, before_predicates);
131128
if let Some(ty) = ty {
132129
self.space();
133130
self.word_space("=");
134131
self.print_type(ty);
135132
}
136-
self.print_where_clause_parts(where_clauses.1.0, after_predicates);
133+
self.print_where_clause_parts(where_clauses.after.has_where_token, after_predicates);
137134
self.word(";");
138135
self.end(); // end inner head-block
139136
self.end(); // end outer head-block
@@ -249,15 +246,13 @@ impl<'a> State<'a> {
249246
defaultness,
250247
generics,
251248
where_clauses,
252-
where_predicates_split,
253249
bounds,
254250
ty,
255251
}) => {
256252
self.print_associated_type(
257253
item.ident,
258254
generics,
259255
*where_clauses,
260-
*where_predicates_split,
261256
bounds,
262257
ty.as_deref(),
263258
&item.vis,
@@ -536,15 +531,13 @@ impl<'a> State<'a> {
536531
defaultness,
537532
generics,
538533
where_clauses,
539-
where_predicates_split,
540534
bounds,
541535
ty,
542536
}) => {
543537
self.print_associated_type(
544538
ident,
545539
generics,
546540
*where_clauses,
547-
*where_predicates_split,
548541
bounds,
549542
ty.as_deref(),
550543
vis,

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,7 @@ impl<'a> TraitDef<'a> {
595595
kind: ast::AssocItemKind::Type(Box::new(ast::TyAlias {
596596
defaultness: ast::Defaultness::Final,
597597
generics: Generics::default(),
598-
where_clauses: (
599-
ast::TyAliasWhereClause::default(),
600-
ast::TyAliasWhereClause::default(),
601-
),
602-
where_predicates_split: 0,
598+
where_clauses: ast::TyAliasWhereClauses::default(),
603599
bounds: Vec::new(),
604600
ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)),
605601
})),

compiler/rustc_parse/src/parser/item.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,17 @@ impl<'a> Parser<'a> {
970970

971971
let after_where_clause = self.parse_where_clause()?;
972972

973-
let where_clauses = (
974-
TyAliasWhereClause(before_where_clause.has_where_token, before_where_clause.span),
975-
TyAliasWhereClause(after_where_clause.has_where_token, after_where_clause.span),
976-
);
977-
let where_predicates_split = before_where_clause.predicates.len();
973+
let where_clauses = TyAliasWhereClauses {
974+
before: TyAliasWhereClause {
975+
has_where_token: before_where_clause.has_where_token,
976+
span: before_where_clause.span,
977+
},
978+
after: TyAliasWhereClause {
979+
has_where_token: after_where_clause.has_where_token,
980+
span: after_where_clause.span,
981+
},
982+
split: before_where_clause.predicates.len(),
983+
};
978984
let mut predicates = before_where_clause.predicates;
979985
predicates.extend(after_where_clause.predicates);
980986
let where_clause = WhereClause {
@@ -993,7 +999,6 @@ impl<'a> Parser<'a> {
993999
defaultness,
9941000
generics,
9951001
where_clauses,
996-
where_predicates_split,
9971002
bounds,
9981003
ty,
9991004
})),

src/tools/rustfmt/src/items.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -1651,8 +1651,7 @@ struct TyAliasRewriteInfo<'c, 'g>(
16511651
&'c RewriteContext<'c>,
16521652
Indent,
16531653
&'g ast::Generics,
1654-
(ast::TyAliasWhereClause, ast::TyAliasWhereClause),
1655-
usize,
1654+
ast::TyAliasWhereClauses,
16561655
symbol::Ident,
16571656
Span,
16581657
);
@@ -1672,23 +1671,14 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
16721671
ref bounds,
16731672
ref ty,
16741673
where_clauses,
1675-
where_predicates_split,
16761674
} = *ty_alias_kind;
16771675
let ty_opt = ty.as_ref();
16781676
let (ident, vis) = match visitor_kind {
16791677
Item(i) => (i.ident, &i.vis),
16801678
AssocTraitItem(i) | AssocImplItem(i) => (i.ident, &i.vis),
16811679
ForeignItem(i) => (i.ident, &i.vis),
16821680
};
1683-
let rw_info = &TyAliasRewriteInfo(
1684-
context,
1685-
indent,
1686-
generics,
1687-
where_clauses,
1688-
where_predicates_split,
1689-
ident,
1690-
span,
1691-
);
1681+
let rw_info = &TyAliasRewriteInfo(context, indent, generics, where_clauses, ident, span);
16921682
let op_ty = opaque_ty(ty);
16931683
// Type Aliases are formatted slightly differently depending on the context
16941684
// in which they appear, whether they are opaque, and whether they are associated.
@@ -1724,19 +1714,11 @@ fn rewrite_ty<R: Rewrite>(
17241714
vis: &ast::Visibility,
17251715
) -> Option<String> {
17261716
let mut result = String::with_capacity(128);
1727-
let TyAliasRewriteInfo(
1728-
context,
1729-
indent,
1730-
generics,
1731-
where_clauses,
1732-
where_predicates_split,
1733-
ident,
1734-
span,
1735-
) = *rw_info;
1717+
let TyAliasRewriteInfo(context, indent, generics, where_clauses, ident, span) = *rw_info;
17361718
let (before_where_predicates, after_where_predicates) = generics
17371719
.where_clause
17381720
.predicates
1739-
.split_at(where_predicates_split);
1721+
.split_at(where_clauses.split);
17401722
if !after_where_predicates.is_empty() {
17411723
return None;
17421724
}
@@ -1771,7 +1753,7 @@ fn rewrite_ty<R: Rewrite>(
17711753
let where_clause_str = rewrite_where_clause(
17721754
context,
17731755
before_where_predicates,
1774-
where_clauses.0.1,
1756+
where_clauses.before.span,
17751757
context.config.brace_style(),
17761758
Shape::legacy(where_budget, indent),
17771759
false,
@@ -1795,7 +1777,7 @@ fn rewrite_ty<R: Rewrite>(
17951777
let comment_span = context
17961778
.snippet_provider
17971779
.opt_span_before(span, "=")
1798-
.map(|op_lo| mk_sp(where_clauses.0.1.hi(), op_lo));
1780+
.map(|op_lo| mk_sp(where_clauses.before.span.hi(), op_lo));
17991781

18001782
let lhs = match comment_span {
18011783
Some(comment_span)

0 commit comments

Comments
 (0)