Skip to content

Commit 2f24967

Browse files
committed
Constify trait aliases
1 parent 05796d4 commit 2f24967

File tree

21 files changed

+157
-39
lines changed

21 files changed

+157
-39
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3701,6 +3701,7 @@ impl Default for FnHeader {
37013701

37023702
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
37033703
pub struct TraitAlias {
3704+
pub constness: Const,
37043705
pub ident: Ident,
37053706
pub generics: Generics,
37063707
#[visitable(extra = BoundKind::Bound)]

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,8 @@ macro_rules! common_visitor_and_walkers {
836836
visit_visitable!($($mut)? vis, impl_),
837837
ItemKind::Trait(trait_) =>
838838
visit_visitable!($($mut)? vis, trait_),
839-
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
840-
visit_visitable!($($mut)? vis, ident, generics);
839+
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds, constness}) => {
840+
visit_visitable!($($mut)? vis, ident, generics, constness);
841841
visit_visitable_with!($($mut)? vis, bounds, BoundKind::Bound)
842842
}
843843
ItemKind::MacCall(m) =>

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
444444
);
445445
hir::ItemKind::Trait(constness, *is_auto, safety, ident, generics, bounds, items)
446446
}
447-
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
447+
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds, constness }) => {
448448
let ident = self.lower_ident(*ident);
449449
let (generics, bounds) = self.lower_generics(
450450
generics,
@@ -458,7 +458,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
458458
)
459459
},
460460
);
461-
hir::ItemKind::TraitAlias(ident, generics, bounds)
461+
let constness = self.lower_constness(*constness);
462+
hir::ItemKind::TraitAlias(constness, ident, generics, bounds)
462463
}
463464
ItemKind::MacroDef(ident, MacroDef { body, macro_rules }) => {
464465
let ident = self.lower_ident(*ident);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,11 @@ impl<'a> State<'a> {
386386
let empty = item.attrs.is_empty() && items.is_empty();
387387
self.bclose(item.span, empty, cb);
388388
}
389-
ast::ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
390-
let (cb, ib) = self.head(visibility_qualified(&item.vis, "trait"));
389+
ast::ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds, constness }) => {
390+
let (cb, ib) = self.head("");
391+
self.print_visibility(&item.vis);
392+
self.print_constness(*constness);
393+
self.word_nbsp("trait");
391394
self.print_ident(*ident);
392395
self.print_generic_params(&generics.params);
393396
self.nbsp();

compiler/rustc_hir/src/hir.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4174,8 +4174,8 @@ impl<'hir> Item<'hir> {
41744174
ItemKind::Trait(constness, is_auto, safety, ident, generics, bounds, items),
41754175
(*constness, *is_auto, *safety, *ident, generics, bounds, items);
41764176

4177-
expect_trait_alias, (Ident, &'hir Generics<'hir>, GenericBounds<'hir>),
4178-
ItemKind::TraitAlias(ident, generics, bounds), (*ident, generics, bounds);
4177+
expect_trait_alias, (Constness, Ident, &'hir Generics<'hir>, GenericBounds<'hir>),
4178+
ItemKind::TraitAlias(constness, ident, generics, bounds), (*constness, *ident, generics, bounds);
41794179

41804180
expect_impl, &'hir Impl<'hir>, ItemKind::Impl(imp), imp;
41814181
}
@@ -4352,7 +4352,7 @@ pub enum ItemKind<'hir> {
43524352
&'hir [TraitItemId],
43534353
),
43544354
/// A trait alias.
4355-
TraitAlias(Ident, &'hir Generics<'hir>, GenericBounds<'hir>),
4355+
TraitAlias(Constness, Ident, &'hir Generics<'hir>, GenericBounds<'hir>),
43564356

43574357
/// An implementation, e.g., `impl<A> Trait for Foo { .. }`.
43584358
Impl(&'hir Impl<'hir>),
@@ -4395,7 +4395,7 @@ impl ItemKind<'_> {
43954395
| ItemKind::Struct(ident, ..)
43964396
| ItemKind::Union(ident, ..)
43974397
| ItemKind::Trait(_, _, _, ident, ..)
4398-
| ItemKind::TraitAlias(ident, ..) => Some(ident),
4398+
| ItemKind::TraitAlias(_, ident, ..) => Some(ident),
43994399

44004400
ItemKind::Use(_, UseKind::Glob | UseKind::ListStem)
44014401
| ItemKind::ForeignMod { .. }
@@ -4413,7 +4413,7 @@ impl ItemKind<'_> {
44134413
| ItemKind::Struct(_, generics, _)
44144414
| ItemKind::Union(_, generics, _)
44154415
| ItemKind::Trait(_, _, _, _, generics, _, _)
4416-
| ItemKind::TraitAlias(_, generics, _)
4416+
| ItemKind::TraitAlias(_, _, generics, _)
44174417
| ItemKind::Impl(Impl { generics, .. }) => generics,
44184418
_ => return None,
44194419
})

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
632632
walk_list!(visitor, visit_param_bound, bounds);
633633
walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
634634
}
635-
ItemKind::TraitAlias(ident, ref generics, bounds) => {
635+
ItemKind::TraitAlias(_constness, ident, ref generics, bounds) => {
636636
try_visit!(visitor.visit_ident(ident));
637637
try_visit!(visitor.visit_generics(generics));
638638
walk_list!(visitor, visit_param_bound, bounds);

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
164164
}
165165
}
166166
ItemKind::Trait(_, _, _, _, _, self_bounds, ..)
167-
| ItemKind::TraitAlias(_, _, self_bounds) => {
167+
| ItemKind::TraitAlias(_, _, _, self_bounds) => {
168168
is_trait = Some((self_bounds, item.span));
169169
}
170170
_ => {}
@@ -656,7 +656,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
656656

657657
let (generics, superbounds) = match item.kind {
658658
hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits),
659-
hir::ItemKind::TraitAlias(_, generics, supertraits) => (generics, supertraits),
659+
hir::ItemKind::TraitAlias(_, _, generics, supertraits) => (generics, supertraits),
660660
_ => span_bug!(item.span, "super_predicates invoked on non-trait"),
661661
};
662662

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
635635
| hir::ItemKind::Struct(_, generics, _)
636636
| hir::ItemKind::Union(_, generics, _)
637637
| hir::ItemKind::Trait(_, _, _, _, generics, ..)
638-
| hir::ItemKind::TraitAlias(_, generics, ..)
638+
| hir::ItemKind::TraitAlias(_, _, generics, ..)
639639
| hir::ItemKind::Impl(&hir::Impl { generics, .. }) => {
640640
// These kinds of items have only early-bound lifetime parameters.
641641
self.visit_early(item.hir_id(), generics, |this| intravisit::walk_item(this, item));

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
330330

331331
let (trait_generics, trait_bounds) = match parent_trait.kind {
332332
hir::ItemKind::Trait(_, _, _, _, generics, supertraits, _) => (generics, supertraits),
333-
hir::ItemKind::TraitAlias(_, generics, supertraits) => (generics, supertraits),
333+
hir::ItemKind::TraitAlias(_, _, generics, supertraits) => (generics, supertraits),
334334
_ => unreachable!(),
335335
};
336336

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,10 @@ impl<'a> State<'a> {
760760
}
761761
self.bclose(item.span, cb);
762762
}
763-
hir::ItemKind::TraitAlias(ident, generics, bounds) => {
764-
let (cb, ib) = self.head("trait");
763+
hir::ItemKind::TraitAlias(constness, ident, generics, bounds) => {
764+
let (cb, ib) = self.head("");
765+
self.print_constness(constness);
766+
self.word_nbsp("trait");
765767
self.print_ident(ident);
766768
self.print_generic_params(generics.params);
767769
self.nbsp();

0 commit comments

Comments
 (0)