Iterate to encode def_kind.

This commit is contained in:
Camille GILLOT 2021-01-16 14:35:16 +01:00
parent c58a6fa422
commit a185cdbc59
6 changed files with 57 additions and 72 deletions

View File

@ -130,7 +130,7 @@ fn into_args(self) -> (DefId, DefId) {
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)

View File

@ -7,7 +7,7 @@
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 @@ fn encode_def_ids(&mut self) {
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 @@ fn encode_enum_variant_info(&mut self, def: &ty::AdtDef, index: VariantIdx) {
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 @@ fn encode_enum_variant_ctor(&mut self, def: &ty::AdtDef, index: VariantIdx) {
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 @@ fn encode_info_for_mod(&mut self, id: hir::HirId, md: &hir::Mod<'_>, attrs: &[as
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 @@ fn encode_field(
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 @@ fn encode_struct_ctor(&mut self, adt_def: &ty::AdtDef, def_id: DefId) {
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 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) {
);
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 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) {
} 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 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) {
}
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 @@ fn encode_info_for_impl_item(&mut self, def_id: DefId) {
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 @@ fn encode_info_for_impl_item(&mut self, def_id: DefId) {
} 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 @@ fn encode_info_for_impl_item(&mut self, def_id: DefId) {
})));
}
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 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'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 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'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 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'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 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'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 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'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 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'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 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'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 @@ fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'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 @@ fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) {
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 @@ fn encode_info_for_closure(&mut self, def_id: LocalDefId) {
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 @@ fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) {
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 @@ fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignIt
},
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 @@ fn encode_info_for_generics(&mut self, generics: &hir::Generics<'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 @@ fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) {
GenericParamKind::Const { .. } => {
self.encode_info_for_generic_param(
def_id.to_def_id(),
DefKind::ConstParam,
EntryKind::ConstParam,
true,
);

View File

@ -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 @@ pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
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<DefKind> {
// 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 @@ pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {
| 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<Entry<'hir>> {
@ -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());
}

View File

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

View File

@ -878,7 +878,7 @@ fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
cache_on_disk_if { true }
}
query def_kind(def_id: DefId) -> DefKind {
query opt_def_kind(def_id: DefId) -> Option<DefKind> {
desc { |tcx| "looking up definition kind of `{}`", tcx.def_path_str(def_id) }
}

View File

@ -130,3 +130,19 @@ fn into_query_param(self) -> DefId {
}
use sealed::IntoQueryParam;
impl TyCtxt<'tcx> {
pub fn def_kind(self, def_id: impl IntoQueryParam<DefId>) -> 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<DefId>) -> 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))
}
}