diff --git a/src/librustc/cfg/construct.rs b/src/librustc/cfg/construct.rs index 6122fe6370940..669c2998d1cb2 100644 --- a/src/librustc/cfg/construct.rs +++ b/src/librustc/cfg/construct.rs @@ -99,7 +99,6 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> { } fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex { - let hir_id = self.tcx.hir().node_to_hir_id(stmt.id); let exit = match stmt.node { hir::StmtKind::Local(ref local) => { let init_exit = self.opt_expr(&local.init, pred); @@ -113,7 +112,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> { self.expr(&expr, pred) } }; - self.add_ast_node(hir_id.local_id, &[exit]) + self.add_ast_node(stmt.hir_id.local_id, &[exit]) } fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex { diff --git a/src/librustc/hir/map/collector.rs b/src/librustc/hir/map/collector.rs index 7cc5d756ff311..9c4fa9e127287 100644 --- a/src/librustc/hir/map/collector.rs +++ b/src/librustc/hir/map/collector.rs @@ -1,5 +1,6 @@ use super::*; use dep_graph::{DepGraph, DepKind, DepNodeIndex}; +use hir; use hir::def_id::{LOCAL_CRATE, CrateNum}; use hir::intravisit::{Visitor, NestedVisitorMap}; use rustc_data_structures::svh::Svh; @@ -28,6 +29,8 @@ pub(super) struct NodeCollector<'a, 'hir> { /// The parent of this node parent_node: NodeId, + parent_hir: hir::HirId, + // These fields keep track of the currently relevant DepNodes during // the visitor's traversal. current_dep_node_owner: DefIndex, @@ -145,6 +148,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { source_map: sess.source_map(), map: repeat(None).take(sess.current_node_id_count()).collect(), parent_node: CRATE_NODE_ID, + parent_hir: hir::CRATE_HIR_ID, current_signature_dep_index: root_mod_sig_dep_index, current_full_dep_index: root_mod_full_dep_index, current_dep_node_owner: CRATE_DEF_INDEX, @@ -156,6 +160,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { }; collector.insert_entry(CRATE_NODE_ID, Entry { parent: CRATE_NODE_ID, + parent_hir: hir::CRATE_HIR_ID, dep_node: root_mod_sig_dep_index, node: Node::Crate, }); @@ -226,6 +231,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { fn insert(&mut self, span: Span, id: NodeId, node: Node<'hir>) { let entry = Entry { parent: self.parent_node, + parent_hir: self.parent_hir, dep_node: if self.currently_in_body { self.current_full_dep_index } else { diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index 1b7445199475c..4c622adefbdb1 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -467,6 +467,21 @@ impl Definitions { } } + // FIXME(@ljedrz): replace the NodeId variant + #[inline] + pub fn as_local_hir_id(&self, def_id: DefId) -> Option { + if def_id.krate == LOCAL_CRATE { + let hir_id = self.def_index_to_hir_id(def_id.index); + if hir_id != hir::DUMMY_HIR_ID { + Some(hir_id) + } else { + None + } + } else { + None + } + } + #[inline] pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId { self.node_to_hir_id[node_id] diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 9066f2238cf24..977ab05b20932 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -40,6 +40,7 @@ pub const REGULAR_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High; #[derive(Copy, Clone, Debug)] pub struct Entry<'hir> { parent: NodeId, + parent_hir: HirId, dep_node: DepNodeIndex, node: Node<'hir>, } @@ -208,6 +209,12 @@ impl<'hir> Map<'hir> { } } + // FIXME(@ljedrz): replace the NodeId variant + pub fn read_by_hir_id(&self, hir_id: HirId) { + let node_id = self.hir_to_node_id(hir_id); + self.read(node_id); + } + #[inline] pub fn definitions(&self) -> &'hir Definitions { self.definitions @@ -224,6 +231,11 @@ impl<'hir> Map<'hir> { }) } + // FIXME(@ljedrz): replace the NodeId variant + pub fn def_path_from_hir_id(&self, id: HirId) -> DefPath { + self.def_path(self.local_def_id_from_hir_id(id)) + } + pub fn def_path(&self, def_id: DefId) -> DefPath { assert!(def_id.is_local()); self.definitions.def_path(def_id.index) @@ -237,6 +249,23 @@ impl<'hir> Map<'hir> { }) } + // FIXME(@ljedrz): replace the NodeId variant + #[inline] + pub fn local_def_id_from_hir_id(&self, hir_id: HirId) -> DefId { + let node_id = self.hir_to_node_id(hir_id); + self.opt_local_def_id(node_id).unwrap_or_else(|| { + bug!("local_def_id_from_hir_id: no entry for `{:?}`, which has a map of `{:?}`", + hir_id, self.find_entry(node_id)) + }) + } + + // FIXME(@ljedrz): replace the NodeId variant + #[inline] + pub fn opt_local_def_id_from_hir_id(&self, hir_id: HirId) -> Option { + let node_id = self.hir_to_node_id(hir_id); + self.definitions.opt_local_def_id(node_id) + } + #[inline] pub fn opt_local_def_id(&self, node: NodeId) -> Option { self.definitions.opt_local_def_id(node) @@ -247,6 +276,12 @@ impl<'hir> Map<'hir> { self.definitions.as_local_node_id(def_id) } + // FIXME(@ljedrz): replace the NodeId variant + #[inline] + pub fn as_local_hir_id(&self, def_id: DefId) -> Option { + self.definitions.as_local_hir_id(def_id) + } + #[inline] pub fn hir_to_node_id(&self, hir_id: HirId) -> NodeId { self.hir_to_node_id[&hir_id] @@ -566,6 +601,12 @@ impl<'hir> Map<'hir> { self.find(id).unwrap_or_else(|| bug!("couldn't find node id {} in the AST map", id)) } + // FIXME(@ljedrz): replace the NodeId variant + pub fn get_by_hir_id(&self, id: HirId) -> Node<'hir> { + let node_id = self.hir_to_node_id(id); + self.get(node_id) + } + pub fn get_if_local(&self, id: DefId) -> Option> { self.as_local_node_id(id).map(|id| self.get(id)) // read recorded by `get` } @@ -613,6 +654,12 @@ impl<'hir> Map<'hir> { result } + // FIXME(@ljedrz): replace the NodeId variant + pub fn find_by_hir_id(&self, hir_id: HirId) -> Option> { + let node_id = self.hir_to_node_id(hir_id); + self.find(node_id) + } + /// Similar to `get_parent`; returns the parent node-id, or own `id` if there is /// no parent. Note that the parent may be `CRATE_NODE_ID`, which is not itself /// present in the map -- so passing the return value of get_parent_node to @@ -633,6 +680,13 @@ impl<'hir> Map<'hir> { self.find_entry(id).and_then(|x| x.parent_node()).unwrap_or(id) } + // FIXME(@ljedrz): replace the NodeId variant + pub fn get_parent_node_by_hir_id(&self, id: HirId) -> HirId { + let node_id = self.hir_to_node_id(id); + let parent_node_id = self.get_parent_node(node_id); + self.node_to_hir_id(parent_node_id) + } + /// Check if the node is an argument. An argument is a local variable whose /// immediate parent is an item or a closure. pub fn is_argument(&self, id: NodeId) -> bool { @@ -757,6 +811,13 @@ impl<'hir> Map<'hir> { } } + // FIXME(@ljedrz): replace the NodeId variant + pub fn get_parent_item(&self, id: HirId) -> HirId { + let node_id = self.hir_to_node_id(id); + let parent_node_id = self.get_parent(node_id); + self.node_to_hir_id(parent_node_id) + } + /// Returns the `DefId` of `id`'s nearest module parent, or `id` itself if no /// module parent is in this map. pub fn get_module_parent(&self, id: NodeId) -> DefId { @@ -814,6 +875,12 @@ impl<'hir> Map<'hir> { } } + // FIXME(@ljedrz): replace the NodeId variant + pub fn expect_item_by_hir_id(&self, id: HirId) -> &'hir Item { + let node_id = self.hir_to_node_id(id); + self.expect_item(node_id) + } + pub fn expect_impl_item(&self, id: NodeId) -> &'hir ImplItem { match self.find(id) { Some(Node::ImplItem(item)) => item, @@ -960,13 +1027,28 @@ impl<'hir> Map<'hir> { node_id_to_string(self, id, true) } + // FIXME(@ljedrz): replace the NodeId variant + pub fn hir_to_string(&self, id: HirId) -> String { + hir_id_to_string(self, id, true) + } + pub fn node_to_user_string(&self, id: NodeId) -> String { node_id_to_string(self, id, false) } + // FIXME(@ljedrz): replace the NodeId variant + pub fn hir_to_user_string(&self, id: HirId) -> String { + hir_id_to_string(self, id, false) + } + pub fn node_to_pretty_string(&self, id: NodeId) -> String { print::to_string(self, |s| s.print_node(self.get(id))) } + + // FIXME(@ljedrz): replace the NodeId variant + pub fn hir_to_pretty_string(&self, id: HirId) -> String { + print::to_string(self, |s| s.print_node(self.get_by_hir_id(id))) + } } pub struct NodesMatchingSuffix<'a, 'hir:'a> { @@ -1310,6 +1392,12 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String { } } +// FIXME(@ljedrz): replace the NodeId variant +fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String { + let node_id = map.hir_to_node_id(id); + node_id_to_string(map, node_id, include_id) +} + pub fn describe_def(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option { if let Some(node_id) = tcx.hir().as_local_node_id(def_id) { tcx.hir().describe_def(node_id) diff --git a/src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs b/src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs index eeaa01375ed4d..ebaef4977f400 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs @@ -114,7 +114,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> { hir::TyKind::Rptr(ref lifetime, _) => { // the lifetime of the TyRptr - let hir_id = self.tcx.hir().node_to_hir_id(lifetime.id); + let hir_id = lifetime.hir_id; match (self.tcx.named_region(hir_id), self.bound_region) { // Find the index of the anonymous region that was part of the // error. We will then search the function parameters for a bound @@ -221,8 +221,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> { } fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) { - let hir_id = self.tcx.hir().node_to_hir_id(lifetime.id); - match (self.tcx.named_region(hir_id), self.bound_region) { + match (self.tcx.named_region(lifetime.hir_id), self.bound_region) { // the lifetime of the TyPath! (Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => { if debruijn_index == self.current_index && anon_index == br_index { diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index 31f91a1bae57f..db52cc3074b9a 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -840,7 +840,7 @@ fn resolve_pat<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, pat: & } fn resolve_stmt<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, stmt: &'tcx hir::Stmt) { - let stmt_id = visitor.tcx.hir().node_to_hir_id(stmt.id).local_id; + let stmt_id = stmt.hir_id.local_id; debug!("resolve_stmt(stmt.id={:?})", stmt_id); // Every statement will clean up the temporaries created during diff --git a/src/librustc/ty/item_path.rs b/src/librustc/ty/item_path.rs index 0ddc5ae87208d..adb7e1fb3e322 100644 --- a/src/librustc/ty/item_path.rs +++ b/src/librustc/ty/item_path.rs @@ -1,3 +1,4 @@ +use hir; use hir::map::DefPathData; use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use ty::{self, DefIdTree, Ty, TyCtxt}; @@ -76,6 +77,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { self.item_path_str(self.hir().local_def_id(id)) } + // FIXME(@ljedrz): replace the NodeId variant + pub fn hir_path_str(self, id: hir::HirId) -> String { + self.item_path_str(self.hir().local_def_id_from_hir_id(id)) + } + /// Returns a string identifying this def-id. This string is /// suitable for user output. It always begins with a crate identifier. pub fn absolute_item_path_str(self, def_id: DefId) -> String { diff --git a/src/librustc_mir/hair/cx/block.rs b/src/librustc_mir/hair/cx/block.rs index c50d9ddcb152e..518ae978ae17a 100644 --- a/src/librustc_mir/hair/cx/block.rs +++ b/src/librustc_mir/hair/cx/block.rs @@ -46,7 +46,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, -> Vec> { let mut result = vec![]; for (index, stmt) in stmts.iter().enumerate() { - let hir_id = cx.tcx.hir().node_to_hir_id(stmt.id); + let hir_id = stmt.hir_id; let opt_dxn_ext = cx.region_scope_tree.opt_destruction_scope(hir_id.local_id); let stmt_span = StatementSpan(cx.tcx.hir().span(stmt.id)); match stmt.node { diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 61674070fbcb2..8da0b6dcbeac3 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -114,8 +114,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { tcx.hir().name(tcx.hir().as_local_node_id(def_id).unwrap()).as_interned_str() }; - let hir_id = tcx.hir().node_to_hir_id(lifetime.id); - let r = match tcx.named_region(hir_id) { + let r = match tcx.named_region(lifetime.hir_id) { Some(rl::Region::Static) => { tcx.types.re_static } @@ -1145,8 +1144,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o { self.ast_region_to_region(lifetime, None) } else { self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| { - let hir_id = tcx.hir().node_to_hir_id(lifetime.id); - if tcx.named_region(hir_id).is_some() { + if tcx.named_region(lifetime.hir_id).is_some() { self.ast_region_to_region(lifetime, None) } else { self.re_infer(span, None).unwrap_or_else(|| { diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 1c8dffaf628c4..9dc74c5d63a4e 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -814,8 +814,7 @@ fn has_late_bound_regions<'a, 'tcx>( return; } - let hir_id = self.tcx.hir().node_to_hir_id(lt.id); - match self.tcx.named_region(hir_id) { + match self.tcx.named_region(lt.hir_id) { Some(rl::Region::Static) | Some(rl::Region::EarlyBound(..)) => {} Some(rl::Region::LateBound(debruijn, _, _)) | Some(rl::Region::LateBoundAnon(debruijn, _)) if debruijn < self.outer_index => {} @@ -841,8 +840,7 @@ fn has_late_bound_regions<'a, 'tcx>( }; for param in &generics.params { if let GenericParamKind::Lifetime { .. } = param.kind { - let hir_id = tcx.hir().node_to_hir_id(param.id); - if tcx.is_late_bound(hir_id) { + if tcx.is_late_bound(param.hir_id) { return Some(param.span); } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index c328dbe82e4b1..116e46df3c10a 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1210,8 +1210,7 @@ impl Lifetime { impl Clean for hir::Lifetime { fn clean(&self, cx: &DocContext) -> Lifetime { if self.id != ast::DUMMY_NODE_ID { - let hir_id = cx.tcx.hir().node_to_hir_id(self.id); - let def = cx.tcx.named_region(hir_id); + let def = cx.tcx.named_region(self.hir_id); match def { Some(rl::Region::EarlyBound(_, node_id, _)) | Some(rl::Region::LateBound(_, node_id, _)) |