Skip to content

Commit 37bee6d

Browse files
committed
Allow hir().find to return None
1 parent 5e9ebf4 commit 37bee6d

File tree

5 files changed

+53
-15
lines changed

5 files changed

+53
-15
lines changed

src/librustc/hir/map/mod.rs

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

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

360+
fn get_entry(&self, id: HirId) -> Entry<'hir> {
361+
self.find_entry(id).unwrap()
362+
}
363+
359364
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
360365
match self.find(id).unwrap() {
361366
Node::Item(item) => item,
@@ -380,6 +385,7 @@ impl<'hir> Map<'hir> {
380385
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
381386
self.tcx
382387
.hir_owner_items(DefId::local(id.hir_id.owner))
388+
.unwrap()
383389
.bodies
384390
.get(&id.hir_id.local_id)
385391
.unwrap()
@@ -541,8 +547,9 @@ impl<'hir> Map<'hir> {
541547

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

548555
/// 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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile-flags: --edition=2018
2+
// run-pass
3+
4+
macro_rules! regex {
5+
//~^ WARN unused macro definition
6+
() => {};
7+
}
8+
9+
#[allow(dead_code)]
10+
use regex;
11+
//~^ WARN unused import
12+
13+
fn main() {}

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
warning: unused macro definition
2+
--> $DIR/issue-70041.rs:4:1
3+
|
4+
LL | / macro_rules! regex {
5+
LL | |
6+
LL | | () => {};
7+
LL | | }
8+
| |_^
9+
|
10+
= note: `#[warn(unused_macros)]` on by default
11+
12+
warning: unused import: `regex`
13+
--> $DIR/issue-70041.rs:10:5
14+
|
15+
LL | use regex;
16+
| ^^^^^
17+
|
18+
= note: `#[warn(unused_imports)]` on by default
19+

0 commit comments

Comments
 (0)