Skip to content

Commit 305847d

Browse files
committed
Trait aliases are rare large ast nodes, box them
1 parent 9f32ccf commit 305847d

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
@@ -3538,8 +3538,9 @@ impl Item {
35383538
ItemKind::Const(i) => Some(&i.generics),
35393539
ItemKind::Fn(i) => Some(&i.generics),
35403540
ItemKind::TyAlias(i) => Some(&i.generics),
3541-
ItemKind::TraitAlias(_, generics, _)
3542-
| ItemKind::Enum(_, generics, _)
3541+
ItemKind::TraitAlias(i) => Some(&i.generics),
3542+
3543+
ItemKind::Enum(_, generics, _)
35433544
| ItemKind::Struct(_, generics, _)
35443545
| ItemKind::Union(_, generics, _) => Some(&generics),
35453546
ItemKind::Trait(i) => Some(&i.generics),
@@ -3621,6 +3622,14 @@ impl Default for FnHeader {
36213622
}
36223623
}
36233624

3625+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3626+
pub struct TraitAlias {
3627+
pub ident: Ident,
3628+
pub generics: Generics,
3629+
#[visitable(extra = BoundKind::Bound)]
3630+
pub bounds: GenericBounds,
3631+
}
3632+
36243633
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
36253634
pub struct Trait {
36263635
pub constness: Const,
@@ -3816,7 +3825,7 @@ pub enum ItemKind {
38163825
/// Trait alias.
38173826
///
38183827
/// E.g., `trait Foo = Bar + Quux;`.
3819-
TraitAlias(Ident, Generics, GenericBounds),
3828+
TraitAlias(Box<TraitAlias>),
38203829
/// An implementation.
38213830
///
38223831
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
@@ -3849,7 +3858,7 @@ impl ItemKind {
38493858
| ItemKind::Struct(ident, ..)
38503859
| ItemKind::Union(ident, ..)
38513860
| ItemKind::Trait(box Trait { ident, .. })
3852-
| ItemKind::TraitAlias(ident, ..)
3861+
| ItemKind::TraitAlias(box TraitAlias { ident, .. })
38533862
| ItemKind::MacroDef(ident, _)
38543863
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
38553864

@@ -3906,7 +3915,7 @@ impl ItemKind {
39063915
| Self::Struct(_, generics, _)
39073916
| Self::Union(_, generics, _)
39083917
| Self::Trait(box Trait { generics, .. })
3909-
| Self::TraitAlias(_, generics, _)
3918+
| Self::TraitAlias(box TraitAlias { generics, .. })
39103919
| Self::Impl(Impl { generics, .. }) => Some(generics),
39113920
_ => None,
39123921
}
@@ -4068,8 +4077,8 @@ mod size_asserts {
40684077
static_assert_size!(GenericBound, 88);
40694078
static_assert_size!(Generics, 40);
40704079
static_assert_size!(Impl, 64);
4071-
static_assert_size!(Item, 144);
4072-
static_assert_size!(ItemKind, 80);
4080+
static_assert_size!(Item, 136);
4081+
static_assert_size!(ItemKind, 72);
40734082
static_assert_size!(LitKind, 24);
40744083
static_assert_size!(Local, 96);
40754084
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
@@ -835,7 +835,7 @@ macro_rules! common_visitor_and_walkers {
835835
visit_visitable!($($mut)? vis, impl_),
836836
ItemKind::Trait(trait_) =>
837837
visit_visitable!($($mut)? vis, trait_),
838-
ItemKind::TraitAlias(ident, generics, bounds) => {
838+
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
839839
visit_visitable!($($mut)? vis, ident, generics);
840840
visit_visitable_with!($($mut)? vis, bounds, BoundKind::Bound)
841841
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
417417
);
418418
hir::ItemKind::Trait(constness, *is_auto, safety, ident, generics, bounds, items)
419419
}
420-
ItemKind::TraitAlias(ident, generics, bounds) => {
420+
ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
421421
let ident = self.lower_ident(*ident);
422422
let (generics, bounds) = self.lower_generics(
423423
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;
@@ -388,7 +387,7 @@ impl<'a> State<'a> {
388387
let empty = item.attrs.is_empty() && items.is_empty();
389388
self.bclose(item.span, empty, cb);
390389
}
391-
ast::ItemKind::TraitAlias(ident, generics, bounds) => {
390+
ast::ItemKind::TraitAlias(box TraitAlias { ident, generics, bounds }) => {
392391
let (cb, ib) = self.head(visibility_qualified(&item.vis, "trait"));
393392
self.print_ident(*ident);
394393
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
@@ -11,7 +11,7 @@ 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, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TyAlias,
14+
ForeignItemKind, Inline, Item, ItemKind, NodeId, StaticItem, StmtKind, TraitAlias, TyAlias,
1515
};
1616
use rustc_attr_parsing as attr;
1717
use rustc_attr_parsing::AttributeParser;
@@ -842,7 +842,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
842842
}
843843

844844
// These items live in the type namespace.
845-
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => {
845+
ItemKind::TyAlias(box TyAlias { ident, .. })
846+
| ItemKind::TraitAlias(box TraitAlias { ident, .. }) => {
846847
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
847848
}
848849

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)