Skip to content

Commit

Permalink
Allow hir().find to return None
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Mar 16, 2020
1 parent 8e6de32 commit 6207e8d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
25 changes: 16 additions & 9 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,25 @@ impl<'hir> Map<'hir> {
}

fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
Some(self.get_entry(id))
}

fn get_entry(&self, id: HirId) -> Entry<'hir> {
if id.local_id == ItemLocalId::from_u32_const(0) {
let owner = self.tcx.hir_owner(id.owner_def_id());
Entry { parent: owner.parent, node: owner.node }
owner.map(|owner| Entry { parent: owner.parent, node: owner.node })
} else {
let owner = self.tcx.hir_owner_items(id.owner_def_id());
let item = owner.items[id.local_id].as_ref().unwrap();
Entry { parent: HirId { owner: id.owner, local_id: item.parent }, node: item.node }
owner.and_then(|owner| {
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,
})
})
}
}

fn get_entry(&self, id: HirId) -> Entry<'hir> {
self.find_entry(id).unwrap()
}

pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
match self.find(id).unwrap() {
Node::Item(item) => item,
Expand All @@ -379,6 +384,7 @@ impl<'hir> Map<'hir> {
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
self.tcx
.hir_owner_items(DefId::local(id.hir_id.owner))
.unwrap()
.bodies
.get(&id.hir_id.local_id)
.unwrap()
Expand Down Expand Up @@ -540,8 +546,9 @@ impl<'hir> Map<'hir> {

/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
pub fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
let node = self.get_entry(hir_id).node;
if let Node::Crate(..) = node { None } else { Some(node) }
self.find_entry(hir_id).and_then(|entry| {
if let Node::Crate(..) = entry.node { None } else { Some(entry.node) }
})
}

/// Similar to `get_parent`; returns the parent HIR Id, or just `hir_id` if there
Expand Down
7 changes: 3 additions & 4 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ pub fn provide(providers: &mut Providers<'_>) {
let module = hir.as_local_hir_id(id).unwrap();
&tcx.untracked_crate.modules[&module]
};
providers.hir_owner = |tcx, id| tcx.index_hir(id.krate).map[id.index].signature.unwrap();
providers.hir_owner_items = |tcx, id| {
tcx.index_hir(id.krate).map[id.index].with_bodies.as_ref().map(|items| &**items).unwrap()
};
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);
}
4 changes: 2 additions & 2 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ rustc_queries! {
// a `DefId`.
// This can be conveniently accessed by methods on `tcx.hir()`.
// Avoid calling this query directly.
query hir_owner(key: DefId) -> &'tcx HirOwner<'tcx> {
query hir_owner(key: DefId) -> Option<&'tcx HirOwner<'tcx>> {
eval_always
}

// The HIR items which do not themselves have a `DefId` and are owned by another HIR item
// with a `DefId`.
// This can be conveniently accessed by methods on `tcx.hir()`.
// Avoid calling this query directly.
query hir_owner_items(key: DefId) -> &'tcx HirOwnerItems<'tcx> {
query hir_owner_items(key: DefId) -> Option<&'tcx HirOwnerItems<'tcx>> {
eval_always
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/issues/issue-70041.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// compile-flags: --edition=2018

macro_rules! regex {
() => {};
}

#[allow(dead_code)]
use regex;

fn main() {}

0 comments on commit 6207e8d

Please sign in to comment.