Skip to content

HirIdification: add key HirId methods #58090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,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
88 changes: 88 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,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<DefId> {
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<DefId> {
self.definitions.opt_local_def_id(node)
Expand All @@ -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<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 +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<Node<'hir>> {
self.as_local_node_id(id).map(|id| self.get(id)) // read recorded by `get`
}
Expand Down Expand Up @@ -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<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 +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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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<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
6 changes: 2 additions & 4 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(|| {
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {}
Expand All @@ -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);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,8 +1210,7 @@ impl Lifetime {
impl Clean<Lifetime> 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, _)) |
Expand Down