Skip to content

Commit 3ca752f

Browse files
committed
Trait aliases are rare large ast nodes, box them
1 parent 23fced0 commit 3ca752f

File tree

11 files changed

+54
-34
lines changed

11 files changed

+54
-34
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3540,8 +3540,9 @@ impl Item {
35403540
ItemKind::Const(i) => Some(&i.generics),
35413541
ItemKind::Fn(i) => Some(&i.generics),
35423542
ItemKind::TyAlias(i) => Some(&i.generics),
3543-
ItemKind::TraitAlias(_, generics, _)
3544-
| ItemKind::Enum(_, generics, _)
3543+
ItemKind::TraitAlias(i) => Some(&i.generics),
3544+
3545+
ItemKind::Enum(_, generics, _)
35453546
| ItemKind::Struct(_, generics, _)
35463547
| ItemKind::Union(_, generics, _) => Some(&generics),
35473548
ItemKind::Trait(i) => Some(&i.generics),
@@ -3623,6 +3624,14 @@ impl Default for FnHeader {
36233624
}
36243625
}
36253626

3627+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3628+
pub struct TraitAlias {
3629+
pub ident: Ident,
3630+
pub generics: Generics,
3631+
#[visitable(extra = BoundKind::Bound)]
3632+
pub bounds: GenericBounds,
3633+
}
3634+
36263635
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
36273636
pub struct Trait {
36283637
pub constness: Const,
@@ -3795,7 +3804,7 @@ pub enum ItemKind {
37953804
/// Trait alias.
37963805
///
37973806
/// E.g., `trait Foo = Bar + Quux;`.
3798-
TraitAlias(Ident, Generics, GenericBounds),
3807+
TraitAlias(Box<TraitAlias>),
37993808
/// An implementation.
38003809
///
38013810
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
@@ -3828,7 +3837,7 @@ impl ItemKind {
38283837
| ItemKind::Struct(ident, ..)
38293838
| ItemKind::Union(ident, ..)
38303839
| ItemKind::Trait(box Trait { ident, .. })
3831-
| ItemKind::TraitAlias(ident, ..)
3840+
| ItemKind::TraitAlias(box TraitAlias { ident, .. })
38323841
| ItemKind::MacroDef(ident, _)
38333842
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
38343843

@@ -3885,7 +3894,7 @@ impl ItemKind {
38853894
| Self::Struct(_, generics, _)
38863895
| Self::Union(_, generics, _)
38873896
| Self::Trait(box Trait { generics, .. })
3888-
| Self::TraitAlias(_, generics, _)
3897+
| Self::TraitAlias(box TraitAlias { generics, .. })
38893898
| Self::Impl(Impl { generics, .. }) => Some(generics),
38903899
_ => None,
38913900
}
@@ -4047,8 +4056,8 @@ mod size_asserts {
40474056
static_assert_size!(GenericBound, 88);
40484057
static_assert_size!(Generics, 40);
40494058
static_assert_size!(Impl, 64);
4050-
static_assert_size!(Item, 144);
4051-
static_assert_size!(ItemKind, 80);
4059+
static_assert_size!(Item, 136);
4060+
static_assert_size!(ItemKind, 72);
40524061
static_assert_size!(LitKind, 24);
40534062
static_assert_size!(Local, 96);
40544063
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
@@ -833,7 +833,7 @@ macro_rules! common_visitor_and_walkers {
833833
visit_visitable!($($mut)? vis, impl_),
834834
ItemKind::Trait(trait_) =>
835835
visit_visitable!($($mut)? vis, trait_),
836-
ItemKind::TraitAlias(ident, generics, bounds) => {
836+
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
837837
visit_visitable!($($mut)? vis, ident, generics);
838838
visit_visitable_with!($($mut)? vis, bounds, BoundKind::Bound)
839839
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
415415
);
416416
hir::ItemKind::Trait(constness, *is_auto, safety, ident, generics, bounds, items)
417417
}
418-
ItemKind::TraitAlias(ident, generics, bounds) => {
418+
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
419419
let ident = self.lower_ident(*ident);
420420
let (generics, bounds) = self.lower_generics(
421421
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,7 +1,6 @@
11
use ast::StaticItem;
22
use itertools::{Itertools, Position};
3-
use rustc_ast as ast;
4-
use rustc_ast::ModKind;
3+
use rustc_ast::{self as ast, ModKind, TraitAlias};
54
use rustc_span::Ident;
65

76
use crate::pp::BoxMarker;
@@ -386,7 +385,7 @@ impl<'a> State<'a> {
386385
let empty = item.attrs.is_empty() && items.is_empty();
387386
self.bclose(item.span, empty, cb);
388387
}
389-
ast::ItemKind::TraitAlias(ident, generics, bounds) => {
388+
ast::ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
390389
let (cb, ib) = self.head(visibility_qualified(&item.vis, "trait"));
391390
self.print_ident(*ident);
392391
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
@@ -160,7 +160,9 @@ impl EarlyLintPass for NonCamelCaseTypes {
160160
ast::ItemKind::Trait(box ast::Trait { ident, .. }) => {
161161
self.check_case(cx, "trait", ident)
162162
}
163-
ast::ItemKind::TraitAlias(ident, _, _) => self.check_case(cx, "trait alias", ident),
163+
ast::ItemKind::TraitAlias(box ast::TraitAlias { ident, .. }) => {
164+
self.check_case(cx, "trait alias", ident)
165+
}
164166

165167
// N.B. This check is only for inherent associated types, so that we don't lint against
166168
// 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
@@ -955,7 +955,7 @@ impl<'a> Parser<'a> {
955955

956956
self.psess.gated_spans.gate(sym::trait_alias, whole_span);
957957

958-
Ok(ItemKind::TraitAlias(ident, generics, bounds))
958+
Ok(ItemKind::TraitAlias(Box::new(TraitAlias { ident, generics, bounds })))
959959
} else {
960960
// It's a normal trait.
961961
generics.where_clause = self.parse_where_clause()?;

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::Arc;
1010
use rustc_ast::visit::{self, AssocCtxt, Visitor, WalkItemKind};
1111
use rustc_ast::{
1212
self as ast, AssocItem, AssocItemKind, Block, ConstItem, Delegation, Fn, ForeignItem,
13-
ForeignItemKind, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TyAlias,
13+
ForeignItemKind, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TraitAlias, TyAlias,
1414
};
1515
use rustc_attr_parsing as attr;
1616
use rustc_attr_parsing::AttributeParser;
@@ -844,7 +844,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
844844
}
845845

846846
// These items live in the type namespace.
847-
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => {
847+
ItemKind::TyAlias(box TyAlias { ident, .. })
848+
| ItemKind::TraitAlias(box TraitAlias { ident, .. }) => {
848849
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
849850
}
850851

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,7 +2645,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
26452645
);
26462646
}
26472647

2648-
ItemKind::TraitAlias(_, ref generics, ref bounds) => {
2648+
ItemKind::TraitAlias(box TraitAlias { ref generics, ref bounds, .. }) => {
26492649
// Create a new rib for the trait-wide type parameters.
26502650
self.with_generic_param_rib(
26512651
&generics.params,
@@ -5164,7 +5164,7 @@ impl<'ast> Visitor<'ast> for ItemInfoCollector<'_, '_, '_> {
51645164
| ItemKind::Union(_, generics, _)
51655165
| ItemKind::Impl(Impl { generics, .. })
51665166
| ItemKind::Trait(box Trait { generics, .. })
5167-
| ItemKind::TraitAlias(_, generics, _) => {
5167+
| ItemKind::TraitAlias(box TraitAlias { generics, .. }) => {
51685168
if let ItemKind::Fn(box Fn { sig, .. }) = &item.kind {
51695169
self.collect_fn_info(sig.header, &sig.decl, item.id, &item.attrs);
51705170
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,18 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
473473
&& over(lb, rb, eq_generic_bound)
474474
&& over(lis, ris, |l, r| eq_item(l, r, eq_assoc_item_kind))
475475
},
476-
(TraitAlias(li, lg, lb), TraitAlias(ri, rg, rb)) => {
477-
eq_id(*li, *ri) && eq_generics(lg, rg) && over(lb, rb, eq_generic_bound)
478-
},
476+
(
477+
TraitAlias(box ast::TraitAlias {
478+
ident: li,
479+
generics: lg,
480+
bounds: lb,
481+
}),
482+
TraitAlias(box ast::TraitAlias {
483+
ident: ri,
484+
generics: rg,
485+
bounds: rb,
486+
}),
487+
) => eq_id(*li, *ri) && eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
479488
(
480489
Impl(ast::Impl {
481490
generics: lg,

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)