Skip to content

Commit 469357e

Browse files
committed
Auto merge of #148014 - jhpratt:rollup-aglren3, r=jhpratt
Rollup of 3 pull requests Successful merges: - #134316 (Add `String::replace_first` and `String::replace_last`) - #147713 (Retire ast::TyAliasWhereClauses.) - #148011 (Revert constification of `AsRef for Cow` due to inference failure ) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4b3ba58 + 5d55418 commit 469357e

File tree

17 files changed

+273
-162
lines changed

17 files changed

+273
-162
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3636,49 +3636,26 @@ pub struct Trait {
36363636
pub items: ThinVec<Box<AssocItem>>,
36373637
}
36383638

3639-
/// The location of a where clause on a `TyAlias` (`Span`) and whether there was
3640-
/// a `where` keyword (`bool`). This is split out from `WhereClause`, since there
3641-
/// are two locations for where clause on type aliases, but their predicates
3642-
/// are concatenated together.
3643-
///
3644-
/// Take this example:
3645-
/// ```ignore (only-for-syntax-highlight)
3646-
/// trait Foo {
3647-
/// type Assoc<'a, 'b> where Self: 'a, Self: 'b;
3648-
/// }
3649-
/// impl Foo for () {
3650-
/// type Assoc<'a, 'b> where Self: 'a = () where Self: 'b;
3651-
/// // ^^^^^^^^^^^^^^ first where clause
3652-
/// // ^^^^^^^^^^^^^^ second where clause
3653-
/// }
3654-
/// ```
3655-
///
3656-
/// If there is no where clause, then this is `false` with `DUMMY_SP`.
3657-
#[derive(Copy, Clone, Encodable, Decodable, Debug, Default, Walkable)]
3658-
pub struct TyAliasWhereClause {
3659-
pub has_where_token: bool,
3660-
pub span: Span,
3661-
}
3662-
3663-
/// The span information for the two where clauses on a `TyAlias`.
3664-
#[derive(Copy, Clone, Encodable, Decodable, Debug, Default, Walkable)]
3665-
pub struct TyAliasWhereClauses {
3666-
/// Before the equals sign.
3667-
pub before: TyAliasWhereClause,
3668-
/// After the equals sign.
3669-
pub after: TyAliasWhereClause,
3670-
/// The index in `TyAlias.generics.where_clause.predicates` that would split
3671-
/// into predicates from the where clause before the equals sign and the ones
3672-
/// from the where clause after the equals sign.
3673-
pub split: usize,
3674-
}
3675-
36763639
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
36773640
pub struct TyAlias {
36783641
pub defaultness: Defaultness,
36793642
pub ident: Ident,
36803643
pub generics: Generics,
3681-
pub where_clauses: TyAliasWhereClauses,
3644+
/// There are two locations for where clause on type aliases. This represents the second
3645+
/// where clause, before the semicolon. The first where clause is stored inside `generics`.
3646+
///
3647+
/// Take this example:
3648+
/// ```ignore (only-for-syntax-highlight)
3649+
/// trait Foo {
3650+
/// type Assoc<'a, 'b> where Self: 'a, Self: 'b;
3651+
/// }
3652+
/// impl Foo for () {
3653+
/// type Assoc<'a, 'b> where Self: 'a = () where Self: 'b;
3654+
/// // ^^^^^^^^^^^^^^ before where clause
3655+
/// // ^^^^^^^^^^^^^^ after where clause
3656+
/// }
3657+
/// ```
3658+
pub after_where_clause: WhereClause,
36823659
#[visitable(extra = BoundKind::Bound)]
36833660
pub bounds: GenericBounds,
36843661
pub ty: Option<Box<Ty>>,

compiler/rustc_ast/src/visit.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,6 @@ macro_rules! common_visitor_and_walkers {
471471
TraitBoundModifiers,
472472
TraitObjectSyntax,
473473
TyAlias,
474-
TyAliasWhereClause,
475-
TyAliasWhereClauses,
476474
TyKind,
477475
TyPatKind,
478476
UnOp,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,18 @@ pub(super) struct ItemLowerer<'a, 'hir> {
3636
/// clause if it exists.
3737
fn add_ty_alias_where_clause(
3838
generics: &mut ast::Generics,
39-
mut where_clauses: TyAliasWhereClauses,
39+
after_where_clause: &ast::WhereClause,
4040
prefer_first: bool,
4141
) {
42+
generics.where_clause.predicates.extend_from_slice(&after_where_clause.predicates);
43+
44+
let mut before = (generics.where_clause.has_where_token, generics.where_clause.span);
45+
let mut after = (after_where_clause.has_where_token, after_where_clause.span);
4246
if !prefer_first {
43-
(where_clauses.before, where_clauses.after) = (where_clauses.after, where_clauses.before);
47+
(before, after) = (after, before);
4448
}
45-
let where_clause =
46-
if where_clauses.before.has_where_token || !where_clauses.after.has_where_token {
47-
where_clauses.before
48-
} else {
49-
where_clauses.after
50-
};
51-
generics.where_clause.has_where_token = where_clause.has_where_token;
52-
generics.where_clause.span = where_clause.span;
49+
(generics.where_clause.has_where_token, generics.where_clause.span) =
50+
if before.0 || !after.0 { before } else { after };
5351
}
5452

5553
impl<'a, 'hir> ItemLowerer<'a, 'hir> {
@@ -271,7 +269,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
271269
self.lower_body(|this| (&[], this.expr(span, hir::ExprKind::InlineAsm(asm))));
272270
hir::ItemKind::GlobalAsm { asm, fake_body }
273271
}
274-
ItemKind::TyAlias(box TyAlias { ident, generics, where_clauses, ty, .. }) => {
272+
ItemKind::TyAlias(box TyAlias { ident, generics, after_where_clause, ty, .. }) => {
275273
// We lower
276274
//
277275
// type Foo = impl Trait
@@ -282,7 +280,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
282280
// opaque type Foo1: Trait
283281
let ident = self.lower_ident(*ident);
284282
let mut generics = generics.clone();
285-
add_ty_alias_where_clause(&mut generics, *where_clauses, true);
283+
add_ty_alias_where_clause(&mut generics, after_where_clause, true);
286284
let (generics, ty) = self.lower_generics(
287285
&generics,
288286
id,
@@ -901,10 +899,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
901899
)
902900
}
903901
AssocItemKind::Type(box TyAlias {
904-
ident, generics, where_clauses, bounds, ty, ..
902+
ident,
903+
generics,
904+
after_where_clause,
905+
bounds,
906+
ty,
907+
..
905908
}) => {
906909
let mut generics = generics.clone();
907-
add_ty_alias_where_clause(&mut generics, *where_clauses, false);
910+
add_ty_alias_where_clause(&mut generics, after_where_clause, false);
908911
let (generics, kind) = self.lower_generics(
909912
&generics,
910913
i.id,
@@ -1070,9 +1073,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
10701073

10711074
(*ident, (generics, hir::ImplItemKind::Fn(sig, body_id)))
10721075
}
1073-
AssocItemKind::Type(box TyAlias { ident, generics, where_clauses, ty, .. }) => {
1076+
AssocItemKind::Type(box TyAlias {
1077+
ident, generics, after_where_clause, ty, ..
1078+
}) => {
10741079
let mut generics = generics.clone();
1075-
add_ty_alias_where_clause(&mut generics, *where_clauses, false);
1080+
add_ty_alias_where_clause(&mut generics, after_where_clause, false);
10761081
(
10771082
*ident,
10781083
self.lower_generics(

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,24 @@ impl<'a> AstValidator<'a> {
145145
&mut self,
146146
ty_alias: &TyAlias,
147147
) -> Result<(), errors::WhereClauseBeforeTypeAlias> {
148-
if ty_alias.ty.is_none() || !ty_alias.where_clauses.before.has_where_token {
148+
if ty_alias.ty.is_none() || !ty_alias.generics.where_clause.has_where_token {
149149
return Ok(());
150150
}
151151

152-
let (before_predicates, after_predicates) =
153-
ty_alias.generics.where_clause.predicates.split_at(ty_alias.where_clauses.split);
154-
let span = ty_alias.where_clauses.before.span;
152+
let span = ty_alias.generics.where_clause.span;
155153

156-
let sugg = if !before_predicates.is_empty() || !ty_alias.where_clauses.after.has_where_token
154+
let sugg = if !ty_alias.generics.where_clause.predicates.is_empty()
155+
|| !ty_alias.after_where_clause.has_where_token
157156
{
158157
let mut state = State::new();
159158

160-
if !ty_alias.where_clauses.after.has_where_token {
159+
if !ty_alias.after_where_clause.has_where_token {
161160
state.space();
162161
state.word_space("where");
163162
}
164163

165-
let mut first = after_predicates.is_empty();
166-
for p in before_predicates {
164+
let mut first = ty_alias.after_where_clause.predicates.is_empty();
165+
for p in &ty_alias.generics.where_clause.predicates {
167166
if !first {
168167
state.word_space(",");
169168
}
@@ -174,7 +173,7 @@ impl<'a> AstValidator<'a> {
174173
errors::WhereClauseBeforeTypeAliasSugg::Move {
175174
left: span,
176175
snippet: state.s.eof(),
177-
right: ty_alias.where_clauses.after.span.shrink_to_hi(),
176+
right: ty_alias.after_where_clause.span.shrink_to_hi(),
178177
}
179178
} else {
180179
errors::WhereClauseBeforeTypeAliasSugg::Remove { span }
@@ -566,11 +565,7 @@ impl<'a> AstValidator<'a> {
566565
self.dcx().emit_err(errors::BoundInContext { span, ctx });
567566
}
568567

569-
fn check_foreign_ty_genericless(
570-
&self,
571-
generics: &Generics,
572-
where_clauses: &TyAliasWhereClauses,
573-
) {
568+
fn check_foreign_ty_genericless(&self, generics: &Generics, after_where_clause: &WhereClause) {
574569
let cannot_have = |span, descr, remove_descr| {
575570
self.dcx().emit_err(errors::ExternTypesCannotHave {
576571
span,
@@ -584,14 +579,14 @@ impl<'a> AstValidator<'a> {
584579
cannot_have(generics.span, "generic parameters", "generic parameters");
585580
}
586581

587-
let check_where_clause = |where_clause: TyAliasWhereClause| {
582+
let check_where_clause = |where_clause: &WhereClause| {
588583
if where_clause.has_where_token {
589584
cannot_have(where_clause.span, "`where` clauses", "`where` clause");
590585
}
591586
};
592587

593-
check_where_clause(where_clauses.before);
594-
check_where_clause(where_clauses.after);
588+
check_where_clause(&generics.where_clause);
589+
check_where_clause(&after_where_clause);
595590
}
596591

597592
fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body_span: Option<Span>) {
@@ -1261,7 +1256,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12611256
visit::walk_item(self, item);
12621257
}
12631258
ItemKind::TyAlias(
1264-
ty_alias @ box TyAlias { defaultness, bounds, where_clauses, ty, .. },
1259+
ty_alias @ box TyAlias { defaultness, bounds, after_where_clause, ty, .. },
12651260
) => {
12661261
self.check_defaultness(item.span, *defaultness);
12671262
if ty.is_none() {
@@ -1276,9 +1271,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12761271
if let Err(err) = self.check_type_alias_where_clause_location(ty_alias) {
12771272
self.dcx().emit_err(err);
12781273
}
1279-
} else if where_clauses.after.has_where_token {
1274+
} else if after_where_clause.has_where_token {
12801275
self.dcx().emit_err(errors::WhereClauseAfterTypeAlias {
1281-
span: where_clauses.after.span,
1276+
span: after_where_clause.span,
12821277
help: self.sess.is_nightly_build(),
12831278
});
12841279
}
@@ -1308,15 +1303,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13081303
defaultness,
13091304
ident,
13101305
generics,
1311-
where_clauses,
1306+
after_where_clause,
13121307
bounds,
13131308
ty,
13141309
..
13151310
}) => {
13161311
self.check_defaultness(fi.span, *defaultness);
13171312
self.check_foreign_kind_bodyless(*ident, "type", ty.as_ref().map(|b| b.span));
13181313
self.check_type_no_bounds(bounds, "`extern` blocks");
1319-
self.check_foreign_ty_genericless(generics, where_clauses);
1314+
self.check_foreign_ty_genericless(generics, after_where_clause);
13201315
self.check_foreign_item_ascii_only(*ident);
13211316
}
13221317
ForeignItemKind::Static(box StaticItem { ident, safety, expr, .. }) => {

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ impl<'a> State<'a> {
5959
defaultness,
6060
ident,
6161
generics,
62-
where_clauses,
62+
after_where_clause,
6363
bounds,
6464
ty,
6565
}) => {
6666
self.print_associated_type(
6767
*ident,
6868
generics,
69-
*where_clauses,
69+
after_where_clause,
7070
bounds,
7171
ty.as_deref(),
7272
vis,
@@ -127,14 +127,12 @@ impl<'a> State<'a> {
127127
&mut self,
128128
ident: Ident,
129129
generics: &ast::Generics,
130-
where_clauses: ast::TyAliasWhereClauses,
130+
after_where_clause: &ast::WhereClause,
131131
bounds: &ast::GenericBounds,
132132
ty: Option<&ast::Ty>,
133133
vis: &ast::Visibility,
134134
defaultness: ast::Defaultness,
135135
) {
136-
let (before_predicates, after_predicates) =
137-
generics.where_clause.predicates.split_at(where_clauses.split);
138136
let (cb, ib) = self.head("");
139137
self.print_visibility(vis);
140138
self.print_defaultness(defaultness);
@@ -145,13 +143,13 @@ impl<'a> State<'a> {
145143
self.word_nbsp(":");
146144
self.print_type_bounds(bounds);
147145
}
148-
self.print_where_clause_parts(where_clauses.before.has_where_token, before_predicates);
146+
self.print_where_clause(&generics.where_clause);
149147
if let Some(ty) = ty {
150148
self.space();
151149
self.word_space("=");
152150
self.print_type(ty);
153151
}
154-
self.print_where_clause_parts(where_clauses.after.has_where_token, after_predicates);
152+
self.print_where_clause(&after_where_clause);
155153
self.word(";");
156154
self.end(ib);
157155
self.end(cb);
@@ -283,14 +281,14 @@ impl<'a> State<'a> {
283281
defaultness,
284282
ident,
285283
generics,
286-
where_clauses,
284+
after_where_clause,
287285
bounds,
288286
ty,
289287
}) => {
290288
self.print_associated_type(
291289
*ident,
292290
generics,
293-
*where_clauses,
291+
after_where_clause,
294292
bounds,
295293
ty.as_deref(),
296294
&item.vis,
@@ -585,14 +583,14 @@ impl<'a> State<'a> {
585583
defaultness,
586584
ident,
587585
generics,
588-
where_clauses,
586+
after_where_clause,
589587
bounds,
590588
ty,
591589
}) => {
592590
self.print_associated_type(
593591
*ident,
594592
generics,
595-
*where_clauses,
593+
after_where_clause,
596594
bounds,
597595
ty.as_deref(),
598596
vis,
@@ -759,14 +757,7 @@ impl<'a> State<'a> {
759757
}
760758

761759
fn print_where_clause(&mut self, where_clause: &ast::WhereClause) {
762-
self.print_where_clause_parts(where_clause.has_where_token, &where_clause.predicates);
763-
}
764-
765-
fn print_where_clause_parts(
766-
&mut self,
767-
has_where_token: bool,
768-
predicates: &[ast::WherePredicate],
769-
) {
760+
let ast::WhereClause { has_where_token, ref predicates, span: _ } = *where_clause;
770761
if predicates.is_empty() && !has_where_token {
771762
return;
772763
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ impl<'a> TraitDef<'a> {
610610
defaultness: ast::Defaultness::Final,
611611
ident,
612612
generics: Generics::default(),
613-
where_clauses: ast::TyAliasWhereClauses::default(),
613+
after_where_clause: ast::WhereClause::default(),
614614
bounds: Vec::new(),
615615
ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)),
616616
})),

0 commit comments

Comments
 (0)