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

@@ -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<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 @@ 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<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 @@ rustc_queries! {
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 @@ mod sealed {
}
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))
}
}