Skip to content

Commit 4314dba

Browse files
committed
Auto merge of rust-lang#58090 - ljedrz:HirIdification_phase_2, r=Zoxc
HirIdification: add key HirId methods This is another PR in a series dedicated to `HirId`-ification, i.e. deprecating `ast::NodeId`s after the AST > HIR lowering process. The bigger proof of concept can be seen in rust-lang#57578. **Phase 2**: add key `HirId` methods mirroring the `NodeId` ones. These should be counterparts of the most widely used `Hir` methods using `NodeId`s. Note that this expands `hir::map::Definitions` with an additional `hir_to_def_index` map (with the intention of later removing `node_to_def_index`). As a bonus there is also a small cleanup commit removing unnecessary calls to `node_to_hir_id` where `HirId` is already available. r? @Zoxc Cc @varkor
2 parents f6fac42 + 272f4df commit 4314dba

File tree

11 files changed

+125
-17
lines changed

11 files changed

+125
-17
lines changed

src/librustc/cfg/construct.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
9999
}
100100

101101
fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex {
102-
let hir_id = self.tcx.hir().node_to_hir_id(stmt.id);
103102
let exit = match stmt.node {
104103
hir::StmtKind::Local(ref local) => {
105104
let init_exit = self.opt_expr(&local.init, pred);
@@ -113,7 +112,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
113112
self.expr(&expr, pred)
114113
}
115114
};
116-
self.add_ast_node(hir_id.local_id, &[exit])
115+
self.add_ast_node(stmt.hir_id.local_id, &[exit])
117116
}
118117

119118
fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {

src/librustc/hir/map/collector.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use dep_graph::{DepGraph, DepKind, DepNodeIndex};
3+
use hir;
34
use hir::def_id::{LOCAL_CRATE, CrateNum};
45
use hir::intravisit::{Visitor, NestedVisitorMap};
56
use rustc_data_structures::svh::Svh;
@@ -28,6 +29,8 @@ pub(super) struct NodeCollector<'a, 'hir> {
2829
/// The parent of this node
2930
parent_node: NodeId,
3031

32+
parent_hir: hir::HirId,
33+
3134
// These fields keep track of the currently relevant DepNodes during
3235
// the visitor's traversal.
3336
current_dep_node_owner: DefIndex,
@@ -145,6 +148,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
145148
source_map: sess.source_map(),
146149
map: repeat(None).take(sess.current_node_id_count()).collect(),
147150
parent_node: CRATE_NODE_ID,
151+
parent_hir: hir::CRATE_HIR_ID,
148152
current_signature_dep_index: root_mod_sig_dep_index,
149153
current_full_dep_index: root_mod_full_dep_index,
150154
current_dep_node_owner: CRATE_DEF_INDEX,
@@ -156,6 +160,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
156160
};
157161
collector.insert_entry(CRATE_NODE_ID, Entry {
158162
parent: CRATE_NODE_ID,
163+
parent_hir: hir::CRATE_HIR_ID,
159164
dep_node: root_mod_sig_dep_index,
160165
node: Node::Crate,
161166
});
@@ -226,6 +231,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
226231
fn insert(&mut self, span: Span, id: NodeId, node: Node<'hir>) {
227232
let entry = Entry {
228233
parent: self.parent_node,
234+
parent_hir: self.parent_hir,
229235
dep_node: if self.currently_in_body {
230236
self.current_full_dep_index
231237
} else {

src/librustc/hir/map/definitions.rs

+15
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,21 @@ impl Definitions {
467467
}
468468
}
469469

470+
// FIXME(@ljedrz): replace the NodeId variant
471+
#[inline]
472+
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<hir::HirId> {
473+
if def_id.krate == LOCAL_CRATE {
474+
let hir_id = self.def_index_to_hir_id(def_id.index);
475+
if hir_id != hir::DUMMY_HIR_ID {
476+
Some(hir_id)
477+
} else {
478+
None
479+
}
480+
} else {
481+
None
482+
}
483+
}
484+
470485
#[inline]
471486
pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
472487
self.node_to_hir_id[node_id]

src/librustc/hir/map/mod.rs

+88
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub const REGULAR_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
4040
#[derive(Copy, Clone, Debug)]
4141
pub struct Entry<'hir> {
4242
parent: NodeId,
43+
parent_hir: HirId,
4344
dep_node: DepNodeIndex,
4445
node: Node<'hir>,
4546
}
@@ -208,6 +209,12 @@ impl<'hir> Map<'hir> {
208209
}
209210
}
210211

212+
// FIXME(@ljedrz): replace the NodeId variant
213+
pub fn read_by_hir_id(&self, hir_id: HirId) {
214+
let node_id = self.hir_to_node_id(hir_id);
215+
self.read(node_id);
216+
}
217+
211218
#[inline]
212219
pub fn definitions(&self) -> &'hir Definitions {
213220
self.definitions
@@ -224,6 +231,11 @@ impl<'hir> Map<'hir> {
224231
})
225232
}
226233

234+
// FIXME(@ljedrz): replace the NodeId variant
235+
pub fn def_path_from_hir_id(&self, id: HirId) -> DefPath {
236+
self.def_path(self.local_def_id_from_hir_id(id))
237+
}
238+
227239
pub fn def_path(&self, def_id: DefId) -> DefPath {
228240
assert!(def_id.is_local());
229241
self.definitions.def_path(def_id.index)
@@ -237,6 +249,23 @@ impl<'hir> Map<'hir> {
237249
})
238250
}
239251

252+
// FIXME(@ljedrz): replace the NodeId variant
253+
#[inline]
254+
pub fn local_def_id_from_hir_id(&self, hir_id: HirId) -> DefId {
255+
let node_id = self.hir_to_node_id(hir_id);
256+
self.opt_local_def_id(node_id).unwrap_or_else(|| {
257+
bug!("local_def_id_from_hir_id: no entry for `{:?}`, which has a map of `{:?}`",
258+
hir_id, self.find_entry(node_id))
259+
})
260+
}
261+
262+
// FIXME(@ljedrz): replace the NodeId variant
263+
#[inline]
264+
pub fn opt_local_def_id_from_hir_id(&self, hir_id: HirId) -> Option<DefId> {
265+
let node_id = self.hir_to_node_id(hir_id);
266+
self.definitions.opt_local_def_id(node_id)
267+
}
268+
240269
#[inline]
241270
pub fn opt_local_def_id(&self, node: NodeId) -> Option<DefId> {
242271
self.definitions.opt_local_def_id(node)
@@ -247,6 +276,12 @@ impl<'hir> Map<'hir> {
247276
self.definitions.as_local_node_id(def_id)
248277
}
249278

279+
// FIXME(@ljedrz): replace the NodeId variant
280+
#[inline]
281+
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
282+
self.definitions.as_local_hir_id(def_id)
283+
}
284+
250285
#[inline]
251286
pub fn hir_to_node_id(&self, hir_id: HirId) -> NodeId {
252287
self.hir_to_node_id[&hir_id]
@@ -566,6 +601,12 @@ impl<'hir> Map<'hir> {
566601
self.find(id).unwrap_or_else(|| bug!("couldn't find node id {} in the AST map", id))
567602
}
568603

604+
// FIXME(@ljedrz): replace the NodeId variant
605+
pub fn get_by_hir_id(&self, id: HirId) -> Node<'hir> {
606+
let node_id = self.hir_to_node_id(id);
607+
self.get(node_id)
608+
}
609+
569610
pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
570611
self.as_local_node_id(id).map(|id| self.get(id)) // read recorded by `get`
571612
}
@@ -613,6 +654,12 @@ impl<'hir> Map<'hir> {
613654
result
614655
}
615656

657+
// FIXME(@ljedrz): replace the NodeId variant
658+
pub fn find_by_hir_id(&self, hir_id: HirId) -> Option<Node<'hir>> {
659+
let node_id = self.hir_to_node_id(hir_id);
660+
self.find(node_id)
661+
}
662+
616663
/// Similar to `get_parent`; returns the parent node-id, or own `id` if there is
617664
/// no parent. Note that the parent may be `CRATE_NODE_ID`, which is not itself
618665
/// present in the map -- so passing the return value of get_parent_node to
@@ -633,6 +680,13 @@ impl<'hir> Map<'hir> {
633680
self.find_entry(id).and_then(|x| x.parent_node()).unwrap_or(id)
634681
}
635682

683+
// FIXME(@ljedrz): replace the NodeId variant
684+
pub fn get_parent_node_by_hir_id(&self, id: HirId) -> HirId {
685+
let node_id = self.hir_to_node_id(id);
686+
let parent_node_id = self.get_parent_node(node_id);
687+
self.node_to_hir_id(parent_node_id)
688+
}
689+
636690
/// Check if the node is an argument. An argument is a local variable whose
637691
/// immediate parent is an item or a closure.
638692
pub fn is_argument(&self, id: NodeId) -> bool {
@@ -757,6 +811,13 @@ impl<'hir> Map<'hir> {
757811
}
758812
}
759813

814+
// FIXME(@ljedrz): replace the NodeId variant
815+
pub fn get_parent_item(&self, id: HirId) -> HirId {
816+
let node_id = self.hir_to_node_id(id);
817+
let parent_node_id = self.get_parent(node_id);
818+
self.node_to_hir_id(parent_node_id)
819+
}
820+
760821
/// Returns the `DefId` of `id`'s nearest module parent, or `id` itself if no
761822
/// module parent is in this map.
762823
pub fn get_module_parent(&self, id: NodeId) -> DefId {
@@ -814,6 +875,12 @@ impl<'hir> Map<'hir> {
814875
}
815876
}
816877

878+
// FIXME(@ljedrz): replace the NodeId variant
879+
pub fn expect_item_by_hir_id(&self, id: HirId) -> &'hir Item {
880+
let node_id = self.hir_to_node_id(id);
881+
self.expect_item(node_id)
882+
}
883+
817884
pub fn expect_impl_item(&self, id: NodeId) -> &'hir ImplItem {
818885
match self.find(id) {
819886
Some(Node::ImplItem(item)) => item,
@@ -960,13 +1027,28 @@ impl<'hir> Map<'hir> {
9601027
node_id_to_string(self, id, true)
9611028
}
9621029

1030+
// FIXME(@ljedrz): replace the NodeId variant
1031+
pub fn hir_to_string(&self, id: HirId) -> String {
1032+
hir_id_to_string(self, id, true)
1033+
}
1034+
9631035
pub fn node_to_user_string(&self, id: NodeId) -> String {
9641036
node_id_to_string(self, id, false)
9651037
}
9661038

1039+
// FIXME(@ljedrz): replace the NodeId variant
1040+
pub fn hir_to_user_string(&self, id: HirId) -> String {
1041+
hir_id_to_string(self, id, false)
1042+
}
1043+
9671044
pub fn node_to_pretty_string(&self, id: NodeId) -> String {
9681045
print::to_string(self, |s| s.print_node(self.get(id)))
9691046
}
1047+
1048+
// FIXME(@ljedrz): replace the NodeId variant
1049+
pub fn hir_to_pretty_string(&self, id: HirId) -> String {
1050+
print::to_string(self, |s| s.print_node(self.get_by_hir_id(id)))
1051+
}
9701052
}
9711053

9721054
pub struct NodesMatchingSuffix<'a, 'hir:'a> {
@@ -1310,6 +1392,12 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
13101392
}
13111393
}
13121394

1395+
// FIXME(@ljedrz): replace the NodeId variant
1396+
fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1397+
let node_id = map.hir_to_node_id(id);
1398+
node_id_to_string(map, node_id, include_id)
1399+
}
1400+
13131401
pub fn describe_def(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<Def> {
13141402
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
13151403
tcx.hir().describe_def(node_id)

src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
114114

115115
hir::TyKind::Rptr(ref lifetime, _) => {
116116
// the lifetime of the TyRptr
117-
let hir_id = self.tcx.hir().node_to_hir_id(lifetime.id);
117+
let hir_id = lifetime.hir_id;
118118
match (self.tcx.named_region(hir_id), self.bound_region) {
119119
// Find the index of the anonymous region that was part of the
120120
// 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> {
221221
}
222222

223223
fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
224-
let hir_id = self.tcx.hir().node_to_hir_id(lifetime.id);
225-
match (self.tcx.named_region(hir_id), self.bound_region) {
224+
match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
226225
// the lifetime of the TyPath!
227226
(Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => {
228227
if debruijn_index == self.current_index && anon_index == br_index {

src/librustc/middle/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ fn resolve_pat<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, pat: &
840840
}
841841

842842
fn resolve_stmt<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, stmt: &'tcx hir::Stmt) {
843-
let stmt_id = visitor.tcx.hir().node_to_hir_id(stmt.id).local_id;
843+
let stmt_id = stmt.hir_id.local_id;
844844
debug!("resolve_stmt(stmt.id={:?})", stmt_id);
845845

846846
// Every statement will clean up the temporaries created during

src/librustc/ty/item_path.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use hir;
12
use hir::map::DefPathData;
23
use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
34
use ty::{self, DefIdTree, Ty, TyCtxt};
@@ -76,6 +77,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
7677
self.item_path_str(self.hir().local_def_id(id))
7778
}
7879

80+
// FIXME(@ljedrz): replace the NodeId variant
81+
pub fn hir_path_str(self, id: hir::HirId) -> String {
82+
self.item_path_str(self.hir().local_def_id_from_hir_id(id))
83+
}
84+
7985
/// Returns a string identifying this def-id. This string is
8086
/// suitable for user output. It always begins with a crate identifier.
8187
pub fn absolute_item_path_str(self, def_id: DefId) -> String {

src/librustc_mir/hair/cx/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
4646
-> Vec<StmtRef<'tcx>> {
4747
let mut result = vec![];
4848
for (index, stmt) in stmts.iter().enumerate() {
49-
let hir_id = cx.tcx.hir().node_to_hir_id(stmt.id);
49+
let hir_id = stmt.hir_id;
5050
let opt_dxn_ext = cx.region_scope_tree.opt_destruction_scope(hir_id.local_id);
5151
let stmt_span = StatementSpan(cx.tcx.hir().span(stmt.id));
5252
match stmt.node {

src/librustc_typeck/astconv.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
114114
tcx.hir().name(tcx.hir().as_local_node_id(def_id).unwrap()).as_interned_str()
115115
};
116116

117-
let hir_id = tcx.hir().node_to_hir_id(lifetime.id);
118-
let r = match tcx.named_region(hir_id) {
117+
let r = match tcx.named_region(lifetime.hir_id) {
119118
Some(rl::Region::Static) => {
120119
tcx.types.re_static
121120
}
@@ -1145,8 +1144,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
11451144
self.ast_region_to_region(lifetime, None)
11461145
} else {
11471146
self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| {
1148-
let hir_id = tcx.hir().node_to_hir_id(lifetime.id);
1149-
if tcx.named_region(hir_id).is_some() {
1147+
if tcx.named_region(lifetime.hir_id).is_some() {
11501148
self.ast_region_to_region(lifetime, None)
11511149
} else {
11521150
self.re_infer(span, None).unwrap_or_else(|| {

src/librustc_typeck/collect.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,7 @@ fn has_late_bound_regions<'a, 'tcx>(
814814
return;
815815
}
816816

817-
let hir_id = self.tcx.hir().node_to_hir_id(lt.id);
818-
match self.tcx.named_region(hir_id) {
817+
match self.tcx.named_region(lt.hir_id) {
819818
Some(rl::Region::Static) | Some(rl::Region::EarlyBound(..)) => {}
820819
Some(rl::Region::LateBound(debruijn, _, _))
821820
| Some(rl::Region::LateBoundAnon(debruijn, _)) if debruijn < self.outer_index => {}
@@ -841,8 +840,7 @@ fn has_late_bound_regions<'a, 'tcx>(
841840
};
842841
for param in &generics.params {
843842
if let GenericParamKind::Lifetime { .. } = param.kind {
844-
let hir_id = tcx.hir().node_to_hir_id(param.id);
845-
if tcx.is_late_bound(hir_id) {
843+
if tcx.is_late_bound(param.hir_id) {
846844
return Some(param.span);
847845
}
848846
}

src/librustdoc/clean/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1210,8 +1210,7 @@ impl Lifetime {
12101210
impl Clean<Lifetime> for hir::Lifetime {
12111211
fn clean(&self, cx: &DocContext) -> Lifetime {
12121212
if self.id != ast::DUMMY_NODE_ID {
1213-
let hir_id = cx.tcx.hir().node_to_hir_id(self.id);
1214-
let def = cx.tcx.named_region(hir_id);
1213+
let def = cx.tcx.named_region(self.hir_id);
12151214
match def {
12161215
Some(rl::Region::EarlyBound(_, node_id, _)) |
12171216
Some(rl::Region::LateBound(_, node_id, _)) |

0 commit comments

Comments
 (0)