]> git.lizzy.rs Git - rust.git/commitdiff
Allow `hir().find` to return `None`
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Mon, 16 Mar 2020 18:17:40 +0000 (19:17 +0100)
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Sat, 21 Mar 2020 19:12:55 +0000 (20:12 +0100)
src/librustc/hir/map/mod.rs
src/librustc/hir/mod.rs
src/librustc/query/mod.rs
src/test/ui/issues/issue-70041.rs [new file with mode: 0644]
src/test/ui/issues/issue-70041.stderr [new file with mode: 0644]

index 13257e7bf47701bd6d6013f5d1129e0cd4ced85c..49b7ce3445ba827901fc2cebdca8326e59307ccf 100644 (file)
@@ -337,23 +337,28 @@ pub fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
     }
 
     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(0) {
             let owner = self.tcx.hir_owner(id.owner);
-            Entry { parent: owner.parent, node: owner.node }
+            owner.map(|owner| Entry { parent: owner.parent, node: owner.node })
         } else {
             let owner = self.tcx.hir_owner_nodes(id.owner);
-            let node = owner.nodes[id.local_id].as_ref().unwrap();
-            // FIXME(eddyb) use a single generic type insted of having both
-            // `Entry` and `ParentedNode`, which are effectively the same.
-            // Alternatively, rewrite code using `Entry` to use `ParentedNode`.
-            Entry { parent: HirId { owner: id.owner, local_id: node.parent }, node: node.node }
+            owner.and_then(|owner| {
+                let node = owner.nodes[id.local_id].as_ref();
+                // FIXME(eddyb) use a single generic type insted of having both
+                // `Entry` and `ParentedNode`, which are effectively the same.
+                // Alternatively, rewrite code using `Entry` to use `ParentedNode`.
+                node.map(|node| Entry {
+                    parent: HirId { owner: id.owner, local_id: node.parent },
+                    node: node.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,
@@ -376,7 +381,7 @@ pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
     }
 
     pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
-        self.tcx.hir_owner_nodes(id.hir_id.owner).bodies.get(&id.hir_id.local_id).unwrap()
+        self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies.get(&id.hir_id.local_id).unwrap()
     }
 
     pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
@@ -536,8 +541,9 @@ pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'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
index d9dfd2961ff17508e73e4947caaee7b9b7fcbd38..ce8e1f48daa77dcf5c0e925b90e743c3a7dca14b 100644 (file)
@@ -78,9 +78,8 @@ pub fn provide(providers: &mut Providers<'_>) {
         let module = hir.as_local_hir_id(id.to_def_id()).unwrap();
         &tcx.untracked_crate.modules[&module]
     };
-    providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature.unwrap();
-    providers.hir_owner_nodes = |tcx, id| {
-        tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|nodes| &**nodes).unwrap()
-    };
+    providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
+    providers.hir_owner_nodes =
+        |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|nodes| &**nodes);
     map::provide(providers);
 }
index 00e40faa95c304fef90503e99be88ec9a4c0586e..54f5103f736ec07875cb2174d5176415dbf0e1e8 100644 (file)
@@ -76,7 +76,7 @@ fn describe_as_module(def_id: DefId, tcx: TyCtxt<'_>) -> String {
         //
         // This can be conveniently accessed by methods on `tcx.hir()`.
         // Avoid calling this query directly.
-        query hir_owner(key: LocalDefId) -> &'tcx crate::hir::Owner<'tcx> {
+        query hir_owner(key: LocalDefId) -> Option<&'tcx crate::hir::Owner<'tcx>> {
             eval_always
             desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
         }
@@ -85,7 +85,7 @@ fn describe_as_module(def_id: DefId, tcx: TyCtxt<'_>) -> String {
         //
         // This can be conveniently accessed by methods on `tcx.hir()`.
         // Avoid calling this query directly.
-        query hir_owner_nodes(key: LocalDefId) -> &'tcx crate::hir::OwnerNodes<'tcx> {
+        query hir_owner_nodes(key: LocalDefId) -> Option<&'tcx crate::hir::OwnerNodes<'tcx>> {
             eval_always
             desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
         }
diff --git a/src/test/ui/issues/issue-70041.rs b/src/test/ui/issues/issue-70041.rs
new file mode 100644 (file)
index 0000000..22e4229
--- /dev/null
@@ -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 (file)
index 0000000..b180175
--- /dev/null
@@ -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
+