Skip to content

Commit 05796d4

Browse files
committed
Trait aliases are rare large ast nodes, box them
1 parent 5a30e43 commit 05796d4

File tree

11 files changed

+55
-34
lines changed

11 files changed

+55
-34
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,8 +3590,9 @@ impl Item {
35903590
ItemKind::Const(i) => Some(&i.generics),
35913591
ItemKind::Fn(i) => Some(&i.generics),
35923592
ItemKind::TyAlias(i) => Some(&i.generics),
3593-
ItemKind::TraitAlias(_, generics, _)
3594-
| ItemKind::Enum(_, generics, _)
3593+
ItemKind::TraitAlias(i) => Some(&i.generics),
3594+
3595+
ItemKind::Enum(_, generics, _)
35953596
| ItemKind::Struct(_, generics, _)
35963597
| ItemKind::Union(_, generics, _) => Some(&generics),
35973598
ItemKind::Trait(i) => Some(&i.generics),
@@ -3698,6 +3699,14 @@ impl Default for FnHeader {
36983699
}
36993700
}
37003701

3702+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3703+
pub struct TraitAlias {
3704+
pub ident: Ident,
3705+
pub generics: Generics,
3706+
#[visitable(extra = BoundKind::Bound)]
3707+
pub bounds: GenericBounds,
3708+
}
3709+
37013710
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
37023711
pub struct Trait {
37033712
pub constness: Const,
@@ -3889,7 +3898,7 @@ pub enum ItemKind {
38893898
/// Trait alias.
38903899
///
38913900
/// E.g., `trait Foo = Bar + Quux;`.
3892-
TraitAlias(Ident, Generics, GenericBounds),
3901+
TraitAlias(Box<TraitAlias>),
38933902
/// An implementation.
38943903
///
38953904
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
@@ -3922,7 +3931,7 @@ impl ItemKind {
39223931
| ItemKind::Struct(ident, ..)
39233932
| ItemKind::Union(ident, ..)
39243933
| ItemKind::Trait(box Trait { ident, .. })
3925-
| ItemKind::TraitAlias(ident, ..)
3934+
| ItemKind::TraitAlias(box TraitAlias { ident, .. })
39263935
| ItemKind::MacroDef(ident, _)
39273936
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
39283937

@@ -3979,7 +3988,7 @@ impl ItemKind {
39793988
| Self::Struct(_, generics, _)
39803989
| Self::Union(_, generics, _)
39813990
| Self::Trait(box Trait { generics, .. })
3982-
| Self::TraitAlias(_, generics, _)
3991+
| Self::TraitAlias(box TraitAlias { generics, .. })
39833992
| Self::Impl(box Impl { generics, .. }) => Some(generics),
39843993
_ => None,
39853994
}
@@ -4141,8 +4150,8 @@ mod size_asserts {
41414150
static_assert_size!(GenericBound, 88);
41424151
static_assert_size!(Generics, 40);
41434152
static_assert_size!(Impl, 136);
4144-
static_assert_size!(Item, 144);
4145-
static_assert_size!(ItemKind, 80);
4153+
static_assert_size!(Item, 136);
4154+
static_assert_size!(ItemKind, 72);
41464155
static_assert_size!(LitKind, 24);
41474156
static_assert_size!(Local, 96);
41484157
static_assert_size!(MetaItemLit, 40);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ 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(ident, generics, bounds) => {
839+
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
840840
visit_visitable!($($mut)? vis, ident, generics);
841841
visit_visitable_with!($($mut)? vis, bounds, BoundKind::Bound)
842842
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
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(ident, generics, bounds) => {
447+
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
448448
let ident = self.lower_ident(*ident);
449449
let (generics, bounds) = self.lower_generics(
450450
generics,

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use ast::StaticItem;
22
use itertools::{Itertools, Position};
3-
use rustc_ast as ast;
4-
use rustc_ast::ModKind;
53
use rustc_ast::ptr::P;
4+
use rustc_ast::{self as ast, ModKind, TraitAlias};
65
use rustc_span::Ident;
76

87
use crate::pp::BoxMarker;
@@ -387,7 +386,7 @@ impl<'a> State<'a> {
387386
let empty = item.attrs.is_empty() && items.is_empty();
388387
self.bclose(item.span, empty, cb);
389388
}
390-
ast::ItemKind::TraitAlias(ident, generics, bounds) => {
389+
ast::ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
391390
let (cb, ib) = self.head(visibility_qualified(&item.vis, "trait"));
392391
self.print_ident(*ident);
393392
self.print_generic_params(&generics.params);

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ impl EarlyLintPass for NonCamelCaseTypes {
183183
ast::ItemKind::Trait(box ast::Trait { ident, .. }) => {
184184
self.check_case(cx, "trait", ident)
185185
}
186-
ast::ItemKind::TraitAlias(ident, _, _) => self.check_case(cx, "trait alias", ident),
186+
ast::ItemKind::TraitAlias(box ast::TraitAlias { ident, .. }) => {
187+
self.check_case(cx, "trait alias", ident)
188+
}
187189

188190
// N.B. This check is only for inherent associated types, so that we don't lint against
189191
// trait impls where we should have warned for the trait definition already.

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ impl<'a> Parser<'a> {
928928

929929
self.psess.gated_spans.gate(sym::trait_alias, whole_span);
930930

931-
Ok(ItemKind::TraitAlias(ident, generics, bounds))
931+
Ok(ItemKind::TraitAlias(Box::new(TraitAlias { ident, generics, bounds })))
932932
} else {
933933
// It's a normal trait.
934934
generics.where_clause = self.parse_where_clause()?;

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use std::sync::Arc;
1111
use rustc_ast::visit::{self, AssocCtxt, Visitor, WalkItemKind};
1212
use rustc_ast::{
1313
self as ast, AssocItem, AssocItemKind, Block, ConstItem, Delegation, Fn, ForeignItem,
14-
ForeignItemKind, Impl, Item, ItemKind, MetaItemKind, NodeId, StaticItem, StmtKind, TyAlias,
14+
ForeignItemKind, Impl, Item, ItemKind, MetaItemKind, NodeId, StaticItem, StmtKind, TraitAlias,
15+
TyAlias,
1516
};
1617
use rustc_attr_parsing as attr;
1718
use rustc_expand::base::ResolverExpand;
@@ -786,7 +787,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
786787
}
787788

788789
// These items live in the type namespace.
789-
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => {
790+
ItemKind::TyAlias(box TyAlias { ident, .. })
791+
| ItemKind::TraitAlias(box TraitAlias { ident, .. }) => {
790792
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
791793
}
792794

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,7 +2678,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
26782678
);
26792679
}
26802680

2681-
ItemKind::TraitAlias(_, ref generics, ref bounds) => {
2681+
ItemKind::TraitAlias(box TraitAlias { ref generics, ref bounds, .. }) => {
26822682
// Create a new rib for the trait-wide type parameters.
26832683
self.with_generic_param_rib(
26842684
&generics.params,
@@ -5193,7 +5193,7 @@ impl<'ast> Visitor<'ast> for ItemInfoCollector<'_, '_, '_> {
51935193
| ItemKind::Union(_, generics, _)
51945194
| ItemKind::Impl(box Impl { generics, .. })
51955195
| ItemKind::Trait(box Trait { generics, .. })
5196-
| ItemKind::TraitAlias(_, generics, _) => {
5196+
| ItemKind::TraitAlias(box TraitAlias { generics, .. }) => {
51975197
if let ItemKind::Fn(box Fn { sig, .. }) = &item.kind {
51985198
self.collect_fn_info(sig.header, &sig.decl, item.id, &item.attrs);
51995199
}

src/tools/clippy/clippy_utils/src/ast_utils/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,18 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
470470
&& over(lb, rb, eq_generic_bound)
471471
&& over(lis, ris, |l, r| eq_item(l, r, eq_assoc_item_kind))
472472
},
473-
(TraitAlias(li, lg, lb), TraitAlias(ri, rg, rb)) => {
474-
eq_id(*li, *ri) && eq_generics(lg, rg) && over(lb, rb, eq_generic_bound)
475-
},
473+
(
474+
TraitAlias(box ast::TraitAlias {
475+
ident: li,
476+
generics: lg,
477+
bounds: lb,
478+
}),
479+
TraitAlias(box ast::TraitAlias {
480+
ident: ri,
481+
generics: rg,
482+
bounds: rb,
483+
}),
484+
) => eq_id(*li, *ri) && eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
476485
(
477486
Impl(box ast::Impl {
478487
safety: lu,

src/tools/rustfmt/src/visitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
497497
let rw = self.with_context(|ctx| format_trait(ctx, item, block_indent));
498498
self.push_rewrite(item.span, rw);
499499
}
500-
ast::ItemKind::TraitAlias(ident, ref generics, ref generic_bounds) => {
500+
ast::ItemKind::TraitAlias(ref ta) => {
501501
let shape = Shape::indented(self.block_indent, self.config);
502502
let rw = format_trait_alias(
503503
&self.get_context(),
504-
ident,
504+
ta.ident,
505505
&item.vis,
506-
generics,
507-
generic_bounds,
506+
&ta.generics,
507+
&ta.bounds,
508508
shape,
509509
);
510510
self.push_rewrite(item.span, rw);

0 commit comments

Comments
 (0)