Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/librustc/cfg/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 39 additions & 1 deletion src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use syntax::ast;
use syntax::ext::hygiene::Mark;
use syntax::symbol::{Symbol, InternedString};
use syntax_pos::{Span, DUMMY_SP};
use util::nodemap::NodeMap;
use util::nodemap::{HirIdMap, NodeMap};

/// The DefPathTable maps DefIndexes to DefKeys and vice versa.
/// Internally the DefPathTable holds a tree of DefKeys, where each DefKey
Expand Down Expand Up @@ -147,6 +147,7 @@ impl Decodable for DefPathTable {
pub struct Definitions {
table: DefPathTable,
node_to_def_index: NodeMap<DefIndex>,
hir_to_def_index: HirIdMap<DefIndex>,
def_index_to_node: [Vec<ast::NodeId>; 2],
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
/// If `Mark` is an ID of some macro expansion,
Expand Down Expand Up @@ -441,16 +442,34 @@ impl Definitions {
self.node_to_def_index.get(&node).cloned()
}

// FIXME(@ljedrz): replace the NodeId variant
#[inline]
pub fn opt_def_index_from_hir_id(&self, hir: hir::HirId) -> Option<DefIndex> {
self.hir_to_def_index.get(&hir).cloned()
}

#[inline]
pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
self.opt_def_index(node).map(DefId::local)
}

// FIXME(@ljedrz): replace the NodeId variant
#[inline]
pub fn opt_local_def_id_from_hir_id(&self, hir: hir::HirId) -> Option<DefId> {
self.opt_def_index_from_hir_id(hir).map(DefId::local)
}

#[inline]
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
self.opt_local_def_id(node).unwrap()
}

// FIXME(@ljedrz): replace the NodeId variant
#[inline]
pub fn local_def_id_from_hir_id(&self, hir: hir::HirId) -> DefId {
self.opt_local_def_id_from_hir_id(hir).unwrap()
}

#[inline]
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
if def_id.krate == LOCAL_CRATE {
Expand All @@ -467,6 +486,21 @@ impl Definitions {
}
}

// FIXME(@ljedrz): replace the NodeId variant
#[inline]
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<hir::HirId> {
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]
Expand Down Expand Up @@ -515,6 +549,7 @@ impl Definitions {
assert!(self.def_index_to_node[address_space.index()].is_empty());
self.def_index_to_node[address_space.index()].push(ast::CRATE_NODE_ID);
self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
self.hir_to_def_index.insert(hir::CRATE_HIR_ID, root_index);

// Allocate some other DefIndices that always must exist.
GlobalMetaDataKind::allocate_def_indices(self);
Expand Down Expand Up @@ -575,6 +610,9 @@ impl Definitions {
if node_id != ast::DUMMY_NODE_ID {
debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
self.node_to_def_index.insert(node_id, index);
if let Some(hir_id) = self.node_to_hir_id.get(node_id) {
self.hir_to_def_index.insert(*hir_id, index);
}
}

if expansion != Mark::root() {
Expand Down
87 changes: 87 additions & 0 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -237,6 +249,22 @@ impl<'hir> Map<'hir> {
})
}

// FIXME(@ljedrz): replace the NodeId variant
#[inline]
pub fn local_def_id_from_hir_id(&self, hir_id: HirId) -> DefId {
self.opt_local_def_id_from_hir_id(hir_id).unwrap_or_else(|| {
let node_id = self.hir_to_node_id(hir_id);
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<DefId> {
self.definitions.opt_local_def_id_from_hir_id(hir_id)
}

#[inline]
pub fn opt_local_def_id(&self, node: NodeId) -> Option<DefId> {
self.definitions.opt_local_def_id(node)
Expand All @@ -247,6 +275,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<HirId> {
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]
Expand Down Expand Up @@ -566,6 +600,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<Node<'hir>> {
self.as_local_node_id(id).map(|id| self.get(id)) // read recorded by `get`
}
Expand Down Expand Up @@ -613,6 +653,12 @@ impl<'hir> Map<'hir> {
result
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn find_by_hir_id(&self, hir_id: HirId) -> Option<Node<'hir>> {
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
Expand All @@ -633,6 +679,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 {
Expand Down Expand Up @@ -757,6 +810,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 {
Expand Down Expand Up @@ -814,6 +874,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,
Expand Down Expand Up @@ -960,13 +1026,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> {
Expand Down Expand Up @@ -1310,6 +1391,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<Def> {
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
tcx.hir().describe_def(node_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/librustc/ty/item_path.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use this - #57967 (or the refactoring that it's based on, rather, will split it into a separate PR) removes node_path_str.

If you really need to replace node_path_str uses, you can call item_path_str directly (a lot of the time you might even have a DefId that was being converted to a NodeId!).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyb thanks for the heads up, I'll keep that in mind; I'll also remove this function with a drive-by commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #58140 in the meanwhile, and it removes hir_path_str, so it's compatible with any PR also removing hir_path_str.

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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/hair/cx/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
-> Vec<StmtRef<'tcx>> {
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 {
Expand Down
Loading