Skip to content

Commit ce0af8a

Browse files
authored
Rollup merge of #70051 - Zoxc:opt-find, r=eddyb
Allow `hir().find` to return `None` Fixes #70041 r? @eddyb
2 parents 834ed36 + 0aa15d0 commit ce0af8a

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

src/librustc/hir/map/mod.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -337,23 +337,28 @@ impl<'hir> Map<'hir> {
337337
}
338338

339339
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
340-
Some(self.get_entry(id))
341-
}
342-
343-
fn get_entry(&self, id: HirId) -> Entry<'hir> {
344340
if id.local_id == ItemLocalId::from_u32(0) {
345341
let owner = self.tcx.hir_owner(id.owner);
346-
Entry { parent: owner.parent, node: owner.node }
342+
owner.map(|owner| Entry { parent: owner.parent, node: owner.node })
347343
} else {
348344
let owner = self.tcx.hir_owner_nodes(id.owner);
349-
let node = owner.nodes[id.local_id].as_ref().unwrap();
350-
// FIXME(eddyb) use a single generic type insted of having both
351-
// `Entry` and `ParentedNode`, which are effectively the same.
352-
// Alternatively, rewrite code using `Entry` to use `ParentedNode`.
353-
Entry { parent: HirId { owner: id.owner, local_id: node.parent }, node: node.node }
345+
owner.and_then(|owner| {
346+
let node = owner.nodes[id.local_id].as_ref();
347+
// FIXME(eddyb) use a single generic type insted of having both
348+
// `Entry` and `ParentedNode`, which are effectively the same.
349+
// Alternatively, rewrite code using `Entry` to use `ParentedNode`.
350+
node.map(|node| Entry {
351+
parent: HirId { owner: id.owner, local_id: node.parent },
352+
node: node.node,
353+
})
354+
})
354355
}
355356
}
356357

358+
fn get_entry(&self, id: HirId) -> Entry<'hir> {
359+
self.find_entry(id).unwrap()
360+
}
361+
357362
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
358363
match self.find(id).unwrap() {
359364
Node::Item(item) => item,
@@ -376,7 +381,7 @@ impl<'hir> Map<'hir> {
376381
}
377382

378383
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
379-
self.tcx.hir_owner_nodes(id.hir_id.owner).bodies.get(&id.hir_id.local_id).unwrap()
384+
self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies.get(&id.hir_id.local_id).unwrap()
380385
}
381386

382387
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
@@ -536,8 +541,9 @@ impl<'hir> Map<'hir> {
536541

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

543549
/// 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.to_def_id()).unwrap();
7979
&tcx.untracked_crate.modules[&module]
8080
};
81-
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature.unwrap();
82-
providers.hir_owner_nodes = |tcx, id| {
83-
tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|nodes| &**nodes).unwrap()
84-
};
81+
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
82+
providers.hir_owner_nodes =
83+
|tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|nodes| &**nodes);
8584
map::provide(providers);
8685
}

src/librustc/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ rustc_queries! {
7676
//
7777
// This can be conveniently accessed by methods on `tcx.hir()`.
7878
// Avoid calling this query directly.
79-
query hir_owner(key: LocalDefId) -> &'tcx crate::hir::Owner<'tcx> {
79+
query hir_owner(key: LocalDefId) -> Option<&'tcx crate::hir::Owner<'tcx>> {
8080
eval_always
8181
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
8282
}
@@ -85,7 +85,7 @@ rustc_queries! {
8585
//
8686
// This can be conveniently accessed by methods on `tcx.hir()`.
8787
// Avoid calling this query directly.
88-
query hir_owner_nodes(key: LocalDefId) -> &'tcx crate::hir::OwnerNodes<'tcx> {
88+
query hir_owner_nodes(key: LocalDefId) -> Option<&'tcx crate::hir::OwnerNodes<'tcx>> {
8989
eval_always
9090
desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
9191
}

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)