Box HIR Generics and Impl.

This commit is contained in:
Camille GILLOT 2022-02-05 15:26:49 +01:00
parent 76d4862fdd
commit 71b4e2d852
16 changed files with 61 additions and 56 deletions

View File

@ -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<NodeId>,
) -> (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,
}
})
}
}

View File

@ -719,7 +719,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
generics: &Generics,
parent_node_id: NodeId,
f: impl FnOnce(&mut Self, &mut Vec<hir::GenericParam<'hir>>) -> 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),
};

View File

@ -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>,

View File

@ -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<u8>`.
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<A, B> {C<A>, D<B>}`.
Enum(EnumDef<'hir>, Generics<'hir>),
Enum(EnumDef<'hir>, &'hir Generics<'hir>),
/// A struct definition, e.g., `struct Foo<A> {x: A}`.
Struct(VariantData<'hir>, Generics<'hir>),
Struct(VariantData<'hir>, &'hir Generics<'hir>),
/// A union definition, e.g., `union Foo<A, B> {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<A> 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<Span>,
pub constness: Constness,
pub generics: Generics<'hir>,
pub generics: &'hir Generics<'hir>,
/// The trait being implemented, if any.
pub of_trait: Option<TraitRef<'hir>>,
@ -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);
}

View File

@ -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) => {

View File

@ -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);

View File

@ -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()

View File

@ -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 {

View File

@ -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())

View File

@ -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 {

View File

@ -86,7 +86,7 @@ impl ForLifetimeSpanType {
}
}
impl<'tcx> Into<MissingLifetimeSpot<'tcx>> for &'tcx hir::Generics<'tcx> {
impl<'tcx> Into<MissingLifetimeSpot<'tcx>> for &&'tcx hir::Generics<'tcx> {
fn into(self) -> MissingLifetimeSpot<'tcx> {
MissingLifetimeSpot::Generics(self)
}

View File

@ -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,
},

View File

@ -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)

View File

@ -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) {

View File

@ -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,

View File

@ -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),