From 71b4e2d852be70174579e7e5a96644418348a7da Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 5 Feb 2022 15:26:49 +0100 Subject: [PATCH] Box HIR Generics and Impl. --- compiler/rustc_ast_lowering/src/item.rs | 14 +++--- compiler/rustc_ast_lowering/src/lib.rs | 10 ++--- compiler/rustc_hir/src/arena.rs | 2 + compiler/rustc_hir/src/hir.rs | 43 ++++++++++--------- compiler/rustc_hir/src/intravisit.rs | 2 +- compiler/rustc_hir_pretty/src/lib.rs | 8 ++-- compiler/rustc_lint/src/builtin.rs | 4 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 +- compiler/rustc_passes/src/dead.rs | 2 +- compiler/rustc_passes/src/stability.rs | 2 +- .../rustc_resolve/src/late/diagnostics.rs | 2 +- compiler/rustc_typeck/src/collect.rs | 14 +++--- compiler/rustc_typeck/src/collect/type_of.rs | 4 +- .../clippy_lints/src/new_without_default.rs | 2 +- .../clippy_lints/src/partialeq_ne_impl.rs | 2 +- .../clippy/clippy_lints/src/serde_api.rs | 2 +- 16 files changed, 61 insertions(+), 56 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 8ae07982f6f..7fc53d264a6 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -410,7 +410,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ImplPolarity::Positive => ImplPolarity::Positive, ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(s)), }; - hir::ItemKind::Impl(hir::Impl { + hir::ItemKind::Impl(self.arena.alloc(hir::Impl { unsafety: self.lower_unsafety(unsafety), polarity, defaultness, @@ -420,7 +420,7 @@ impl<'hir> LoweringContext<'_, 'hir> { of_trait: trait_ref, self_ty: lowered_ty, items: new_impl_items, - }) + })) } ItemKind::Trait(box Trait { is_auto, @@ -1226,7 +1226,7 @@ impl<'hir> LoweringContext<'_, 'hir> { id: NodeId, kind: FnDeclKind, is_async: Option, - ) -> (hir::Generics<'hir>, hir::FnSig<'hir>) { + ) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) { let header = self.lower_fn_header(sig.header); let (generics, decl) = self.add_implicit_generics(generics, id, |this, idty| { this.lower_fn_decl(&sig.decl, Some((id, idty)), kind, is_async) @@ -1349,7 +1349,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &mut self, generics: &Generics, itctx: ImplTraitContext<'_, 'hir>, - ) -> hir::Generics<'hir> { + ) -> &'hir hir::Generics<'hir> { let generics_ctor = self.lower_generics_mut(generics, itctx); generics_ctor.into_generics(self.arena) } @@ -1419,11 +1419,11 @@ pub(super) struct GenericsCtor<'hir> { } impl<'hir> GenericsCtor<'hir> { - pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> hir::Generics<'hir> { - hir::Generics { + pub(super) fn into_generics(self, arena: &'hir Arena<'hir>) -> &'hir hir::Generics<'hir> { + arena.alloc(hir::Generics { params: arena.alloc_from_iter(self.params), where_clause: self.where_clause, span: self.span, - } + }) } } diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index de4f8e04b16..a5e12408087 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -719,7 +719,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { generics: &Generics, parent_node_id: NodeId, f: impl FnOnce(&mut Self, &mut Vec>) -> T, - ) -> (hir::Generics<'hir>, T) { + ) -> (&'hir hir::Generics<'hir>, T) { let mut impl_trait_defs = Vec::new(); let mut lowered_generics = self.lower_generics_mut( generics, @@ -1383,11 +1383,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { debug!("lower_opaque_impl_trait: lifetime_defs={:#?}", lifetime_defs); let opaque_ty_item = hir::OpaqueTy { - generics: hir::Generics { + generics: self.arena.alloc(hir::Generics { params: lifetime_defs, where_clause: hir::WhereClause { predicates: &[], span: lctx.lower_span(span) }, span: lctx.lower_span(span), - }, + }), bounds: hir_bounds, origin, }; @@ -1715,11 +1715,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params); let opaque_ty_item = hir::OpaqueTy { - generics: hir::Generics { + generics: this.arena.alloc(hir::Generics { params: generic_params, where_clause: hir::WhereClause { predicates: &[], span: this.lower_span(span) }, span: this.lower_span(span), - }, + }), bounds: arena_vec![this; future_bound], origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id), }; diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index 27ec4619064..5d1314ebb48 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -15,11 +15,13 @@ macro_rules! arena_types { [] block: rustc_hir::Block<'tcx>, [] bare_fn_ty: rustc_hir::BareFnTy<'tcx>, [] body: rustc_hir::Body<'tcx>, + [] generics: rustc_hir::Generics<'tcx>, [] generic_arg: rustc_hir::GenericArg<'tcx>, [] generic_args: rustc_hir::GenericArgs<'tcx>, [] generic_bound: rustc_hir::GenericBound<'tcx>, [] generic_param: rustc_hir::GenericParam<'tcx>, [] expr: rustc_hir::Expr<'tcx>, + [] impl_: rustc_hir::Impl<'tcx>, [] let_expr: rustc_hir::Let<'tcx>, [] expr_field: rustc_hir::ExprField<'tcx>, [] pat_field: rustc_hir::PatField<'tcx>, diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 7d9203b70ab..b723312f094 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -572,12 +572,13 @@ pub struct Generics<'hir> { } impl<'hir> Generics<'hir> { - pub const fn empty() -> Generics<'hir> { - Generics { + pub const fn empty() -> &'hir Generics<'hir> { + const NOPE: Generics<'_> = Generics { params: &[], where_clause: WhereClause { predicates: &[], span: DUMMY_SP }, span: DUMMY_SP, - } + }; + &NOPE } pub fn get_named(&self, name: Symbol) -> Option<&GenericParam<'_>> { @@ -2075,7 +2076,7 @@ impl TraitItemId { pub struct TraitItem<'hir> { pub ident: Ident, pub def_id: LocalDefId, - pub generics: Generics<'hir>, + pub generics: &'hir Generics<'hir>, pub kind: TraitItemKind<'hir>, pub span: Span, } @@ -2135,7 +2136,7 @@ impl ImplItemId { pub struct ImplItem<'hir> { pub ident: Ident, pub def_id: LocalDefId, - pub generics: Generics<'hir>, + pub generics: &'hir Generics<'hir>, pub kind: ImplItemKind<'hir>, pub span: Span, pub vis_span: Span, @@ -2340,7 +2341,7 @@ pub struct BareFnTy<'hir> { #[derive(Debug, HashStable_Generic)] pub struct OpaqueTy<'hir> { - pub generics: Generics<'hir>, + pub generics: &'hir Generics<'hir>, pub bounds: GenericBounds<'hir>, pub origin: OpaqueTyOrigin, } @@ -2814,7 +2815,7 @@ pub enum ItemKind<'hir> { /// A `const` item. Const(&'hir Ty<'hir>, BodyId), /// A function declaration. - Fn(FnSig<'hir>, Generics<'hir>, BodyId), + Fn(FnSig<'hir>, &'hir Generics<'hir>, BodyId), /// A MBE macro definition (`macro_rules!` or `macro`). Macro(ast::MacroDef, MacroKind), /// A module. @@ -2824,22 +2825,22 @@ pub enum ItemKind<'hir> { /// Module-level inline assembly (from `global_asm!`). GlobalAsm(&'hir InlineAsm<'hir>), /// A type alias, e.g., `type Foo = Bar`. - TyAlias(&'hir Ty<'hir>, Generics<'hir>), + TyAlias(&'hir Ty<'hir>, &'hir Generics<'hir>), /// An opaque `impl Trait` type alias, e.g., `type Foo = impl Bar;`. OpaqueTy(OpaqueTy<'hir>), /// An enum definition, e.g., `enum Foo {C, D}`. - Enum(EnumDef<'hir>, Generics<'hir>), + Enum(EnumDef<'hir>, &'hir Generics<'hir>), /// A struct definition, e.g., `struct Foo {x: A}`. - Struct(VariantData<'hir>, Generics<'hir>), + Struct(VariantData<'hir>, &'hir Generics<'hir>), /// A union definition, e.g., `union Foo {x: A, y: B}`. - Union(VariantData<'hir>, Generics<'hir>), + Union(VariantData<'hir>, &'hir Generics<'hir>), /// A trait definition. - Trait(IsAuto, Unsafety, Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemRef]), + Trait(IsAuto, Unsafety, &'hir Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemRef]), /// A trait alias. - TraitAlias(Generics<'hir>, GenericBounds<'hir>), + TraitAlias(&'hir Generics<'hir>, GenericBounds<'hir>), /// An implementation, e.g., `impl Trait for Foo { .. }`. - Impl(Impl<'hir>), + Impl(&'hir Impl<'hir>), } #[derive(Debug, HashStable_Generic)] @@ -2851,7 +2852,7 @@ pub struct Impl<'hir> { // decoding as `Span`s cannot be decoded when a `Session` is not available. pub defaultness_span: Option, pub constness: Constness, - pub generics: Generics<'hir>, + pub generics: &'hir Generics<'hir>, /// The trait being implemented, if any. pub of_trait: Option>, @@ -2993,7 +2994,7 @@ impl ForeignItem<'_> { #[derive(Debug, HashStable_Generic)] pub enum ForeignItemKind<'hir> { /// A foreign function. - Fn(&'hir FnDecl<'hir>, &'hir [Ident], Generics<'hir>), + Fn(&'hir FnDecl<'hir>, &'hir [Ident], &'hir Generics<'hir>), /// A foreign static item (`static ext: u8`). Static(&'hir Ty<'hir>, Mutability), /// A foreign type. @@ -3326,9 +3327,11 @@ mod size_asserts { rustc_data_structures::static_assert_size!(super::QPath<'static>, 24); rustc_data_structures::static_assert_size!(super::Ty<'static>, 72); rustc_data_structures::static_assert_size!(super::GenericBound<'_>, 48); + rustc_data_structures::static_assert_size!(super::Generics<'static>, 48); + rustc_data_structures::static_assert_size!(super::Impl<'static>, 80); - rustc_data_structures::static_assert_size!(super::Item<'static>, 160); - rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 128); - rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 120); - rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 112); + rustc_data_structures::static_assert_size!(super::Item<'static>, 80); + rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 88); + rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 80); + rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 72); } diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 59bd46ae353..002fa607416 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -619,7 +619,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { visitor.visit_generics(generics); walk_list!(visitor, visit_trait_ref, of_trait); visitor.visit_ty(self_ty); - walk_list!(visitor, visit_impl_item_ref, items); + walk_list!(visitor, visit_impl_item_ref, *items); } ItemKind::Struct(ref struct_definition, ref generics) | ItemKind::Union(ref struct_definition, ref generics) => { diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 87ff9457783..3978579c60b 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -626,8 +626,8 @@ impl<'a> State<'a> { items, }) => { self.head(""); - self.print_defaultness(defaultness); - self.print_unsafety(unsafety); + self.print_defaultness(*defaultness); + self.print_unsafety(*unsafety); self.word_nbsp("impl"); if !generics.params.is_empty() { @@ -635,7 +635,7 @@ impl<'a> State<'a> { self.space(); } - if constness == hir::Constness::Const { + if *constness == hir::Constness::Const { self.word_nbsp("const"); } @@ -655,7 +655,7 @@ impl<'a> State<'a> { self.space(); self.bopen(); self.print_inner_attributes(attrs); - for impl_item in items { + for impl_item in *items { self.ann.nested(self, Nested::ImplItem(impl_item.id)); } self.bclose(item.span); diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 68658e2616e..e097ab9d7fd 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1199,8 +1199,8 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems { }); } } - hir::ItemKind::Impl(hir::Impl { ref generics, items, .. }) => { - for it in items { + hir::ItemKind::Impl(hir::Impl { generics, items, .. }) => { + for it in *items { if let hir::AssocItemKind::Fn { .. } = it.kind { if let Some(no_mangle_attr) = cx .sess() diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index ed49eebd16d..b46ea955a3a 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1462,8 +1462,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { })) } hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => { - self.tables.impl_defaultness.set(def_id.index, defaultness); - self.tables.impl_constness.set(def_id.index, constness); + self.tables.impl_defaultness.set(def_id.index, *defaultness); + self.tables.impl_constness.set(def_id.index, *constness); let trait_ref = self.tcx.impl_trait_ref(def_id); if let Some(trait_ref) = trait_ref { diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index b661f6f9d72..3cd18390285 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -512,7 +512,7 @@ impl<'v, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'tcx> { if of_trait.is_some() { self.worklist.push(item.def_id); } - for impl_item_ref in items { + for impl_item_ref in *items { let impl_item = self.tcx.hir().impl_item(impl_item_ref.id); if of_trait.is_some() || has_allow_dead_code_or_lang_attr(self.tcx, impl_item.hir_id()) diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 35a858cb86c..10dc587be6e 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -737,7 +737,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { } } - for impl_item_ref in items { + for impl_item_ref in *items { let impl_item = self.tcx.associated_item(impl_item_ref.id.def_id); if let Some(def_id) = impl_item.trait_item_def_id { diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 894ff0f17f8..a5243bf8ac3 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -86,7 +86,7 @@ impl ForLifetimeSpanType { } } -impl<'tcx> Into> for &'tcx hir::Generics<'tcx> { +impl<'tcx> Into> for &&'tcx hir::Generics<'tcx> { fn into(self) -> MissingLifetimeSpot<'tcx> { MissingLifetimeSpot::Generics(self) } diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 0ccc2b6b182..e36eebcf931 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -2200,16 +2200,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP let icx = ItemCtxt::new(tcx, def_id); - const NO_GENERICS: &hir::Generics<'_> = &hir::Generics::empty(); + const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty(); // We use an `IndexSet` to preserves order of insertion. // Preserving the order of insertion is important here so as not to break UI tests. let mut predicates: FxIndexSet<(ty::Predicate<'_>, Span)> = FxIndexSet::default(); let ast_generics = match node { - Node::TraitItem(item) => &item.generics, + Node::TraitItem(item) => item.generics, - Node::ImplItem(item) => &item.generics, + Node::ImplItem(item) => item.generics, Node::Item(item) => { match item.kind { @@ -2223,15 +2223,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP | ItemKind::TyAlias(_, ref generics) | ItemKind::Enum(_, ref generics) | ItemKind::Struct(_, ref generics) - | ItemKind::Union(_, ref generics) => generics, + | ItemKind::Union(_, ref generics) => *generics, ItemKind::Trait(_, _, ref generics, ..) => { is_trait = Some(ty::TraitRef::identity(tcx, def_id)); - generics + *generics } ItemKind::TraitAlias(ref generics, _) => { is_trait = Some(ty::TraitRef::identity(tcx, def_id)); - generics + *generics } ItemKind::OpaqueTy(OpaqueTy { origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..), @@ -2268,7 +2268,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP Node::ForeignItem(item) => match item.kind { ForeignItemKind::Static(..) => NO_GENERICS, - ForeignItemKind::Fn(_, _, ref generics) => generics, + ForeignItemKind::Fn(_, _, ref generics) => *generics, ForeignItemKind::Type => NO_GENERICS, }, diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index fa06ec09fce..495b8d3b4ee 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -337,8 +337,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { icx.to_ty(ty) } } - ItemKind::TyAlias(self_ty, _) - | ItemKind::Impl(hir::Impl { self_ty, .. }) => icx.to_ty(self_ty), + ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty), + ItemKind::Impl(hir::Impl { self_ty, .. }) => icx.to_ty(*self_ty), ItemKind::Fn(..) => { let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id()); tcx.mk_fn_def(def_id.to_def_id(), substs) diff --git a/src/tools/clippy/clippy_lints/src/new_without_default.rs b/src/tools/clippy/clippy_lints/src/new_without_default.rs index 9419056be14..96c00c205ff 100644 --- a/src/tools/clippy/clippy_lints/src/new_without_default.rs +++ b/src/tools/clippy/clippy_lints/src/new_without_default.rs @@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault { .. }) = item.kind { - for assoc_item in items { + for assoc_item in *items { if assoc_item.kind == (hir::AssocItemKind::Fn { has_self: false }) { let impl_item = cx.tcx.hir().impl_item(assoc_item.id); if in_external_macro(cx.sess(), impl_item.span) { diff --git a/src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs b/src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs index e827cdaae87..1469cb434c0 100644 --- a/src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs +++ b/src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs @@ -42,7 +42,7 @@ impl<'tcx> LateLintPass<'tcx> for PartialEqNeImpl { if let Some(eq_trait) = cx.tcx.lang_items().eq_trait(); if trait_ref.path.res.def_id() == eq_trait; then { - for impl_item in impl_items { + for impl_item in *impl_items { if impl_item.ident.name == sym::ne { span_lint_hir( cx, diff --git a/src/tools/clippy/clippy_lints/src/serde_api.rs b/src/tools/clippy/clippy_lints/src/serde_api.rs index 398e2c200de..fc1c2af9257 100644 --- a/src/tools/clippy/clippy_lints/src/serde_api.rs +++ b/src/tools/clippy/clippy_lints/src/serde_api.rs @@ -36,7 +36,7 @@ impl<'tcx> LateLintPass<'tcx> for SerdeApi { if did == visit_did { let mut seen_str = None; let mut seen_string = None; - for item in items { + for item in *items { match item.ident.as_str() { "visit_str" => seen_str = Some(item.span), "visit_string" => seen_string = Some(item.span),