From 37bee6daad3a91fc60817db5180e1231b542c400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 16 Mar 2020 19:17:40 +0100 Subject: [PATCH] Allow `hir().find` to return `None` --- src/librustc/hir/map/mod.rs | 25 ++++++++++++++++--------- src/librustc/hir/mod.rs | 7 +++---- src/librustc/query/mod.rs | 4 ++-- src/test/ui/issues/issue-70041.rs | 13 +++++++++++++ src/test/ui/issues/issue-70041.stderr | 19 +++++++++++++++++++ 5 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 src/test/ui/issues/issue-70041.rs create mode 100644 src/test/ui/issues/issue-70041.stderr diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 55ed07a97d168..67d43d5f98f18 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -342,20 +342,25 @@ impl<'hir> Map<'hir> { } fn find_entry(&self, id: HirId) -> Option> { - 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, @@ -380,6 +385,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() @@ -541,8 +547,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> { - 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 diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 3b69fc8d8f2ac..9fdcaadde4f85 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -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); } diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs index ff3a82e53639e..19a9f6b2d5ac0 100644 --- a/src/librustc/query/mod.rs +++ b/src/librustc/query/mod.rs @@ -74,7 +74,7 @@ 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 } @@ -82,7 +82,7 @@ rustc_queries! { // 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 } diff --git a/src/test/ui/issues/issue-70041.rs b/src/test/ui/issues/issue-70041.rs new file mode 100644 index 0000000000000..22e42295eedf3 --- /dev/null +++ b/src/test/ui/issues/issue-70041.rs @@ -0,0 +1,13 @@ +// compile-flags: --edition=2018 +// run-pass + +macro_rules! regex { + //~^ WARN unused macro definition + () => {}; +} + +#[allow(dead_code)] +use regex; +//~^ WARN unused import + +fn main() {} diff --git a/src/test/ui/issues/issue-70041.stderr b/src/test/ui/issues/issue-70041.stderr new file mode 100644 index 0000000000000..b180175c5ab76 --- /dev/null +++ b/src/test/ui/issues/issue-70041.stderr @@ -0,0 +1,19 @@ +warning: unused macro definition + --> $DIR/issue-70041.rs:4:1 + | +LL | / macro_rules! regex { +LL | | +LL | | () => {}; +LL | | } + | |_^ + | + = note: `#[warn(unused_macros)]` on by default + +warning: unused import: `regex` + --> $DIR/issue-70041.rs:10:5 + | +LL | use regex; + | ^^^^^ + | + = note: `#[warn(unused_imports)]` on by default +