1
Fork 0

Allow hir().find to return None

This commit is contained in:
John Kåre Alsaker 2020-03-16 19:17:40 +01:00 committed by Markus Westerlind
parent 04689e22e9
commit 61621e2667
4 changed files with 18 additions and 18 deletions

View file

@ -311,19 +311,16 @@ impl<'hir> Map<'hir> {
}
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
if id.local_id == ItemLocalId::from_u32(0) {
let owner = self.tcx.hir_owner(id.owner);
if id.local_id == ItemLocalId::from_u32_const(0) {
let owner = self.tcx.hir_owner(id.owner_def_id());
owner.map(|owner| Entry { parent: owner.parent, node: owner.node })
} else {
let owner = self.tcx.hir_owner_nodes(id.owner);
let owner = self.tcx.hir_owner_items(id.owner_def_id());
owner.and_then(|owner| {
let node = owner.nodes[id.local_id].as_ref();
// FIXME(eddyb) use a single generic type insted of having both
// `Entry` and `ParentedNode`, which are effectively the same.
// Alternatively, rewrite code using `Entry` to use `ParentedNode`.
node.map(|node| Entry {
parent: HirId { owner: id.owner, local_id: node.parent },
node: node.node,
let item = owner.items[id.local_id].as_ref();
item.map(|item| Entry {
parent: HirId { owner: id.owner, local_id: item.parent },
node: item.node,
})
})
}
@ -355,7 +352,12 @@ impl<'hir> Map<'hir> {
}
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies.get(&id.hir_id.local_id).unwrap()
self.tcx
.hir_owner_items(DefId::local(id.hir_id.owner))
.unwrap()
.bodies
.get(&id.hir_id.local_id)
.unwrap()
}
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {

View file

@ -77,8 +77,8 @@ pub fn provide(providers: &mut Providers<'_>) {
let module = hir.as_local_hir_id(id);
&tcx.untracked_crate.modules[&module]
};
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_ref().map(|nodes| &**nodes);
providers.hir_owner = |tcx, id| tcx.index_hir(id.krate).map[id.index].signature;
providers.hir_owner_items =
|tcx, id| tcx.index_hir(id.krate).map[id.index].with_bodies.as_ref().map(|items| &**items);
map::provide(providers);
}

View file

@ -75,7 +75,7 @@ rustc_queries! {
//
// This can be conveniently accessed by methods on `tcx.hir()`.
// Avoid calling this query directly.
query hir_owner(key: LocalDefId) -> Option<&'tcx crate::hir::Owner<'tcx>> {
query hir_owner(key: DefId) -> Option<&'tcx HirOwner<'tcx>> {
eval_always
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
}
@ -84,7 +84,7 @@ rustc_queries! {
//
// This can be conveniently accessed by methods on `tcx.hir()`.
// Avoid calling this query directly.
query hir_owner_nodes(key: LocalDefId) -> Option<&'tcx crate::hir::OwnerNodes<'tcx>> {
query hir_owner_items(key: DefId) -> Option<&'tcx HirOwnerItems<'tcx>> {
eval_always
desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
}

View file

@ -2,12 +2,10 @@
// run-pass
macro_rules! regex {
//~^ WARN unused macro definition
() => {};
}
#[allow(dead_code)]
use regex;
//~^ WARN unused import
fn main() {}