From 064a351953c0491eaa3eff8c9359bc0d2aed1129 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 23 Dec 2020 10:32:00 +0100 Subject: [PATCH 01/13] Infallible version of def_span. --- compiler/rustc_middle/src/hir/map/mod.rs | 69 +++++++++++++----------- compiler/rustc_middle/src/hir/mod.rs | 2 + compiler/rustc_ty_utils/src/ty.rs | 5 -- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 06bb1347dc1de..e1fcc9be66417 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -848,50 +848,55 @@ impl<'hir> Map<'hir> { /// Gets the span of the definition of the specified HIR node. /// This is used by `tcx.get_span` pub fn span(&self, hir_id: HirId) -> Span { - match self.find_entry(hir_id).map(|entry| entry.node) { - Some(Node::Param(param)) => param.span, - Some(Node::Item(item)) => match &item.kind { + self.opt_span(hir_id) + .unwrap_or_else(|| bug!("hir::map::Map::span: id not in map: {:?}", hir_id)) + } + + pub fn opt_span(&self, hir_id: HirId) -> Option { + let span = match self.find_entry(hir_id)?.node { + Node::Param(param) => param.span, + Node::Item(item) => match &item.kind { ItemKind::Fn(sig, _, _) => sig.span, _ => item.span, }, - Some(Node::ForeignItem(foreign_item)) => foreign_item.span, - Some(Node::TraitItem(trait_item)) => match &trait_item.kind { + Node::ForeignItem(foreign_item) => foreign_item.span, + Node::TraitItem(trait_item) => match &trait_item.kind { TraitItemKind::Fn(sig, _) => sig.span, _ => trait_item.span, }, - Some(Node::ImplItem(impl_item)) => match &impl_item.kind { + Node::ImplItem(impl_item) => match &impl_item.kind { ImplItemKind::Fn(sig, _) => sig.span, _ => impl_item.span, }, - Some(Node::Variant(variant)) => variant.span, - Some(Node::Field(field)) => field.span, - Some(Node::AnonConst(constant)) => self.body(constant.body).value.span, - Some(Node::Expr(expr)) => expr.span, - Some(Node::Stmt(stmt)) => stmt.span, - Some(Node::PathSegment(seg)) => seg.ident.span, - Some(Node::Ty(ty)) => ty.span, - Some(Node::TraitRef(tr)) => tr.path.span, - Some(Node::Binding(pat)) => pat.span, - Some(Node::Pat(pat)) => pat.span, - Some(Node::Arm(arm)) => arm.span, - Some(Node::Block(block)) => block.span, - Some(Node::Ctor(..)) => match self.find(self.get_parent_node(hir_id)) { - Some(Node::Item(item)) => item.span, - Some(Node::Variant(variant)) => variant.span, + Node::Variant(variant) => variant.span, + Node::Field(field) => field.span, + Node::AnonConst(constant) => self.body(constant.body).value.span, + Node::Expr(expr) => expr.span, + Node::Stmt(stmt) => stmt.span, + Node::PathSegment(seg) => seg.ident.span, + Node::Ty(ty) => ty.span, + Node::TraitRef(tr) => tr.path.span, + Node::Binding(pat) => pat.span, + Node::Pat(pat) => pat.span, + Node::Arm(arm) => arm.span, + Node::Block(block) => block.span, + Node::Ctor(..) => match self.find(self.get_parent_node(hir_id))? { + Node::Item(item) => item.span, + Node::Variant(variant) => variant.span, _ => unreachable!(), }, - Some(Node::Lifetime(lifetime)) => lifetime.span, - Some(Node::GenericParam(param)) => param.span, - Some(Node::Visibility(&Spanned { + Node::Lifetime(lifetime) => lifetime.span, + Node::GenericParam(param) => param.span, + Node::Visibility(&Spanned { node: VisibilityKind::Restricted { ref path, .. }, .. - })) => path.span, - Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v), - Some(Node::Local(local)) => local.span, - Some(Node::MacroDef(macro_def)) => macro_def.span, - Some(Node::Crate(item)) => item.span, - None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id), - } + }) => path.span, + Node::Visibility(v) => bug!("unexpected Visibility {:?}", v), + Node::Local(local) => local.span, + Node::MacroDef(macro_def) => macro_def.span, + Node::Crate(item) => item.span, + }; + Some(span) } /// Like `hir.span()`, but includes the body of function items @@ -907,7 +912,7 @@ impl<'hir> Map<'hir> { } pub fn span_if_local(&self, id: DefId) -> Option { - id.as_local().map(|id| self.span(self.local_def_id_to_hir_id(id))) + id.as_local().and_then(|id| self.opt_span(self.local_def_id_to_hir_id(id))) } pub fn res_span(&self, res: Res) -> Option { diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index ae3b30217cc4a..dd4bfdd3e4a54 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -15,6 +15,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE}; use rustc_hir::*; use rustc_index::vec::IndexVec; +use rustc_span::DUMMY_SP; pub struct Owner<'tcx> { parent: HirId, @@ -77,6 +78,7 @@ pub fn provide(providers: &mut Providers) { }; providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature; providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref(); + providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP); providers.fn_arg_names = |tcx, id| { let hir = tcx.hir(); let hir_id = hir.local_def_id_to_hir_id(id.expect_local()); diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index bd1d9cc895fb0..77aa441340912 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -218,10 +218,6 @@ fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssociatedItems<'_> { ty::AssociatedItems::new(items) } -fn def_span(tcx: TyCtxt<'_>, def_id: DefId) -> Span { - tcx.hir().span_if_local(def_id).unwrap() -} - fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option { tcx.hir().get_if_local(def_id).and_then(|node| node.ident()).map(|ident| ident.span) } @@ -495,7 +491,6 @@ pub fn provide(providers: &mut ty::query::Providers) { associated_item_def_ids, associated_items, adt_sized_constraint, - def_span, def_ident_span, param_env, param_env_reveal_all_normalized, From 0b6c9e9f8853c30640e08088c2fae681afaa4b4f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 15 Nov 2020 18:02:00 +0100 Subject: [PATCH 02/13] Encode DefKind directly. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 78 ++----- compiler/rustc_metadata/src/rmeta/encoder.rs | 221 +++++++++++-------- compiler/rustc_metadata/src/rmeta/mod.rs | 3 +- 3 files changed, 160 insertions(+), 142 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 115569fc60d9f..e3c3539079857 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -618,43 +618,6 @@ impl MetadataBlob { } } -impl EntryKind { - fn def_kind(&self) -> DefKind { - match *self { - EntryKind::AnonConst(..) => DefKind::AnonConst, - EntryKind::Const(..) => DefKind::Const, - EntryKind::AssocConst(..) => DefKind::AssocConst, - EntryKind::ImmStatic - | EntryKind::MutStatic - | EntryKind::ForeignImmStatic - | EntryKind::ForeignMutStatic => DefKind::Static, - EntryKind::Struct(_, _) => DefKind::Struct, - EntryKind::Union(_, _) => DefKind::Union, - EntryKind::Fn(_) | EntryKind::ForeignFn(_) => DefKind::Fn, - EntryKind::AssocFn(_) => DefKind::AssocFn, - EntryKind::Type => DefKind::TyAlias, - EntryKind::TypeParam => DefKind::TyParam, - EntryKind::ConstParam => DefKind::ConstParam, - EntryKind::OpaqueTy => DefKind::OpaqueTy, - EntryKind::AssocType(_) => DefKind::AssocTy, - EntryKind::Mod(_) => DefKind::Mod, - EntryKind::Variant(_) => DefKind::Variant, - EntryKind::Trait(_) => DefKind::Trait, - EntryKind::TraitAlias => DefKind::TraitAlias, - EntryKind::Enum(..) => DefKind::Enum, - EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang), - EntryKind::ProcMacro(kind) => DefKind::Macro(kind), - EntryKind::ForeignType => DefKind::ForeignTy, - EntryKind::Impl(_) => DefKind::Impl, - EntryKind::Closure => DefKind::Closure, - EntryKind::ForeignMod => DefKind::ForeignMod, - EntryKind::GlobalAsm => DefKind::GlobalAsm, - EntryKind::Field => DefKind::Field, - EntryKind::Generator(_) => DefKind::Generator, - } - } -} - impl CrateRoot<'_> { crate fn is_proc_macro_crate(&self) -> bool { self.proc_macro_data.is_some() @@ -685,21 +648,6 @@ impl CrateRoot<'_> { } impl<'a, 'tcx> CrateMetadataRef<'a> { - fn maybe_kind(&self, item_id: DefIndex) -> Option { - self.root.tables.kind.get(self, item_id).map(|k| k.decode(self)) - } - - fn kind(&self, item_id: DefIndex) -> EntryKind { - self.maybe_kind(item_id).unwrap_or_else(|| { - bug!( - "CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}", - item_id, - self.root.name, - self.cnum, - ) - }) - } - fn raw_proc_macro(&self, id: DefIndex) -> &ProcMacro { // DefIndex's in root.proc_macro_data have a one-to-one correspondence // with items in 'raw_proc_macros'. @@ -736,8 +684,30 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { self.try_item_ident(item_index, sess).unwrap() } - fn def_kind(&self, index: DefIndex) -> DefKind { - self.kind(index).def_kind() + fn maybe_kind(&self, item_id: DefIndex) -> Option { + self.root.tables.kind.get(self, item_id).map(|k| k.decode(self)) + } + + fn kind(&self, item_id: DefIndex) -> EntryKind { + self.maybe_kind(item_id).unwrap_or_else(|| { + bug!( + "CrateMetadata::kind({:?}): id not found, in crate {:?} with number {}", + item_id, + self.root.name, + self.cnum, + ) + }) + } + + fn def_kind(&self, item_id: DefIndex) -> DefKind { + self.root.tables.def_kind.get(self, item_id).map(|k| k.decode(self)).unwrap_or_else(|| { + bug!( + "CrateMetadata::def_kind({:?}): id not found, in crate {:?} with number {}", + item_id, + self.root.name, + self.cnum, + ) + }) } fn get_span(&self, index: DefIndex, sess: &Session) -> Span { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 5e2674254b295..600f521318b92 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -7,7 +7,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::sync::{join, Lrc}; use rustc_hir as hir; -use rustc_hir::def::CtorKind; +use rustc_hir::def::{CtorKind, DefKind}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::definitions::DefPathData; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; @@ -739,6 +739,7 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; + record!(self.tables.def_kind[def_id] <- DefKind::Variant); record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); @@ -779,6 +780,7 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; + record!(self.tables.def_kind[def_id] <- DefKind::Variant); record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); @@ -831,6 +833,7 @@ impl EncodeContext<'a, 'tcx> { expansion: tcx.hir().definitions().expansion_that_defined(local_def_id), }; + record!(self.tables.def_kind[def_id] <- DefKind::Mod); record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); @@ -862,6 +865,7 @@ impl EncodeContext<'a, 'tcx> { let variant_id = tcx.hir().local_def_id_to_hir_id(variant.def_id.expect_local()); let variant_data = tcx.hir().expect_variant_data(variant_id); + record!(self.tables.def_kind[def_id] <- DefKind::Field); record!(self.tables.kind[def_id] <- EntryKind::Field); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); @@ -888,6 +892,7 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; + record!(self.tables.def_kind[def_id] <- DefKind::Struct); record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr)); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); @@ -955,29 +960,26 @@ impl EncodeContext<'a, 'tcx> { hir::Defaultness::Final => span_bug!(ast_item.span, "traits cannot have final items"), }; - record!(self.tables.kind[def_id] <- match trait_item.kind { + match trait_item.kind { ty::AssocKind::Const => { let rendered = rustc_hir_pretty::to_string( &(&self.tcx.hir() as &dyn intravisit::Map<'_>), - |s| s.print_trait_item(ast_item) + |s| s.print_trait_item(ast_item), ); let rendered_const = self.lazy(RenderedConst(rendered)); - EntryKind::AssocConst( + record!(self.tables.def_kind[def_id] <- DefKind::AssocConst); + record!(self.tables.kind[def_id] <- EntryKind::AssocConst( container, Default::default(), rendered_const, - ) + )); } ty::AssocKind::Fn => { let fn_data = if let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind { let param_names = match *m { - hir::TraitFn::Required(ref names) => { - self.encode_fn_param_names(names) - } - hir::TraitFn::Provided(body) => { - self.encode_fn_param_names_for_body(body) - } + hir::TraitFn::Required(ref names) => self.encode_fn_param_names(names), + hir::TraitFn::Provided(body) => self.encode_fn_param_names_for_body(body), }; FnData { asyncness: m_sig.header.asyncness, @@ -987,17 +989,19 @@ impl EncodeContext<'a, 'tcx> { } else { bug!() }; - EntryKind::AssocFn(self.lazy(AssocFnData { + record!(self.tables.def_kind[def_id] <- DefKind::AssocFn); + record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData { fn_data, container, has_self: trait_item.fn_has_self_parameter, - })) + }))); } ty::AssocKind::Type => { self.encode_explicit_item_bounds(def_id); - EntryKind::AssocType(container) + record!(self.tables.def_kind[def_id] <- DefKind::AssocTy); + record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); } - }); + } record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- ast_item.span); record!(self.tables.attributes[def_id] <- ast_item.attrs); @@ -1068,15 +1072,17 @@ impl EncodeContext<'a, 'tcx> { } }; - record!(self.tables.kind[def_id] <- match impl_item.kind { + match impl_item.kind { ty::AssocKind::Const => { if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind { let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id); - EntryKind::AssocConst( + record!(self.tables.def_kind[def_id] <- DefKind::AssocConst); + record!(self.tables.kind[def_id] <- EntryKind::AssocConst( container, qualifs, self.encode_rendered_const_for_body(body_id)) + ); } else { bug!() } @@ -1091,14 +1097,18 @@ impl EncodeContext<'a, 'tcx> { } else { bug!() }; - EntryKind::AssocFn(self.lazy(AssocFnData { + record!(self.tables.def_kind[def_id] <- DefKind::AssocFn); + record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData { fn_data, container, has_self: impl_item.fn_has_self_parameter, - })) + }))); } - ty::AssocKind::Type => EntryKind::AssocType(container) - }); + ty::AssocKind::Type => { + record!(self.tables.def_kind[def_id] <- DefKind::AssocTy); + record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); + } + } record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- ast_item.span); record!(self.tables.attributes[def_id] <- ast_item.attrs); @@ -1235,14 +1245,18 @@ impl EncodeContext<'a, 'tcx> { self.encode_ident_span(def_id, item.ident); - record!(self.tables.kind[def_id] <- match item.kind { - hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic, - hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic, + let (def_kind, entry_kind) = match item.kind { + hir::ItemKind::Static(_, hir::Mutability::Mut, _) => { + (DefKind::Static, EntryKind::MutStatic) + } + hir::ItemKind::Static(_, hir::Mutability::Not, _) => { + (DefKind::Static, EntryKind::ImmStatic) + } hir::ItemKind::Const(_, body_id) => { let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id); - EntryKind::Const( - qualifs, - self.encode_rendered_const_for_body(body_id) + ( + DefKind::Const, + EntryKind::Const(qualifs, self.encode_rendered_const_for_body(body_id)), ) } hir::ItemKind::Fn(ref sig, .., body) => { @@ -1252,19 +1266,21 @@ impl EncodeContext<'a, 'tcx> { param_names: self.encode_fn_param_names_for_body(body), }; - EntryKind::Fn(self.lazy(data)) + (DefKind::Fn, EntryKind::Fn(self.lazy(data))) } hir::ItemKind::Mod(ref m) => { return self.encode_info_for_mod(item.hir_id, m, &item.attrs); } - hir::ItemKind::ForeignMod { .. } => EntryKind::ForeignMod, - hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm, - hir::ItemKind::TyAlias(..) => EntryKind::Type, + hir::ItemKind::ForeignMod { .. } => (DefKind::ForeignMod, EntryKind::ForeignMod), + hir::ItemKind::GlobalAsm(..) => (DefKind::GlobalAsm, EntryKind::GlobalAsm), + hir::ItemKind::TyAlias(..) => (DefKind::TyAlias, EntryKind::Type), hir::ItemKind::OpaqueTy(..) => { self.encode_explicit_item_bounds(def_id); - EntryKind::OpaqueTy + (DefKind::OpaqueTy, EntryKind::OpaqueTy) + } + hir::ItemKind::Enum(..) => { + (DefKind::Enum, EntryKind::Enum(self.tcx.adt_def(def_id).repr)) } - hir::ItemKind::Enum(..) => EntryKind::Enum(self.tcx.adt_def(def_id).repr), hir::ItemKind::Struct(ref struct_def, _) => { let adt_def = self.tcx.adt_def(def_id); let variant = adt_def.non_enum_variant(); @@ -1272,63 +1288,65 @@ impl EncodeContext<'a, 'tcx> { // Encode def_ids for each field and method // for methods, write all the stuff get_trait_method // needs to know - let ctor = struct_def.ctor_hir_id().map(|ctor_hir_id| { - self.tcx.hir().local_def_id(ctor_hir_id).local_def_index - }); - - EntryKind::Struct(self.lazy(VariantData { - ctor_kind: variant.ctor_kind, - discr: variant.discr, - ctor, - is_non_exhaustive: variant.is_field_list_non_exhaustive(), - }), adt_def.repr) + let ctor = struct_def + .ctor_hir_id() + .map(|ctor_hir_id| self.tcx.hir().local_def_id(ctor_hir_id).local_def_index); + + let ek = EntryKind::Struct( + self.lazy(VariantData { + ctor_kind: variant.ctor_kind, + discr: variant.discr, + ctor, + is_non_exhaustive: variant.is_field_list_non_exhaustive(), + }), + adt_def.repr, + ); + (DefKind::Struct, ek) } hir::ItemKind::Union(..) => { let adt_def = self.tcx.adt_def(def_id); let variant = adt_def.non_enum_variant(); - EntryKind::Union(self.lazy(VariantData { - ctor_kind: variant.ctor_kind, - discr: variant.discr, - ctor: None, - is_non_exhaustive: variant.is_field_list_non_exhaustive(), - }), adt_def.repr) + let ek = EntryKind::Union( + self.lazy(VariantData { + ctor_kind: variant.ctor_kind, + discr: variant.discr, + ctor: None, + is_non_exhaustive: variant.is_field_list_non_exhaustive(), + }), + adt_def.repr, + ); + (DefKind::Union, ek) } hir::ItemKind::Impl(hir::Impl { defaultness, .. }) => { let trait_ref = self.tcx.impl_trait_ref(def_id); let polarity = self.tcx.impl_polarity(def_id); let parent = if let Some(trait_ref) = trait_ref { let trait_def = self.tcx.trait_def(trait_ref.def_id); - trait_def.ancestors(self.tcx, def_id).ok() - .and_then(|mut an| an.nth(1).and_then(|node| { - match node { - specialization_graph::Node::Impl(parent) => Some(parent), - _ => None, - } - })) + trait_def.ancestors(self.tcx, def_id).ok().and_then(|mut an| { + an.nth(1).and_then(|node| match node { + specialization_graph::Node::Impl(parent) => Some(parent), + _ => None, + }) + }) } else { None }; // if this is an impl of `CoerceUnsized`, create its // "unsized info", else just store None - let coerce_unsized_info = - trait_ref.and_then(|t| { - if Some(t.def_id) == self.tcx.lang_items().coerce_unsized_trait() { - Some(self.tcx.at(item.span).coerce_unsized_info(def_id)) - } else { - None - } - }); + let coerce_unsized_info = trait_ref.and_then(|t| { + if Some(t.def_id) == self.tcx.lang_items().coerce_unsized_trait() { + Some(self.tcx.at(item.span).coerce_unsized_info(def_id)) + } else { + None + } + }); - let data = ImplData { - polarity, - defaultness, - parent_impl: parent, - coerce_unsized_info, - }; + let data = + ImplData { polarity, defaultness, parent_impl: parent, coerce_unsized_info }; - EntryKind::Impl(self.lazy(data)) + (DefKind::Impl, EntryKind::Impl(self.lazy(data))) } hir::ItemKind::Trait(..) => { let trait_def = self.tcx.trait_def(def_id); @@ -1340,12 +1358,15 @@ impl EncodeContext<'a, 'tcx> { specialization_kind: trait_def.specialization_kind, }; - EntryKind::Trait(self.lazy(data)) + (DefKind::Trait, EntryKind::Trait(self.lazy(data))) } - hir::ItemKind::TraitAlias(..) => EntryKind::TraitAlias, - hir::ItemKind::ExternCrate(_) | - hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item), - }); + hir::ItemKind::TraitAlias(..) => (DefKind::TraitAlias, EntryKind::TraitAlias), + hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => { + bug!("cannot encode info for item {:?}", item) + } + }; + record!(self.tables.def_kind[def_id] <- def_kind); + record!(self.tables.kind[def_id] <- entry_kind); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.attributes[def_id] <- item.attrs); @@ -1465,6 +1486,7 @@ impl EncodeContext<'a, 'tcx> { /// Serialize the text of exported macros fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) { let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id(); + record!(self.tables.def_kind[def_id] <- DefKind::Macro(MacroKind::Bang)); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- macro_def.span); @@ -1474,7 +1496,14 @@ impl EncodeContext<'a, 'tcx> { self.encode_deprecation(def_id); } - fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) { + fn encode_info_for_generic_param( + &mut self, + def_id: DefId, + def_kind: DefKind, + kind: EntryKind, + encode_type: bool, + ) { + record!(self.tables.def_kind[def_id] <- def_kind); record!(self.tables.kind[def_id] <- kind); record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); if encode_type { @@ -1490,16 +1519,20 @@ impl EncodeContext<'a, 'tcx> { let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); let ty = self.tcx.typeck(def_id).node_type(hir_id); - record!(self.tables.kind[def_id.to_def_id()] <- match ty.kind() { + match ty.kind() { ty::Generator(..) => { let data = self.tcx.generator_kind(def_id).unwrap(); - EntryKind::Generator(data) + record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::Generator); + record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator(data)); } - ty::Closure(..) => EntryKind::Closure, + ty::Closure(..) => { + record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::Closure); + record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Closure); + } _ => bug!("closure that is neither generator nor closure"), - }); + } record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id)); record!(self.tables.attributes[def_id.to_def_id()] <- &self.tcx.get_attrs(def_id.to_def_id())[..]); self.encode_item_type(def_id.to_def_id()); @@ -1524,6 +1557,7 @@ impl EncodeContext<'a, 'tcx> { let const_data = self.encode_rendered_const_for_body(body_id); let qualifs = self.tcx.mir_const_qualif(def_id); + record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::AnonConst); record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data)); record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id)); self.encode_item_type(def_id.to_def_id()); @@ -1606,6 +1640,7 @@ impl EncodeContext<'a, 'tcx> { def_key.disambiguated_data.data = DefPathData::MacroNs(name); let def_id = DefId::local(id); + record!(self.tables.def_kind[def_id] <- DefKind::Macro(macro_kind)); record!(self.tables.kind[def_id] <- EntryKind::ProcMacro(macro_kind)); record!(self.tables.attributes[def_id] <- attrs); record!(self.tables.def_keys[def_id] <- def_key); @@ -1773,7 +1808,7 @@ impl EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_info_for_foreign_item({:?})", def_id); - record!(self.tables.kind[def_id] <- match nitem.kind { + match nitem.kind { hir::ForeignItemKind::Fn(_, ref names, _) => { let data = FnData { asyncness: hir::IsAsync::NotAsync, @@ -1784,12 +1819,22 @@ impl EncodeContext<'a, 'tcx> { }, param_names: self.encode_fn_param_names(names), }; - EntryKind::ForeignFn(self.lazy(data)) + record!(self.tables.def_kind[def_id] <- DefKind::Fn); + record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data))); } - hir::ForeignItemKind::Static(_, hir::Mutability::Mut) => EntryKind::ForeignMutStatic, - hir::ForeignItemKind::Static(_, hir::Mutability::Not) => EntryKind::ForeignImmStatic, - hir::ForeignItemKind::Type => EntryKind::ForeignType, - }); + hir::ForeignItemKind::Static(_, hir::Mutability::Mut) => { + record!(self.tables.def_kind[def_id] <- DefKind::Static); + record!(self.tables.kind[def_id] <- EntryKind::ForeignMutStatic); + } + hir::ForeignItemKind::Static(_, hir::Mutability::Not) => { + record!(self.tables.def_kind[def_id] <- DefKind::Static); + record!(self.tables.kind[def_id] <- EntryKind::ForeignImmStatic); + } + hir::ForeignItemKind::Type => { + record!(self.tables.def_kind[def_id] <- DefKind::ForeignTy); + record!(self.tables.kind[def_id] <- EntryKind::ForeignType); + } + } record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.span[def_id] <- nitem.span); record!(self.tables.attributes[def_id] <- nitem.attrs); @@ -1865,6 +1910,7 @@ impl EncodeContext<'a, 'tcx> { GenericParamKind::Type { ref default, .. } => { self.encode_info_for_generic_param( def_id.to_def_id(), + DefKind::TyParam, EntryKind::TypeParam, default.is_some(), ); @@ -1875,6 +1921,7 @@ impl EncodeContext<'a, 'tcx> { GenericParamKind::Const { .. } => { self.encode_info_for_generic_param( def_id.to_def_id(), + DefKind::ConstParam, EntryKind::ConstParam, true, ); diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 59a8bc7fac1be..b44c3bfcac647 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -6,7 +6,7 @@ use rustc_attr as attr; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::MetadataRef; use rustc_hir as hir; -use rustc_hir::def::CtorKind; +use rustc_hir::def::{CtorKind, DefKind}; use rustc_hir::def_id::{DefId, DefIndex, DefPathHash}; use rustc_hir::definitions::DefKey; use rustc_hir::lang_items; @@ -279,6 +279,7 @@ macro_rules! define_tables { } define_tables! { + def_kind: Table>, kind: Table>, visibility: Table>, span: Table>, From c58a6fa422dd7c5bcf4f9d32d096c3d42f313240 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 28 Nov 2020 22:07:00 +0100 Subject: [PATCH 03/13] Iterate DefId to encode spans. --- compiler/rustc_hir/src/definitions.rs | 4 +++ compiler/rustc_metadata/src/rmeta/encoder.rs | 28 ++++++++++--------- compiler/rustc_middle/src/hir/map/mod.rs | 4 +++ .../ui/lint/lint-const-item-mutation.stderr | 10 ++----- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index d5ade86593e56..6a1b9bdbb9491 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -419,6 +419,10 @@ impl Definitions { pub fn add_parent_module_of_macro_def(&mut self, expn_id: ExpnId, module: DefId) { self.parent_modules_of_macro_defs.insert(expn_id, module); } + + pub fn iter_local_def_id(&self) -> impl Iterator + '_ { + self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k) + } } #[derive(Copy, Clone, PartialEq, Debug)] diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 600f521318b92..bc9fc00e0ec2c 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -580,6 +580,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // Encode the items. i = self.position(); + self.encode_def_ids(); self.encode_info_for_items(); let item_bytes = self.position() - i; @@ -716,6 +717,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } impl EncodeContext<'a, 'tcx> { + fn encode_def_ids(&mut self) { + if self.is_proc_macro { + return; + } + let tcx = self.tcx; + let hir = tcx.hir(); + for local_id in hir.iter_local_def_id() { + let def_id = local_id.to_def_id(); + record!(self.tables.span[def_id] <- tcx.def_span(def_id)); + } + } + fn encode_variances_of(&mut self, def_id: DefId) { debug!("EncodeContext::encode_variances_of({:?})", def_id); record!(self.tables.variances[def_id] <- &self.tcx.variances_of(def_id)[..]); @@ -742,7 +755,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.def_kind[def_id] <- DefKind::Variant); record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| { @@ -783,7 +795,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.def_kind[def_id] <- DefKind::Variant); record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); self.encode_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); @@ -836,7 +847,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.def_kind[def_id] <- DefKind::Mod); record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.attributes[def_id] <- attrs); if self.is_proc_macro { record!(self.tables.children[def_id] <- &[]); @@ -868,7 +878,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.def_kind[def_id] <- DefKind::Field); record!(self.tables.kind[def_id] <- EntryKind::Field); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); self.encode_ident_span(def_id, field.ident); @@ -895,7 +904,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.def_kind[def_id] <- DefKind::Struct); record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr)); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); self.encode_stability(def_id); self.encode_deprecation(def_id); @@ -1003,7 +1011,6 @@ impl EncodeContext<'a, 'tcx> { } } record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- ast_item.span); record!(self.tables.attributes[def_id] <- ast_item.attrs); self.encode_ident_span(def_id, ast_item.ident); self.encode_stability(def_id); @@ -1110,7 +1117,6 @@ impl EncodeContext<'a, 'tcx> { } } record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- ast_item.span); record!(self.tables.attributes[def_id] <- ast_item.attrs); self.encode_ident_span(def_id, impl_item.ident); self.encode_stability(def_id); @@ -1368,7 +1374,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.def_kind[def_id] <- def_kind); record!(self.tables.kind[def_id] <- entry_kind); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); record!(self.tables.attributes[def_id] <- item.attrs); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); // FIXME(eddyb) there should be a nicer way to do this. @@ -1489,7 +1494,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.def_kind[def_id] <- DefKind::Macro(MacroKind::Bang)); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- macro_def.span); record!(self.tables.attributes[def_id] <- macro_def.attrs); self.encode_ident_span(def_id, macro_def.ident); self.encode_stability(def_id); @@ -1505,7 +1509,6 @@ impl EncodeContext<'a, 'tcx> { ) { record!(self.tables.def_kind[def_id] <- def_kind); record!(self.tables.kind[def_id] <- kind); - record!(self.tables.span[def_id] <- self.tcx.def_span(def_id)); if encode_type { self.encode_item_type(def_id); } @@ -1533,7 +1536,6 @@ impl EncodeContext<'a, 'tcx> { _ => bug!("closure that is neither generator nor closure"), } - record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id)); record!(self.tables.attributes[def_id.to_def_id()] <- &self.tcx.get_attrs(def_id.to_def_id())[..]); self.encode_item_type(def_id.to_def_id()); if let ty::Closure(def_id, substs) = *ty.kind() { @@ -1559,7 +1561,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::AnonConst); record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data)); - record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id)); self.encode_item_type(def_id.to_def_id()); self.encode_generics(def_id.to_def_id()); self.encode_explicit_predicates(def_id.to_def_id()); @@ -1605,6 +1606,8 @@ impl EncodeContext<'a, 'tcx> { let tcx = self.tcx; let hir = tcx.hir(); + record!(self.tables.span[LOCAL_CRATE.as_def_id()] <- hir.span(hir::CRATE_HIR_ID)); + let proc_macro_decls_static = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap().index; let stability = tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX)).copied(); let macros = self.lazy(hir.krate().proc_macros.iter().map(|p| p.owner.local_def_index)); @@ -1836,7 +1839,6 @@ impl EncodeContext<'a, 'tcx> { } } record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); - record!(self.tables.span[def_id] <- nitem.span); record!(self.tables.attributes[def_id] <- nitem.attrs); self.encode_ident_span(def_id, nitem.ident); self.encode_stability(def_id); diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index e1fcc9be66417..3cc8683abb187 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -183,6 +183,10 @@ impl<'hir> Map<'hir> { self.tcx.definitions.opt_local_def_id_to_hir_id(def_id) } + pub fn iter_local_def_id(&self) -> impl Iterator + '_ { + self.tcx.definitions.iter_local_def_id() + } + pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind { // FIXME(eddyb) support `find` on the crate root. if local_def_id.to_def_id().index == CRATE_DEF_INDEX { diff --git a/src/test/ui/lint/lint-const-item-mutation.stderr b/src/test/ui/lint/lint-const-item-mutation.stderr index 3973af540c81f..540e076c7e4c6 100644 --- a/src/test/ui/lint/lint-const-item-mutation.stderr +++ b/src/test/ui/lint/lint-const-item-mutation.stderr @@ -109,14 +109,8 @@ LL | VEC.push(0); note: mutable reference created due to call to this method --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL | -LL | / pub fn push(&mut self, value: T) { -LL | | // This will panic or abort if we would allocate > isize::MAX bytes -LL | | // or if the length increment would overflow for zero-sized types. -LL | | if self.len == self.buf.capacity() { -... | -LL | | } -LL | | } - | |_____^ +LL | pub fn push(&mut self, value: T) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: `const` item defined here --> $DIR/lint-const-item-mutation.rs:31:1 | From a185cdbc595da86fbe6831f8b678cf44af6e537a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 16 Jan 2021 14:35:16 +0100 Subject: [PATCH 04/13] Iterate to encode def_kind. --- .../src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 86 ++++++------------- compiler/rustc_middle/src/hir/map/mod.rs | 21 ++--- compiler/rustc_middle/src/hir/mod.rs | 2 +- compiler/rustc_middle/src/query/mod.rs | 2 +- compiler/rustc_middle/src/ty/query/mod.rs | 16 ++++ 6 files changed, 57 insertions(+), 72 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 17ce4a5f9520e..828c025d38d0b 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -130,7 +130,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, is_foreign_item => { cdata.is_foreign_item(def_id.index) } static_mutability => { cdata.static_mutability(def_id.index) } generator_kind => { cdata.generator_kind(def_id.index) } - def_kind => { cdata.def_kind(def_id.index) } + opt_def_kind => { Some(cdata.def_kind(def_id.index)) } def_span => { cdata.get_span(def_id.index, &tcx.sess) } def_ident_span => { cdata.try_item_ident(def_id.index, &tcx.sess).ok().map(|ident| ident.span) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index bc9fc00e0ec2c..2b22ef0374c57 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -7,7 +7,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::sync::{join, Lrc}; use rustc_hir as hir; -use rustc_hir::def::{CtorKind, DefKind}; +use rustc_hir::def::{CtorOf, DefKind}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::definitions::DefPathData; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; @@ -725,6 +725,14 @@ impl EncodeContext<'a, 'tcx> { let hir = tcx.hir(); for local_id in hir.iter_local_def_id() { let def_id = local_id.to_def_id(); + let def_kind = tcx.opt_def_kind(local_id); + let def_kind = if let Some(def_kind) = def_kind { def_kind } else { continue }; + record!(self.tables.def_kind[def_id] <- match def_kind { + // Replace Ctor by the enclosing object to avoid leaking details in children crates. + DefKind::Ctor(CtorOf::Struct, _) => DefKind::Struct, + DefKind::Ctor(CtorOf::Variant, _) => DefKind::Variant, + def_kind => def_kind, + }); record!(self.tables.span[def_id] <- tcx.def_span(def_id)); } } @@ -752,7 +760,6 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; - record!(self.tables.def_kind[def_id] <- DefKind::Variant); record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]); @@ -792,7 +799,6 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; - record!(self.tables.def_kind[def_id] <- DefKind::Variant); record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); self.encode_stability(def_id); @@ -844,7 +850,6 @@ impl EncodeContext<'a, 'tcx> { expansion: tcx.hir().definitions().expansion_that_defined(local_def_id), }; - record!(self.tables.def_kind[def_id] <- DefKind::Mod); record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- attrs); @@ -875,7 +880,6 @@ impl EncodeContext<'a, 'tcx> { let variant_id = tcx.hir().local_def_id_to_hir_id(variant.def_id.expect_local()); let variant_data = tcx.hir().expect_variant_data(variant_id); - record!(self.tables.def_kind[def_id] <- DefKind::Field); record!(self.tables.kind[def_id] <- EntryKind::Field); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs); @@ -901,7 +905,6 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; - record!(self.tables.def_kind[def_id] <- DefKind::Struct); record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr)); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); @@ -976,7 +979,6 @@ impl EncodeContext<'a, 'tcx> { ); let rendered_const = self.lazy(RenderedConst(rendered)); - record!(self.tables.def_kind[def_id] <- DefKind::AssocConst); record!(self.tables.kind[def_id] <- EntryKind::AssocConst( container, Default::default(), @@ -997,7 +999,6 @@ impl EncodeContext<'a, 'tcx> { } else { bug!() }; - record!(self.tables.def_kind[def_id] <- DefKind::AssocFn); record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData { fn_data, container, @@ -1006,7 +1007,6 @@ impl EncodeContext<'a, 'tcx> { } ty::AssocKind::Type => { self.encode_explicit_item_bounds(def_id); - record!(self.tables.def_kind[def_id] <- DefKind::AssocTy); record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); } } @@ -1084,7 +1084,6 @@ impl EncodeContext<'a, 'tcx> { if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind { let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id); - record!(self.tables.def_kind[def_id] <- DefKind::AssocConst); record!(self.tables.kind[def_id] <- EntryKind::AssocConst( container, qualifs, @@ -1104,7 +1103,6 @@ impl EncodeContext<'a, 'tcx> { } else { bug!() }; - record!(self.tables.def_kind[def_id] <- DefKind::AssocFn); record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData { fn_data, container, @@ -1112,7 +1110,6 @@ impl EncodeContext<'a, 'tcx> { }))); } ty::AssocKind::Type => { - record!(self.tables.def_kind[def_id] <- DefKind::AssocTy); record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); } } @@ -1251,19 +1248,12 @@ impl EncodeContext<'a, 'tcx> { self.encode_ident_span(def_id, item.ident); - let (def_kind, entry_kind) = match item.kind { - hir::ItemKind::Static(_, hir::Mutability::Mut, _) => { - (DefKind::Static, EntryKind::MutStatic) - } - hir::ItemKind::Static(_, hir::Mutability::Not, _) => { - (DefKind::Static, EntryKind::ImmStatic) - } + let entry_kind = match item.kind { + hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic, + hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic, hir::ItemKind::Const(_, body_id) => { let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id); - ( - DefKind::Const, - EntryKind::Const(qualifs, self.encode_rendered_const_for_body(body_id)), - ) + EntryKind::Const(qualifs, self.encode_rendered_const_for_body(body_id)) } hir::ItemKind::Fn(ref sig, .., body) => { let data = FnData { @@ -1272,21 +1262,19 @@ impl EncodeContext<'a, 'tcx> { param_names: self.encode_fn_param_names_for_body(body), }; - (DefKind::Fn, EntryKind::Fn(self.lazy(data))) + EntryKind::Fn(self.lazy(data)) } hir::ItemKind::Mod(ref m) => { return self.encode_info_for_mod(item.hir_id, m, &item.attrs); } - hir::ItemKind::ForeignMod { .. } => (DefKind::ForeignMod, EntryKind::ForeignMod), - hir::ItemKind::GlobalAsm(..) => (DefKind::GlobalAsm, EntryKind::GlobalAsm), - hir::ItemKind::TyAlias(..) => (DefKind::TyAlias, EntryKind::Type), + hir::ItemKind::ForeignMod { .. } => EntryKind::ForeignMod, + hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm, + hir::ItemKind::TyAlias(..) => EntryKind::Type, hir::ItemKind::OpaqueTy(..) => { self.encode_explicit_item_bounds(def_id); - (DefKind::OpaqueTy, EntryKind::OpaqueTy) - } - hir::ItemKind::Enum(..) => { - (DefKind::Enum, EntryKind::Enum(self.tcx.adt_def(def_id).repr)) + EntryKind::OpaqueTy } + hir::ItemKind::Enum(..) => EntryKind::Enum(self.tcx.adt_def(def_id).repr), hir::ItemKind::Struct(ref struct_def, _) => { let adt_def = self.tcx.adt_def(def_id); let variant = adt_def.non_enum_variant(); @@ -1298,7 +1286,7 @@ impl EncodeContext<'a, 'tcx> { .ctor_hir_id() .map(|ctor_hir_id| self.tcx.hir().local_def_id(ctor_hir_id).local_def_index); - let ek = EntryKind::Struct( + EntryKind::Struct( self.lazy(VariantData { ctor_kind: variant.ctor_kind, discr: variant.discr, @@ -1306,14 +1294,13 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }), adt_def.repr, - ); - (DefKind::Struct, ek) + ) } hir::ItemKind::Union(..) => { let adt_def = self.tcx.adt_def(def_id); let variant = adt_def.non_enum_variant(); - let ek = EntryKind::Union( + EntryKind::Union( self.lazy(VariantData { ctor_kind: variant.ctor_kind, discr: variant.discr, @@ -1321,8 +1308,7 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }), adt_def.repr, - ); - (DefKind::Union, ek) + ) } hir::ItemKind::Impl(hir::Impl { defaultness, .. }) => { let trait_ref = self.tcx.impl_trait_ref(def_id); @@ -1352,7 +1338,7 @@ impl EncodeContext<'a, 'tcx> { let data = ImplData { polarity, defaultness, parent_impl: parent, coerce_unsized_info }; - (DefKind::Impl, EntryKind::Impl(self.lazy(data))) + EntryKind::Impl(self.lazy(data)) } hir::ItemKind::Trait(..) => { let trait_def = self.tcx.trait_def(def_id); @@ -1364,14 +1350,13 @@ impl EncodeContext<'a, 'tcx> { specialization_kind: trait_def.specialization_kind, }; - (DefKind::Trait, EntryKind::Trait(self.lazy(data))) + EntryKind::Trait(self.lazy(data)) } - hir::ItemKind::TraitAlias(..) => (DefKind::TraitAlias, EntryKind::TraitAlias), + hir::ItemKind::TraitAlias(..) => EntryKind::TraitAlias, hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => { bug!("cannot encode info for item {:?}", item) } }; - record!(self.tables.def_kind[def_id] <- def_kind); record!(self.tables.kind[def_id] <- entry_kind); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- item.attrs); @@ -1491,7 +1476,6 @@ impl EncodeContext<'a, 'tcx> { /// Serialize the text of exported macros fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) { let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id(); - record!(self.tables.def_kind[def_id] <- DefKind::Macro(MacroKind::Bang)); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- macro_def.attrs); @@ -1500,14 +1484,7 @@ impl EncodeContext<'a, 'tcx> { self.encode_deprecation(def_id); } - fn encode_info_for_generic_param( - &mut self, - def_id: DefId, - def_kind: DefKind, - kind: EntryKind, - encode_type: bool, - ) { - record!(self.tables.def_kind[def_id] <- def_kind); + fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) { record!(self.tables.kind[def_id] <- kind); if encode_type { self.encode_item_type(def_id); @@ -1525,12 +1502,10 @@ impl EncodeContext<'a, 'tcx> { match ty.kind() { ty::Generator(..) => { let data = self.tcx.generator_kind(def_id).unwrap(); - record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::Generator); record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Generator(data)); } ty::Closure(..) => { - record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::Closure); record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::Closure); } @@ -1559,7 +1534,6 @@ impl EncodeContext<'a, 'tcx> { let const_data = self.encode_rendered_const_for_body(body_id); let qualifs = self.tcx.mir_const_qualif(def_id); - record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::AnonConst); record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data)); self.encode_item_type(def_id.to_def_id()); self.encode_generics(def_id.to_def_id()); @@ -1822,19 +1796,15 @@ impl EncodeContext<'a, 'tcx> { }, param_names: self.encode_fn_param_names(names), }; - record!(self.tables.def_kind[def_id] <- DefKind::Fn); record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data))); } hir::ForeignItemKind::Static(_, hir::Mutability::Mut) => { - record!(self.tables.def_kind[def_id] <- DefKind::Static); record!(self.tables.kind[def_id] <- EntryKind::ForeignMutStatic); } hir::ForeignItemKind::Static(_, hir::Mutability::Not) => { - record!(self.tables.def_kind[def_id] <- DefKind::Static); record!(self.tables.kind[def_id] <- EntryKind::ForeignImmStatic); } hir::ForeignItemKind::Type => { - record!(self.tables.def_kind[def_id] <- DefKind::ForeignTy); record!(self.tables.kind[def_id] <- EntryKind::ForeignType); } } @@ -1912,7 +1882,6 @@ impl EncodeContext<'a, 'tcx> { GenericParamKind::Type { ref default, .. } => { self.encode_info_for_generic_param( def_id.to_def_id(), - DefKind::TyParam, EntryKind::TypeParam, default.is_some(), ); @@ -1923,7 +1892,6 @@ impl EncodeContext<'a, 'tcx> { GenericParamKind::Const { .. } => { self.encode_info_for_generic_param( def_id.to_def_id(), - DefKind::ConstParam, EntryKind::ConstParam, true, ); diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 3cc8683abb187..70819c9b3b54e 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -1,7 +1,6 @@ use self::collector::NodeCollector; use crate::hir::{Owner, OwnerNodes}; -use crate::ty::query::Providers; use crate::ty::TyCtxt; use rustc_ast as ast; use rustc_data_structures::svh::Svh; @@ -187,14 +186,14 @@ impl<'hir> Map<'hir> { self.tcx.definitions.iter_local_def_id() } - pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind { + pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option { // FIXME(eddyb) support `find` on the crate root. if local_def_id.to_def_id().index == CRATE_DEF_INDEX { - return DefKind::Mod; + return Some(DefKind::Mod); } let hir_id = self.local_def_id_to_hir_id(local_def_id); - match self.get(hir_id) { + let def_kind = match self.find(hir_id)? { Node::Item(item) => match item.kind { ItemKind::Static(..) => DefKind::Static, ItemKind::Const(..) => DefKind::Const, @@ -265,8 +264,14 @@ impl<'hir> Map<'hir> { | Node::Lifetime(_) | Node::Visibility(_) | Node::Block(_) - | Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)), - } + | Node::Crate(_) => return None, + }; + Some(def_kind) + } + + pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind { + self.opt_def_kind(local_def_id) + .unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", local_def_id)) } fn find_entry(&self, id: HirId) -> Option> { @@ -1110,7 +1115,3 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String { None => format!("unknown node{}", id_str), } } - -pub fn provide(providers: &mut Providers) { - providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local()); -} diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index dd4bfdd3e4a54..e96f3f2a29475 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -94,5 +94,5 @@ pub fn provide(providers: &mut Providers) { span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id); } }; - map::provide(providers); + providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local()); } diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 00ee7b8ec7709..e23d04f8211f3 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -878,7 +878,7 @@ rustc_queries! { cache_on_disk_if { true } } - query def_kind(def_id: DefId) -> DefKind { + query opt_def_kind(def_id: DefId) -> Option { desc { |tcx| "looking up definition kind of `{}`", tcx.def_path_str(def_id) } } diff --git a/compiler/rustc_middle/src/ty/query/mod.rs b/compiler/rustc_middle/src/ty/query/mod.rs index acfa58e511ed1..f580cb14dc988 100644 --- a/compiler/rustc_middle/src/ty/query/mod.rs +++ b/compiler/rustc_middle/src/ty/query/mod.rs @@ -130,3 +130,19 @@ mod sealed { } use sealed::IntoQueryParam; + +impl TyCtxt<'tcx> { + pub fn def_kind(self, def_id: impl IntoQueryParam) -> DefKind { + let def_id = def_id.into_query_param(); + self.opt_def_kind(def_id) + .unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id)) + } +} + +impl TyCtxtAt<'tcx> { + pub fn def_kind(self, def_id: impl IntoQueryParam) -> DefKind { + let def_id = def_id.into_query_param(); + self.opt_def_kind(def_id) + .unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id)) + } +} From f1bf6d0e48fdf5be3c3a1264b5d0ef8f1f272e5f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 28 Nov 2020 22:07:46 +0100 Subject: [PATCH 05/13] Iterate DefId to encode visibility. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 48 ++++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 2b22ef0374c57..7460174c1af0c 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -716,6 +716,41 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } +fn should_encode_visibility(def_kind: DefKind) -> bool { + match def_kind { + DefKind::Mod + | DefKind::Struct + | DefKind::Union + | DefKind::Enum + | DefKind::Variant + | DefKind::Trait + | DefKind::TyAlias + | DefKind::ForeignTy + | DefKind::TraitAlias + | DefKind::AssocTy + | DefKind::Fn + | DefKind::Const + | DefKind::Static + | DefKind::Ctor(..) + | DefKind::AssocFn + | DefKind::AssocConst + | DefKind::Macro(..) + | DefKind::Use + | DefKind::ForeignMod + | DefKind::OpaqueTy + | DefKind::Impl + | DefKind::Field => true, + DefKind::TyParam + | DefKind::ConstParam + | DefKind::LifetimeParam + | DefKind::AnonConst + | DefKind::GlobalAsm + | DefKind::Closure + | DefKind::Generator + | DefKind::ExternCrate => false, + } +} + impl EncodeContext<'a, 'tcx> { fn encode_def_ids(&mut self) { if self.is_proc_macro { @@ -734,6 +769,9 @@ impl EncodeContext<'a, 'tcx> { def_kind => def_kind, }); record!(self.tables.span[def_id] <- tcx.def_span(def_id)); + if should_encode_visibility(def_kind) { + record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); + } } } @@ -761,7 +799,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| { @@ -800,7 +837,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); self.encode_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); @@ -851,7 +887,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data))); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- attrs); if self.is_proc_macro { record!(self.tables.children[def_id] <- &[]); @@ -881,7 +916,6 @@ impl EncodeContext<'a, 'tcx> { let variant_data = tcx.hir().expect_variant_data(variant_id); record!(self.tables.kind[def_id] <- EntryKind::Field); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); self.encode_ident_span(def_id, field.ident); @@ -906,7 +940,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr)); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); self.encode_stability(def_id); self.encode_deprecation(def_id); @@ -1010,7 +1043,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); } } - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- ast_item.attrs); self.encode_ident_span(def_id, ast_item.ident); self.encode_stability(def_id); @@ -1113,7 +1145,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); } } - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- ast_item.attrs); self.encode_ident_span(def_id, impl_item.ident); self.encode_stability(def_id); @@ -1358,7 +1389,6 @@ impl EncodeContext<'a, 'tcx> { } }; record!(self.tables.kind[def_id] <- entry_kind); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- item.attrs); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); // FIXME(eddyb) there should be a nicer way to do this. @@ -1477,7 +1507,6 @@ impl EncodeContext<'a, 'tcx> { fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) { let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id(); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- macro_def.attrs); self.encode_ident_span(def_id, macro_def.ident); self.encode_stability(def_id); @@ -1808,7 +1837,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::ForeignType); } } - record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.attributes[def_id] <- nitem.attrs); self.encode_ident_span(def_id, nitem.ident); self.encode_stability(def_id); From 293e885daaa6fdb518f713e46170e1fc5cdb7183 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 28 Nov 2020 22:10:00 +0100 Subject: [PATCH 06/13] Iterate DefId to encode attributes. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 21 ++++---------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 7460174c1af0c..c80f70ae5286f 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1,7 +1,6 @@ use crate::rmeta::table::{FixedSizeEncoding, TableBuilder}; use crate::rmeta::*; -use rustc_ast as ast; use rustc_data_structures::fingerprint::{Fingerprint, FingerprintEncoder}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::stable_hasher::StableHasher; @@ -437,7 +436,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_info_for_items(&mut self) { let krate = self.tcx.hir().krate(); - self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.item.module, &krate.item.attrs); + self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.item.module); // Proc-macro crates only export proc-macro items, which are looked // up using `proc_macro_data` @@ -769,6 +768,7 @@ impl EncodeContext<'a, 'tcx> { def_kind => def_kind, }); record!(self.tables.span[def_id] <- tcx.def_span(def_id)); + record!(self.tables.attributes[def_id] <- tcx.get_attrs(def_id)); if should_encode_visibility(def_kind) { record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); } @@ -799,7 +799,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); - record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| { assert!(f.did.is_local()); @@ -854,7 +853,7 @@ impl EncodeContext<'a, 'tcx> { self.encode_mir_for_ctfe(def_id.expect_local()); } - fn encode_info_for_mod(&mut self, id: hir::HirId, md: &hir::Mod<'_>, attrs: &[ast::Attribute]) { + fn encode_info_for_mod(&mut self, id: hir::HirId, md: &hir::Mod<'_>) { let tcx = self.tcx; let local_def_id = tcx.hir().local_def_id(id); let def_id = local_def_id.to_def_id(); @@ -887,7 +886,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data))); - record!(self.tables.attributes[def_id] <- attrs); if self.is_proc_macro { record!(self.tables.children[def_id] <- &[]); } else { @@ -905,18 +903,13 @@ impl EncodeContext<'a, 'tcx> { variant_index: VariantIdx, field_index: usize, ) { - let tcx = self.tcx; let variant = &adt_def.variants[variant_index]; let field = &variant.fields[field_index]; let def_id = field.did; debug!("EncodeContext::encode_field({:?})", def_id); - let variant_id = tcx.hir().local_def_id_to_hir_id(variant.def_id.expect_local()); - let variant_data = tcx.hir().expect_variant_data(variant_id); - record!(self.tables.kind[def_id] <- EntryKind::Field); - record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); self.encode_ident_span(def_id, field.ident); self.encode_stability(def_id); @@ -1043,7 +1036,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); } } - record!(self.tables.attributes[def_id] <- ast_item.attrs); self.encode_ident_span(def_id, ast_item.ident); self.encode_stability(def_id); self.encode_const_stability(def_id); @@ -1145,7 +1137,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); } } - record!(self.tables.attributes[def_id] <- ast_item.attrs); self.encode_ident_span(def_id, impl_item.ident); self.encode_stability(def_id); self.encode_const_stability(def_id); @@ -1296,7 +1287,7 @@ impl EncodeContext<'a, 'tcx> { EntryKind::Fn(self.lazy(data)) } hir::ItemKind::Mod(ref m) => { - return self.encode_info_for_mod(item.hir_id, m, &item.attrs); + return self.encode_info_for_mod(item.hir_id, m); } hir::ItemKind::ForeignMod { .. } => EntryKind::ForeignMod, hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm, @@ -1389,7 +1380,6 @@ impl EncodeContext<'a, 'tcx> { } }; record!(self.tables.kind[def_id] <- entry_kind); - record!(self.tables.attributes[def_id] <- item.attrs); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); // FIXME(eddyb) there should be a nicer way to do this. match item.kind { @@ -1507,7 +1497,6 @@ impl EncodeContext<'a, 'tcx> { fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) { let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id(); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); - record!(self.tables.attributes[def_id] <- macro_def.attrs); self.encode_ident_span(def_id, macro_def.ident); self.encode_stability(def_id); self.encode_deprecation(def_id); @@ -1540,7 +1529,6 @@ impl EncodeContext<'a, 'tcx> { _ => bug!("closure that is neither generator nor closure"), } - record!(self.tables.attributes[def_id.to_def_id()] <- &self.tcx.get_attrs(def_id.to_def_id())[..]); self.encode_item_type(def_id.to_def_id()); if let ty::Closure(def_id, substs) = *ty.kind() { record!(self.tables.fn_sig[def_id] <- substs.as_closure().sig()); @@ -1837,7 +1825,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::ForeignType); } } - record!(self.tables.attributes[def_id] <- nitem.attrs); self.encode_ident_span(def_id, nitem.ident); self.encode_stability(def_id); self.encode_const_stability(def_id); From 7a5620c63a3c2431777e93169cfaf9d1e32ca8e0 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 28 Nov 2020 22:12:56 +0100 Subject: [PATCH 07/13] Iterate DefId to encode expn_that_defined. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index c80f70ae5286f..da611ab9c1c20 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -769,6 +769,7 @@ impl EncodeContext<'a, 'tcx> { }); record!(self.tables.span[def_id] <- tcx.def_span(def_id)); record!(self.tables.attributes[def_id] <- tcx.get_attrs(def_id)); + record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); if should_encode_visibility(def_kind) { record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); } @@ -799,7 +800,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); - record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| { assert!(f.did.is_local()); f.did.index @@ -910,7 +910,6 @@ impl EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_field({:?})", def_id); record!(self.tables.kind[def_id] <- EntryKind::Field); - record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); self.encode_ident_span(def_id, field.ident); self.encode_stability(def_id); self.encode_deprecation(def_id); @@ -933,7 +932,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr)); - record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); self.encode_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); @@ -1380,7 +1378,6 @@ impl EncodeContext<'a, 'tcx> { } }; record!(self.tables.kind[def_id] <- entry_kind); - record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id)); // FIXME(eddyb) there should be a nicer way to do this. match item.kind { hir::ItemKind::ForeignMod { items, .. } => record!(self.tables.children[def_id] <- From f1a7e1df3aa9c698d4e793e865cac867b0bd3694 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 6 Dec 2020 22:20:24 +0100 Subject: [PATCH 08/13] Iterate on stability. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index da611ab9c1c20..9314ccc4adb30 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -773,6 +773,7 @@ impl EncodeContext<'a, 'tcx> { if should_encode_visibility(def_kind) { record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); } + self.encode_stability(def_id); } } @@ -805,7 +806,6 @@ impl EncodeContext<'a, 'tcx> { f.did.index })); self.encode_ident_span(def_id, variant.ident); - self.encode_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { @@ -836,7 +836,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); - self.encode_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { @@ -893,7 +892,6 @@ impl EncodeContext<'a, 'tcx> { tcx.hir().local_def_id(item_id.id).local_def_index })); } - self.encode_stability(def_id); self.encode_deprecation(def_id); } @@ -911,7 +909,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::Field); self.encode_ident_span(def_id, field.ident); - self.encode_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); self.encode_generics(def_id); @@ -932,7 +929,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr)); - self.encode_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { @@ -1035,7 +1031,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, ast_item.ident); - self.encode_stability(def_id); self.encode_const_stability(def_id); self.encode_deprecation(def_id); match trait_item.kind { @@ -1136,7 +1131,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, impl_item.ident); - self.encode_stability(def_id); self.encode_const_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); @@ -1411,7 +1405,6 @@ impl EncodeContext<'a, 'tcx> { } _ => {} } - self.encode_stability(def_id); self.encode_const_stability(def_id); self.encode_deprecation(def_id); match item.kind { @@ -1495,7 +1488,6 @@ impl EncodeContext<'a, 'tcx> { let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id(); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); self.encode_ident_span(def_id, macro_def.ident); - self.encode_stability(def_id); self.encode_deprecation(def_id); } @@ -1823,7 +1815,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, nitem.ident); - self.encode_stability(def_id); self.encode_const_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); @@ -1891,15 +1882,12 @@ impl EncodeContext<'a, 'tcx> { let def_id = self.tcx.hir().local_def_id(param.hir_id); match param.kind { GenericParamKind::Lifetime { .. } => continue, - GenericParamKind::Type { ref default, .. } => { + GenericParamKind::Type { default, .. } => { self.encode_info_for_generic_param( def_id.to_def_id(), EntryKind::TypeParam, default.is_some(), ); - if default.is_some() { - self.encode_stability(def_id.to_def_id()); - } } GenericParamKind::Const { .. } => { self.encode_info_for_generic_param( From 4468473e1b2d3bdb9e7da438acc1448d810771ef Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 6 Dec 2020 22:21:00 +0100 Subject: [PATCH 09/13] Iterate on const_stability. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 9314ccc4adb30..fe79b15477401 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -774,6 +774,7 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); } self.encode_stability(def_id); + self.encode_const_stability(def_id); } } @@ -1031,7 +1032,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, ast_item.ident); - self.encode_const_stability(def_id); self.encode_deprecation(def_id); match trait_item.kind { ty::AssocKind::Const | ty::AssocKind::Fn => { @@ -1131,7 +1131,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, impl_item.ident); - self.encode_const_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); if impl_item.kind == ty::AssocKind::Fn { @@ -1405,7 +1404,6 @@ impl EncodeContext<'a, 'tcx> { } _ => {} } - self.encode_const_stability(def_id); self.encode_deprecation(def_id); match item.kind { hir::ItemKind::Static(..) @@ -1815,7 +1813,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, nitem.ident); - self.encode_const_stability(def_id); self.encode_deprecation(def_id); self.encode_item_type(def_id); self.encode_inherent_implementations(def_id); From 4e60ecf78357887861b3480da8745fd1cb7eb4c7 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 6 Dec 2020 22:21:42 +0100 Subject: [PATCH 10/13] Iterate on deprecation. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index fe79b15477401..ec95a74fc7e6a 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -775,6 +775,7 @@ impl EncodeContext<'a, 'tcx> { } self.encode_stability(def_id); self.encode_const_stability(def_id); + self.encode_deprecation(def_id); } } @@ -807,7 +808,6 @@ impl EncodeContext<'a, 'tcx> { f.did.index })); self.encode_ident_span(def_id, variant.ident); - self.encode_deprecation(def_id); self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { // FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`. @@ -837,7 +837,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); - self.encode_deprecation(def_id); self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); @@ -893,7 +892,6 @@ impl EncodeContext<'a, 'tcx> { tcx.hir().local_def_id(item_id.id).local_def_index })); } - self.encode_deprecation(def_id); } fn encode_field( @@ -910,7 +908,6 @@ impl EncodeContext<'a, 'tcx> { record!(self.tables.kind[def_id] <- EntryKind::Field); self.encode_ident_span(def_id, field.ident); - self.encode_deprecation(def_id); self.encode_item_type(def_id); self.encode_generics(def_id); self.encode_explicit_predicates(def_id); @@ -930,7 +927,6 @@ impl EncodeContext<'a, 'tcx> { }; record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr)); - self.encode_deprecation(def_id); self.encode_item_type(def_id); if variant.ctor_kind == CtorKind::Fn { record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); @@ -1032,7 +1028,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, ast_item.ident); - self.encode_deprecation(def_id); match trait_item.kind { ty::AssocKind::Const | ty::AssocKind::Fn => { self.encode_item_type(def_id); @@ -1131,7 +1126,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, impl_item.ident); - self.encode_deprecation(def_id); self.encode_item_type(def_id); if impl_item.kind == ty::AssocKind::Fn { record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); @@ -1404,7 +1398,6 @@ impl EncodeContext<'a, 'tcx> { } _ => {} } - self.encode_deprecation(def_id); match item.kind { hir::ItemKind::Static(..) | hir::ItemKind::Const(..) @@ -1486,7 +1479,6 @@ impl EncodeContext<'a, 'tcx> { let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id(); record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))); self.encode_ident_span(def_id, macro_def.ident); - self.encode_deprecation(def_id); } fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) { @@ -1813,7 +1805,6 @@ impl EncodeContext<'a, 'tcx> { } } self.encode_ident_span(def_id, nitem.ident); - self.encode_deprecation(def_id); self.encode_item_type(def_id); self.encode_inherent_implementations(def_id); if let hir::ForeignItemKind::Fn(..) = nitem.kind { From 4eb0bbd2a89a1342f411e138b944aa0598f3a311 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 16 Jan 2021 23:16:02 +0100 Subject: [PATCH 11/13] Filter stability. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 43 ++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index ec95a74fc7e6a..23329acffd916 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -750,6 +750,41 @@ fn should_encode_visibility(def_kind: DefKind) -> bool { } } +fn should_encode_stability(def_kind: DefKind) -> bool { + match def_kind { + DefKind::Mod + | DefKind::Ctor(..) + | DefKind::Variant + | DefKind::Field + | DefKind::Struct + | DefKind::AssocTy + | DefKind::AssocFn + | DefKind::AssocConst + | DefKind::TyParam + | DefKind::ConstParam + | DefKind::Static + | DefKind::Const + | DefKind::Fn + | DefKind::ForeignMod + | DefKind::TyAlias + | DefKind::OpaqueTy + | DefKind::Enum + | DefKind::Union + | DefKind::Impl + | DefKind::Trait + | DefKind::TraitAlias + | DefKind::Macro(..) + | DefKind::ForeignTy => true, + DefKind::Use + | DefKind::LifetimeParam + | DefKind::AnonConst + | DefKind::GlobalAsm + | DefKind::Closure + | DefKind::Generator + | DefKind::ExternCrate => false, + } +} + impl EncodeContext<'a, 'tcx> { fn encode_def_ids(&mut self) { if self.is_proc_macro { @@ -773,9 +808,11 @@ impl EncodeContext<'a, 'tcx> { if should_encode_visibility(def_kind) { record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); } - self.encode_stability(def_id); - self.encode_const_stability(def_id); - self.encode_deprecation(def_id); + if should_encode_stability(def_kind) { + self.encode_stability(def_id); + self.encode_const_stability(def_id); + self.encode_deprecation(def_id); + } } } From 89360931f23a512d76559a796cdbd74b6681c46e Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 16 Jan 2021 23:14:42 +0100 Subject: [PATCH 12/13] Fix proc macro crate encoding. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 23329acffd916..3961adacecae8 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1613,12 +1613,19 @@ impl EncodeContext<'a, 'tcx> { let tcx = self.tcx; let hir = tcx.hir(); - record!(self.tables.span[LOCAL_CRATE.as_def_id()] <- hir.span(hir::CRATE_HIR_ID)); - let proc_macro_decls_static = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap().index; let stability = tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX)).copied(); let macros = self.lazy(hir.krate().proc_macros.iter().map(|p| p.owner.local_def_index)); + record!(self.tables.def_kind[LOCAL_CRATE.as_def_id()] <- DefKind::Mod); + record!(self.tables.span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); + record!(self.tables.attributes[LOCAL_CRATE.as_def_id()] <- tcx.get_attrs(LOCAL_CRATE.as_def_id())); + record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- tcx.visibility(LOCAL_CRATE.as_def_id())); + if let Some(stability) = stability { + record!(self.tables.stability[LOCAL_CRATE.as_def_id()] <- stability); + } + self.encode_deprecation(LOCAL_CRATE.as_def_id()); + // Normally, this information is encoded when we walk the items // defined in this crate. However, we skip doing that for proc-macro crates, // so we manually encode just the information that we need From 97ee7c7e5ab5b70453920e105530dd8a2a4391d9 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 18 Jan 2021 19:15:53 +0100 Subject: [PATCH 13/13] Allow to query the HIR crate node. --- compiler/rustc_middle/src/hir/map/mod.rs | 8 +++----- compiler/rustc_passes/src/reachable.rs | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 70819c9b3b54e..9b01a64de8415 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -252,6 +252,7 @@ impl<'hir> Map<'hir> { GenericParamKind::Type { .. } => DefKind::TyParam, GenericParamKind::Const { .. } => DefKind::ConstParam, }, + Node::Crate(_) => DefKind::Mod, Node::Stmt(_) | Node::PathSegment(_) | Node::Ty(_) @@ -263,8 +264,7 @@ impl<'hir> Map<'hir> { | Node::Arm(_) | Node::Lifetime(_) | Node::Visibility(_) - | Node::Block(_) - | Node::Crate(_) => return None, + | Node::Block(_) => return None, }; Some(def_kind) } @@ -523,9 +523,7 @@ impl<'hir> Map<'hir> { /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found. pub fn find(&self, hir_id: HirId) -> Option> { - self.find_entry(hir_id).and_then(|entry| { - if let Node::Crate(..) = entry.node { None } else { Some(entry.node) } - }) + self.find_entry(hir_id).map(|entry| entry.node) } /// Similar to `get_parent`; returns the parent HIR Id, or just `hir_id` if there diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index b237671f8e208..eb24c51c54c35 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -307,6 +307,7 @@ impl<'tcx> ReachableContext<'tcx> { | Node::Ctor(..) | Node::Field(_) | Node::Ty(_) + | Node::Crate(_) | Node::MacroDef(_) => {} _ => { bug!(