Skip to content

Commit 6207e8d

Browse files
committed
Allow hir().find to return None
1 parent 8e6de32 commit 6207e8d

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

src/librustc/hir/map/mod.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -341,20 +341,25 @@ impl<'hir> Map<'hir> {
341341
}
342342

343343
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
344-
Some(self.get_entry(id))
345-
}
346-
347-
fn get_entry(&self, id: HirId) -> Entry<'hir> {
348344
if id.local_id == ItemLocalId::from_u32_const(0) {
349345
let owner = self.tcx.hir_owner(id.owner_def_id());
350-
Entry { parent: owner.parent, node: owner.node }
346+
owner.map(|owner| Entry { parent: owner.parent, node: owner.node })
351347
} else {
352348
let owner = self.tcx.hir_owner_items(id.owner_def_id());
353-
let item = owner.items[id.local_id].as_ref().unwrap();
354-
Entry { parent: HirId { owner: id.owner, local_id: item.parent }, node: item.node }
349+
owner.and_then(|owner| {
350+
let item = owner.items[id.local_id].as_ref();
351+
item.map(|item| Entry {
352+
parent: HirId { owner: id.owner, local_id: item.parent },
353+
node: item.node,
354+
})
355+
})
355356
}
356357
}
357358

359+
fn get_entry(&self, id: HirId) -> Entry<'hir> {
360+
self.find_entry(id).unwrap()
361+
}
362+
358363
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
359364
match self.find(id).unwrap() {
360365
Node::Item(item) => item,
@@ -379,6 +384,7 @@ impl<'hir> Map<'hir> {
379384
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
380385
self.tcx
381386
.hir_owner_items(DefId::local(id.hir_id.owner))
387+
.unwrap()
382388
.bodies
383389
.get(&id.hir_id.local_id)
384390
.unwrap()
@@ -540,8 +546,9 @@ impl<'hir> Map<'hir> {
540546

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

547554
/// Similar to `get_parent`; returns the parent HIR Id, or just `hir_id` if there

src/librustc/hir/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ pub fn provide(providers: &mut Providers<'_>) {
7878
let module = hir.as_local_hir_id(id).unwrap();
7979
&tcx.untracked_crate.modules[&module]
8080
};
81-
providers.hir_owner = |tcx, id| tcx.index_hir(id.krate).map[id.index].signature.unwrap();
82-
providers.hir_owner_items = |tcx, id| {
83-
tcx.index_hir(id.krate).map[id.index].with_bodies.as_ref().map(|items| &**items).unwrap()
84-
};
81+
providers.hir_owner = |tcx, id| tcx.index_hir(id.krate).map[id.index].signature;
82+
providers.hir_owner_items =
83+
|tcx, id| tcx.index_hir(id.krate).map[id.index].with_bodies.as_ref().map(|items| &**items);
8584
map::provide(providers);
8685
}

src/librustc/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ rustc_queries! {
7474
// a `DefId`.
7575
// This can be conveniently accessed by methods on `tcx.hir()`.
7676
// Avoid calling this query directly.
77-
query hir_owner(key: DefId) -> &'tcx HirOwner<'tcx> {
77+
query hir_owner(key: DefId) -> Option<&'tcx HirOwner<'tcx>> {
7878
eval_always
7979
}
8080

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

src/test/ui/issues/issue-70041.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// compile-flags: --edition=2018
2+
3+
macro_rules! regex {
4+
() => {};
5+
}
6+
7+
#[allow(dead_code)]
8+
use regex;
9+
10+
fn main() {}

0 commit comments

Comments
 (0)