Skip to content

Commit 71b4e2d

Browse files
committed
Box HIR Generics and Impl.
1 parent 76d4862 commit 71b4e2d

File tree

16 files changed

+61
-56
lines changed

16 files changed

+61
-56
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
410410
ImplPolarity::Positive => ImplPolarity::Positive,
411411
ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(s)),
412412
};
413-
hir::ItemKind::Impl(hir::Impl {
413+
hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
414414
unsafety: self.lower_unsafety(unsafety),
415415
polarity,
416416
defaultness,
@@ -420,7 +420,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
420420
of_trait: trait_ref,
421421
self_ty: lowered_ty,
422422
items: new_impl_items,
423-
})
423+
}))
424424
}
425425
ItemKind::Trait(box Trait {
426426
is_auto,
@@ -1226,7 +1226,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12261226
id: NodeId,
12271227
kind: FnDeclKind,
12281228
is_async: Option<NodeId>,
1229-
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
1229+
) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
12301230
let header = self.lower_fn_header(sig.header);
12311231
let (generics, decl) = self.add_implicit_generics(generics, id, |this, idty| {
12321232
this.lower_fn_decl(&sig.decl, Some((id, idty)), kind, is_async)
@@ -1349,7 +1349,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13491349
&mut self,
13501350
generics: &Generics,
13511351
itctx: ImplTraitContext<'_, 'hir>,
1352-
) -> hir::Generics<'hir> {
1352+
) -> &'hir hir::Generics<'hir> {
13531353
let generics_ctor = self.lower_generics_mut(generics, itctx);
13541354
generics_ctor.into_generics(self.arena)
13551355
}
@@ -1419,11 +1419,11 @@ pub(super) struct GenericsCtor<'hir> {
14191419
}
14201420

14211421
impl<'hir> GenericsCtor<'hir> {
1422-
pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> hir::Generics<'hir> {
1423-
hir::Generics {
1422+
pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> &'hir hir::Generics<'hir> {
1423+
arena.alloc(hir::Generics {
14241424
params: arena.alloc_from_iter(self.params),
14251425
where_clause: self.where_clause,
14261426
span: self.span,
1427-
}
1427+
})
14281428
}
14291429
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
719719
generics: &Generics,
720720
parent_node_id: NodeId,
721721
f: impl FnOnce(&mut Self, &mut Vec<hir::GenericParam<'hir>>) -> T,
722-
) -> (hir::Generics<'hir>, T) {
722+
) -> (&'hir hir::Generics<'hir>, T) {
723723
let mut impl_trait_defs = Vec::new();
724724
let mut lowered_generics = self.lower_generics_mut(
725725
generics,
@@ -1383,11 +1383,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13831383
debug!("lower_opaque_impl_trait: lifetime_defs={:#?}", lifetime_defs);
13841384

13851385
let opaque_ty_item = hir::OpaqueTy {
1386-
generics: hir::Generics {
1386+
generics: self.arena.alloc(hir::Generics {
13871387
params: lifetime_defs,
13881388
where_clause: hir::WhereClause { predicates: &[], span: lctx.lower_span(span) },
13891389
span: lctx.lower_span(span),
1390-
},
1390+
}),
13911391
bounds: hir_bounds,
13921392
origin,
13931393
};
@@ -1715,11 +1715,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17151715
debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params);
17161716

17171717
let opaque_ty_item = hir::OpaqueTy {
1718-
generics: hir::Generics {
1718+
generics: this.arena.alloc(hir::Generics {
17191719
params: generic_params,
17201720
where_clause: hir::WhereClause { predicates: &[], span: this.lower_span(span) },
17211721
span: this.lower_span(span),
1722-
},
1722+
}),
17231723
bounds: arena_vec![this; future_bound],
17241724
origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
17251725
};

compiler/rustc_hir/src/arena.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ macro_rules! arena_types {
1515
[] block: rustc_hir::Block<'tcx>,
1616
[] bare_fn_ty: rustc_hir::BareFnTy<'tcx>,
1717
[] body: rustc_hir::Body<'tcx>,
18+
[] generics: rustc_hir::Generics<'tcx>,
1819
[] generic_arg: rustc_hir::GenericArg<'tcx>,
1920
[] generic_args: rustc_hir::GenericArgs<'tcx>,
2021
[] generic_bound: rustc_hir::GenericBound<'tcx>,
2122
[] generic_param: rustc_hir::GenericParam<'tcx>,
2223
[] expr: rustc_hir::Expr<'tcx>,
24+
[] impl_: rustc_hir::Impl<'tcx>,
2325
[] let_expr: rustc_hir::Let<'tcx>,
2426
[] expr_field: rustc_hir::ExprField<'tcx>,
2527
[] pat_field: rustc_hir::PatField<'tcx>,

compiler/rustc_hir/src/hir.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,13 @@ pub struct Generics<'hir> {
572572
}
573573

574574
impl<'hir> Generics<'hir> {
575-
pub const fn empty() -> Generics<'hir> {
576-
Generics {
575+
pub const fn empty() -> &'hir Generics<'hir> {
576+
const NOPE: Generics<'_> = Generics {
577577
params: &[],
578578
where_clause: WhereClause { predicates: &[], span: DUMMY_SP },
579579
span: DUMMY_SP,
580-
}
580+
};
581+
&NOPE
581582
}
582583

583584
pub fn get_named(&self, name: Symbol) -> Option<&GenericParam<'_>> {
@@ -2075,7 +2076,7 @@ impl TraitItemId {
20752076
pub struct TraitItem<'hir> {
20762077
pub ident: Ident,
20772078
pub def_id: LocalDefId,
2078-
pub generics: Generics<'hir>,
2079+
pub generics: &'hir Generics<'hir>,
20792080
pub kind: TraitItemKind<'hir>,
20802081
pub span: Span,
20812082
}
@@ -2135,7 +2136,7 @@ impl ImplItemId {
21352136
pub struct ImplItem<'hir> {
21362137
pub ident: Ident,
21372138
pub def_id: LocalDefId,
2138-
pub generics: Generics<'hir>,
2139+
pub generics: &'hir Generics<'hir>,
21392140
pub kind: ImplItemKind<'hir>,
21402141
pub span: Span,
21412142
pub vis_span: Span,
@@ -2340,7 +2341,7 @@ pub struct BareFnTy<'hir> {
23402341

23412342
#[derive(Debug, HashStable_Generic)]
23422343
pub struct OpaqueTy<'hir> {
2343-
pub generics: Generics<'hir>,
2344+
pub generics: &'hir Generics<'hir>,
23442345
pub bounds: GenericBounds<'hir>,
23452346
pub origin: OpaqueTyOrigin,
23462347
}
@@ -2814,7 +2815,7 @@ pub enum ItemKind<'hir> {
28142815
/// A `const` item.
28152816
Const(&'hir Ty<'hir>, BodyId),
28162817
/// A function declaration.
2817-
Fn(FnSig<'hir>, Generics<'hir>, BodyId),
2818+
Fn(FnSig<'hir>, &'hir Generics<'hir>, BodyId),
28182819
/// A MBE macro definition (`macro_rules!` or `macro`).
28192820
Macro(ast::MacroDef, MacroKind),
28202821
/// A module.
@@ -2824,22 +2825,22 @@ pub enum ItemKind<'hir> {
28242825
/// Module-level inline assembly (from `global_asm!`).
28252826
GlobalAsm(&'hir InlineAsm<'hir>),
28262827
/// A type alias, e.g., `type Foo = Bar<u8>`.
2827-
TyAlias(&'hir Ty<'hir>, Generics<'hir>),
2828+
TyAlias(&'hir Ty<'hir>, &'hir Generics<'hir>),
28282829
/// An opaque `impl Trait` type alias, e.g., `type Foo = impl Bar;`.
28292830
OpaqueTy(OpaqueTy<'hir>),
28302831
/// An enum definition, e.g., `enum Foo<A, B> {C<A>, D<B>}`.
2831-
Enum(EnumDef<'hir>, Generics<'hir>),
2832+
Enum(EnumDef<'hir>, &'hir Generics<'hir>),
28322833
/// A struct definition, e.g., `struct Foo<A> {x: A}`.
2833-
Struct(VariantData<'hir>, Generics<'hir>),
2834+
Struct(VariantData<'hir>, &'hir Generics<'hir>),
28342835
/// A union definition, e.g., `union Foo<A, B> {x: A, y: B}`.
2835-
Union(VariantData<'hir>, Generics<'hir>),
2836+
Union(VariantData<'hir>, &'hir Generics<'hir>),
28362837
/// A trait definition.
2837-
Trait(IsAuto, Unsafety, Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemRef]),
2838+
Trait(IsAuto, Unsafety, &'hir Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemRef]),
28382839
/// A trait alias.
2839-
TraitAlias(Generics<'hir>, GenericBounds<'hir>),
2840+
TraitAlias(&'hir Generics<'hir>, GenericBounds<'hir>),
28402841

28412842
/// An implementation, e.g., `impl<A> Trait for Foo { .. }`.
2842-
Impl(Impl<'hir>),
2843+
Impl(&'hir Impl<'hir>),
28432844
}
28442845

28452846
#[derive(Debug, HashStable_Generic)]
@@ -2851,7 +2852,7 @@ pub struct Impl<'hir> {
28512852
// decoding as `Span`s cannot be decoded when a `Session` is not available.
28522853
pub defaultness_span: Option<Span>,
28532854
pub constness: Constness,
2854-
pub generics: Generics<'hir>,
2855+
pub generics: &'hir Generics<'hir>,
28552856

28562857
/// The trait being implemented, if any.
28572858
pub of_trait: Option<TraitRef<'hir>>,
@@ -2993,7 +2994,7 @@ impl ForeignItem<'_> {
29932994
#[derive(Debug, HashStable_Generic)]
29942995
pub enum ForeignItemKind<'hir> {
29952996
/// A foreign function.
2996-
Fn(&'hir FnDecl<'hir>, &'hir [Ident], Generics<'hir>),
2997+
Fn(&'hir FnDecl<'hir>, &'hir [Ident], &'hir Generics<'hir>),
29972998
/// A foreign static item (`static ext: u8`).
29982999
Static(&'hir Ty<'hir>, Mutability),
29993000
/// A foreign type.
@@ -3326,9 +3327,11 @@ mod size_asserts {
33263327
rustc_data_structures::static_assert_size!(super::QPath<'static>, 24);
33273328
rustc_data_structures::static_assert_size!(super::Ty<'static>, 72);
33283329
rustc_data_structures::static_assert_size!(super::GenericBound<'_>, 48);
3330+
rustc_data_structures::static_assert_size!(super::Generics<'static>, 48);
3331+
rustc_data_structures::static_assert_size!(super::Impl<'static>, 80);
33293332

3330-
rustc_data_structures::static_assert_size!(super::Item<'static>, 160);
3331-
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 128);
3332-
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 120);
3333-
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 112);
3333+
rustc_data_structures::static_assert_size!(super::Item<'static>, 80);
3334+
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 88);
3335+
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 80);
3336+
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 72);
33343337
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
619619
visitor.visit_generics(generics);
620620
walk_list!(visitor, visit_trait_ref, of_trait);
621621
visitor.visit_ty(self_ty);
622-
walk_list!(visitor, visit_impl_item_ref, items);
622+
walk_list!(visitor, visit_impl_item_ref, *items);
623623
}
624624
ItemKind::Struct(ref struct_definition, ref generics)
625625
| ItemKind::Union(ref struct_definition, ref generics) => {

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,16 +626,16 @@ impl<'a> State<'a> {
626626
items,
627627
}) => {
628628
self.head("");
629-
self.print_defaultness(defaultness);
630-
self.print_unsafety(unsafety);
629+
self.print_defaultness(*defaultness);
630+
self.print_unsafety(*unsafety);
631631
self.word_nbsp("impl");
632632

633633
if !generics.params.is_empty() {
634634
self.print_generic_params(&generics.params);
635635
self.space();
636636
}
637637

638-
if constness == hir::Constness::Const {
638+
if *constness == hir::Constness::Const {
639639
self.word_nbsp("const");
640640
}
641641

@@ -655,7 +655,7 @@ impl<'a> State<'a> {
655655
self.space();
656656
self.bopen();
657657
self.print_inner_attributes(attrs);
658-
for impl_item in items {
658+
for impl_item in *items {
659659
self.ann.nested(self, Nested::ImplItem(impl_item.id));
660660
}
661661
self.bclose(item.span);

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,8 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
11991199
});
12001200
}
12011201
}
1202-
hir::ItemKind::Impl(hir::Impl { ref generics, items, .. }) => {
1203-
for it in items {
1202+
hir::ItemKind::Impl(hir::Impl { generics, items, .. }) => {
1203+
for it in *items {
12041204
if let hir::AssocItemKind::Fn { .. } = it.kind {
12051205
if let Some(no_mangle_attr) = cx
12061206
.sess()

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,8 +1462,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14621462
}))
14631463
}
14641464
hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => {
1465-
self.tables.impl_defaultness.set(def_id.index, defaultness);
1466-
self.tables.impl_constness.set(def_id.index, constness);
1465+
self.tables.impl_defaultness.set(def_id.index, *defaultness);
1466+
self.tables.impl_constness.set(def_id.index, *constness);
14671467

14681468
let trait_ref = self.tcx.impl_trait_ref(def_id);
14691469
if let Some(trait_ref) = trait_ref {

compiler/rustc_passes/src/dead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl<'v, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'tcx> {
512512
if of_trait.is_some() {
513513
self.worklist.push(item.def_id);
514514
}
515-
for impl_item_ref in items {
515+
for impl_item_ref in *items {
516516
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
517517
if of_trait.is_some()
518518
|| has_allow_dead_code_or_lang_attr(self.tcx, impl_item.hir_id())

compiler/rustc_passes/src/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
737737
}
738738
}
739739

740-
for impl_item_ref in items {
740+
for impl_item_ref in *items {
741741
let impl_item = self.tcx.associated_item(impl_item_ref.id.def_id);
742742

743743
if let Some(def_id) = impl_item.trait_item_def_id {

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl ForLifetimeSpanType {
8686
}
8787
}
8888

89-
impl<'tcx> Into<MissingLifetimeSpot<'tcx>> for &'tcx hir::Generics<'tcx> {
89+
impl<'tcx> Into<MissingLifetimeSpot<'tcx>> for &&'tcx hir::Generics<'tcx> {
9090
fn into(self) -> MissingLifetimeSpot<'tcx> {
9191
MissingLifetimeSpot::Generics(self)
9292
}

compiler/rustc_typeck/src/collect.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,16 +2200,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
22002200

22012201
let icx = ItemCtxt::new(tcx, def_id);
22022202

2203-
const NO_GENERICS: &hir::Generics<'_> = &hir::Generics::empty();
2203+
const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
22042204

22052205
// We use an `IndexSet` to preserves order of insertion.
22062206
// Preserving the order of insertion is important here so as not to break UI tests.
22072207
let mut predicates: FxIndexSet<(ty::Predicate<'_>, Span)> = FxIndexSet::default();
22082208

22092209
let ast_generics = match node {
2210-
Node::TraitItem(item) => &item.generics,
2210+
Node::TraitItem(item) => item.generics,
22112211

2212-
Node::ImplItem(item) => &item.generics,
2212+
Node::ImplItem(item) => item.generics,
22132213

22142214
Node::Item(item) => {
22152215
match item.kind {
@@ -2223,15 +2223,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
22232223
| ItemKind::TyAlias(_, ref generics)
22242224
| ItemKind::Enum(_, ref generics)
22252225
| ItemKind::Struct(_, ref generics)
2226-
| ItemKind::Union(_, ref generics) => generics,
2226+
| ItemKind::Union(_, ref generics) => *generics,
22272227

22282228
ItemKind::Trait(_, _, ref generics, ..) => {
22292229
is_trait = Some(ty::TraitRef::identity(tcx, def_id));
2230-
generics
2230+
*generics
22312231
}
22322232
ItemKind::TraitAlias(ref generics, _) => {
22332233
is_trait = Some(ty::TraitRef::identity(tcx, def_id));
2234-
generics
2234+
*generics
22352235
}
22362236
ItemKind::OpaqueTy(OpaqueTy {
22372237
origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..),
@@ -2268,7 +2268,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
22682268

22692269
Node::ForeignItem(item) => match item.kind {
22702270
ForeignItemKind::Static(..) => NO_GENERICS,
2271-
ForeignItemKind::Fn(_, _, ref generics) => generics,
2271+
ForeignItemKind::Fn(_, _, ref generics) => *generics,
22722272
ForeignItemKind::Type => NO_GENERICS,
22732273
},
22742274

compiler/rustc_typeck/src/collect/type_of.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
337337
icx.to_ty(ty)
338338
}
339339
}
340-
ItemKind::TyAlias(self_ty, _)
341-
| ItemKind::Impl(hir::Impl { self_ty, .. }) => icx.to_ty(self_ty),
340+
ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty),
341+
ItemKind::Impl(hir::Impl { self_ty, .. }) => icx.to_ty(*self_ty),
342342
ItemKind::Fn(..) => {
343343
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
344344
tcx.mk_fn_def(def_id.to_def_id(), substs)

src/tools/clippy/clippy_lints/src/new_without_default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
6868
..
6969
}) = item.kind
7070
{
71-
for assoc_item in items {
71+
for assoc_item in *items {
7272
if assoc_item.kind == (hir::AssocItemKind::Fn { has_self: false }) {
7373
let impl_item = cx.tcx.hir().impl_item(assoc_item.id);
7474
if in_external_macro(cx.sess(), impl_item.span) {

src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'tcx> LateLintPass<'tcx> for PartialEqNeImpl {
4242
if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
4343
if trait_ref.path.res.def_id() == eq_trait;
4444
then {
45-
for impl_item in impl_items {
45+
for impl_item in *impl_items {
4646
if impl_item.ident.name == sym::ne {
4747
span_lint_hir(
4848
cx,

src/tools/clippy/clippy_lints/src/serde_api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'tcx> LateLintPass<'tcx> for SerdeApi {
3636
if did == visit_did {
3737
let mut seen_str = None;
3838
let mut seen_string = None;
39-
for item in items {
39+
for item in *items {
4040
match item.ident.as_str() {
4141
"visit_str" => seen_str = Some(item.span),
4242
"visit_string" => seen_string = Some(item.span),

0 commit comments

Comments
 (0)