Skip to content

Commit c3b8ab5

Browse files
committed
Auto merge of #60337 - fabric-and-ink:hiridification, r=Zoxc
A bit of HirIdification A small contribution to #50928.
2 parents d98a165 + 164988c commit c3b8ab5

File tree

3 files changed

+39
-60
lines changed

3 files changed

+39
-60
lines changed

src/librustc/hir/map/mod.rs

+33-57
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,6 @@ pub struct Map<'hir> {
174174
/// The SVH of the local crate.
175175
pub crate_hash: Svh,
176176

177-
/// `NodeId`s are sequential integers from 0, so we can be
178-
/// super-compact by storing them in a vector. Not everything with
179-
/// a `NodeId` is in the map, but empirically the occupancy is about
180-
/// 75-80%, so there's not too much overhead (certainly less than
181-
/// a hashmap, since they (at the time of writing) have a maximum
182-
/// of 75% occupancy).
183-
///
184-
/// Also, indexing is pretty quick when you've got a vector and
185-
/// plain old integers.
186177
map: FxHashMap<HirId, Entry<'hir>>,
187178

188179
definitions: &'hir Definitions,
@@ -199,13 +190,7 @@ impl<'hir> Map<'hir> {
199190
/// otherwise have had access to those contents, and hence needs a
200191
/// read recorded). If the function just returns a DefId or
201192
/// NodeId, no actual content was returned, so no read is needed.
202-
pub fn read(&self, id: NodeId) {
203-
let hir_id = self.node_to_hir_id(id);
204-
self.read_by_hir_id(hir_id);
205-
}
206-
207-
// FIXME(@ljedrz): replace the NodeId variant
208-
pub fn read_by_hir_id(&self, hir_id: HirId) {
193+
pub fn read(&self, hir_id: HirId) {
209194
if let Some(entry) = self.map.get(&hir_id) {
210195
self.dep_graph.read_index(entry.dep_node);
211196
} else {
@@ -223,17 +208,12 @@ impl<'hir> Map<'hir> {
223208
self.definitions.def_key(def_id.index)
224209
}
225210

226-
pub fn def_path_from_id(&self, id: NodeId) -> Option<DefPath> {
227-
self.opt_local_def_id(id).map(|def_id| {
211+
pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
212+
self.opt_local_def_id_from_hir_id(id).map(|def_id| {
228213
self.def_path(def_id)
229214
})
230215
}
231216

232-
// FIXME(@ljedrz): replace the NodeId variant
233-
pub fn def_path_from_hir_id(&self, id: HirId) -> DefPath {
234-
self.def_path(self.local_def_id_from_hir_id(id))
235-
}
236-
237217
pub fn def_path(&self, def_id: DefId) -> DefPath {
238218
assert!(def_id.is_local());
239219
self.definitions.def_path(def_id.index)
@@ -411,23 +391,23 @@ impl<'hir> Map<'hir> {
411391
}
412392

413393
pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem {
414-
self.read_by_hir_id(id.hir_id);
394+
self.read(id.hir_id);
415395

416396
// N.B., intentionally bypass `self.forest.krate()` so that we
417397
// do not trigger a read of the whole krate here
418398
self.forest.krate.trait_item(id)
419399
}
420400

421401
pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem {
422-
self.read_by_hir_id(id.hir_id);
402+
self.read(id.hir_id);
423403

424404
// N.B., intentionally bypass `self.forest.krate()` so that we
425405
// do not trigger a read of the whole krate here
426406
self.forest.krate.impl_item(id)
427407
}
428408

429409
pub fn body(&self, id: BodyId) -> &'hir Body {
430-
self.read_by_hir_id(id.hir_id);
410+
self.read(id.hir_id);
431411

432412
// N.B., intentionally bypass `self.forest.krate()` so that we
433413
// do not trigger a read of the whole krate here
@@ -560,7 +540,7 @@ impl<'hir> Map<'hir> {
560540
pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, HirId)
561541
{
562542
let hir_id = self.as_local_hir_id(module).unwrap();
563-
self.read_by_hir_id(hir_id);
543+
self.read(hir_id);
564544
match self.find_entry(hir_id).unwrap().node {
565545
Node::Item(&Item {
566546
span,
@@ -575,13 +555,15 @@ impl<'hir> Map<'hir> {
575555
pub fn visit_item_likes_in_module<V>(&self, module: DefId, visitor: &mut V)
576556
where V: ItemLikeVisitor<'hir>
577557
{
578-
let node_id = self.as_local_node_id(module).unwrap();
558+
let hir_id = self.as_local_hir_id(module).unwrap();
579559

580560
// Read the module so we'll be re-executed if new items
581561
// appear immediately under in the module. If some new item appears
582562
// in some nested item in the module, we'll be re-executed due to reads
583563
// in the expect_* calls the loops below
584-
self.read(node_id);
564+
self.read(hir_id);
565+
566+
let node_id = self.hir_to_node_id[&hir_id];
585567

586568
let module = &self.forest.krate.modules[&node_id];
587569

@@ -659,7 +641,7 @@ impl<'hir> Map<'hir> {
659641
}
660642
});
661643
if result.is_some() {
662-
self.read_by_hir_id(hir_id);
644+
self.read(hir_id);
663645
}
664646
result
665647
}
@@ -893,7 +875,7 @@ impl<'hir> Map<'hir> {
893875
if let Entry {
894876
node: Node::Item(Item { node: ItemKind::ForeignMod(ref nm), .. }), .. } = entry
895877
{
896-
self.read_by_hir_id(hir_id); // reveals some of the content of a node
878+
self.read(hir_id); // reveals some of the content of a node
897879
return nm.abi;
898880
}
899881
}
@@ -1001,7 +983,7 @@ impl<'hir> Map<'hir> {
1001983

1002984
// FIXME(@ljedrz): replace the NodeId variant
1003985
pub fn attrs_by_hir_id(&self, id: HirId) -> &'hir [ast::Attribute] {
1004-
self.read_by_hir_id(id); // reveals attributes on the node
986+
self.read(id); // reveals attributes on the node
1005987
let attrs = match self.find_entry(id).map(|entry| entry.node) {
1006988
Some(Node::Local(l)) => Some(&l.attrs[..]),
1007989
Some(Node::Item(i)) => Some(&i.attrs[..]),
@@ -1046,7 +1028,7 @@ impl<'hir> Map<'hir> {
10461028

10471029
// FIXME(@ljedrz): replace the NodeId variant
10481030
pub fn span_by_hir_id(&self, hir_id: HirId) -> Span {
1049-
self.read_by_hir_id(hir_id); // reveals span from node
1031+
self.read(hir_id); // reveals span from node
10501032
match self.find_entry(hir_id).map(|entry| entry.node) {
10511033
Some(Node::Item(item)) => item.span,
10521034
Some(Node::ForeignItem(foreign_item)) => foreign_item.span,
@@ -1088,7 +1070,7 @@ impl<'hir> Map<'hir> {
10881070
}
10891071

10901072
pub fn node_to_string(&self, id: NodeId) -> String {
1091-
node_id_to_string(self, id, true)
1073+
hir_id_to_string(self, self.node_to_hir_id(id), true)
10921074
}
10931075

10941076
// FIXME(@ljedrz): replace the NodeId variant
@@ -1097,7 +1079,7 @@ impl<'hir> Map<'hir> {
10971079
}
10981080

10991081
pub fn node_to_user_string(&self, id: NodeId) -> String {
1100-
node_id_to_string(self, id, false)
1082+
hir_id_to_string(self, self.node_to_hir_id(id), false)
11011083
}
11021084

11031085
// FIXME(@ljedrz): replace the NodeId variant
@@ -1316,18 +1298,18 @@ impl<'a> print::State<'a> {
13161298
}
13171299
}
13181300

1319-
fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
1320-
let id_str = format!(" (id={})", id);
1301+
fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1302+
let id_str = format!(" (hir_id={})", id);
13211303
let id_str = if include_id { &id_str[..] } else { "" };
13221304

13231305
let path_str = || {
13241306
// This functionality is used for debugging, try to use TyCtxt to get
13251307
// the user-friendly path, otherwise fall back to stringifying DefPath.
13261308
crate::ty::tls::with_opt(|tcx| {
13271309
if let Some(tcx) = tcx {
1328-
let def_id = map.local_def_id(id);
1310+
let def_id = map.local_def_id_from_hir_id(id);
13291311
tcx.def_path_str(def_id)
1330-
} else if let Some(path) = map.def_path_from_id(id) {
1312+
} else if let Some(path) = map.def_path_from_hir_id(id) {
13311313
path.data.into_iter().map(|elem| {
13321314
elem.data.to_string()
13331315
}).collect::<Vec<_>>().join("::")
@@ -1337,7 +1319,7 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
13371319
})
13381320
};
13391321

1340-
match map.find(id) {
1322+
match map.find_by_hir_id(id) {
13411323
Some(Node::Item(item)) => {
13421324
let item_str = match item.node {
13431325
ItemKind::ExternCrate(..) => "extern crate",
@@ -1398,40 +1380,40 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
13981380
path_str(), id_str)
13991381
}
14001382
Some(Node::AnonConst(_)) => {
1401-
format!("const {}{}", map.node_to_pretty_string(id), id_str)
1383+
format!("const {}{}", map.hir_to_pretty_string(id), id_str)
14021384
}
14031385
Some(Node::Expr(_)) => {
1404-
format!("expr {}{}", map.node_to_pretty_string(id), id_str)
1386+
format!("expr {}{}", map.hir_to_pretty_string(id), id_str)
14051387
}
14061388
Some(Node::Stmt(_)) => {
1407-
format!("stmt {}{}", map.node_to_pretty_string(id), id_str)
1389+
format!("stmt {}{}", map.hir_to_pretty_string(id), id_str)
14081390
}
14091391
Some(Node::PathSegment(_)) => {
1410-
format!("path segment {}{}", map.node_to_pretty_string(id), id_str)
1392+
format!("path segment {}{}", map.hir_to_pretty_string(id), id_str)
14111393
}
14121394
Some(Node::Ty(_)) => {
1413-
format!("type {}{}", map.node_to_pretty_string(id), id_str)
1395+
format!("type {}{}", map.hir_to_pretty_string(id), id_str)
14141396
}
14151397
Some(Node::TraitRef(_)) => {
1416-
format!("trait_ref {}{}", map.node_to_pretty_string(id), id_str)
1398+
format!("trait_ref {}{}", map.hir_to_pretty_string(id), id_str)
14171399
}
14181400
Some(Node::Binding(_)) => {
1419-
format!("local {}{}", map.node_to_pretty_string(id), id_str)
1401+
format!("local {}{}", map.hir_to_pretty_string(id), id_str)
14201402
}
14211403
Some(Node::Pat(_)) => {
1422-
format!("pat {}{}", map.node_to_pretty_string(id), id_str)
1404+
format!("pat {}{}", map.hir_to_pretty_string(id), id_str)
14231405
}
14241406
Some(Node::Block(_)) => {
1425-
format!("block {}{}", map.node_to_pretty_string(id), id_str)
1407+
format!("block {}{}", map.hir_to_pretty_string(id), id_str)
14261408
}
14271409
Some(Node::Local(_)) => {
1428-
format!("local {}{}", map.node_to_pretty_string(id), id_str)
1410+
format!("local {}{}", map.hir_to_pretty_string(id), id_str)
14291411
}
14301412
Some(Node::Ctor(..)) => {
14311413
format!("ctor {}{}", path_str(), id_str)
14321414
}
14331415
Some(Node::Lifetime(_)) => {
1434-
format!("lifetime {}{}", map.node_to_pretty_string(id), id_str)
1416+
format!("lifetime {}{}", map.hir_to_pretty_string(id), id_str)
14351417
}
14361418
Some(Node::GenericParam(ref param)) => {
14371419
format!("generic_param {:?}{}", param, id_str)
@@ -1447,12 +1429,6 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
14471429
}
14481430
}
14491431

1450-
// FIXME(@ljedrz): replace the NodeId variant
1451-
fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1452-
let node_id = map.hir_to_node_id(id);
1453-
node_id_to_string(map, node_id, include_id)
1454-
}
1455-
14561432
pub fn def_kind(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<DefKind> {
14571433
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
14581434
tcx.hir().def_kind(node_id)

src/librustc_driver/pretty.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
255255

256256
/// Computes an user-readable representation of a path, if possible.
257257
fn node_path(&self, id: ast::NodeId) -> Option<String> {
258-
self.hir_map().and_then(|map| map.def_path_from_id(id)).map(|path| {
258+
self.hir_map().and_then(|map| {
259+
let hir_id = map.node_to_hir_id(id);
260+
map.def_path_from_hir_id(hir_id)
261+
}).map(|path| {
259262
path.data
260263
.into_iter()
261264
.map(|elem| elem.data.to_string())

src/librustc_metadata/index_builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ macro_rules! read_hir {
185185
($t:ty) => {
186186
impl<'tcx> DepGraphRead for &'tcx $t {
187187
fn read(&self, tcx: TyCtxt<'_, '_, '_>) {
188-
tcx.hir().read_by_hir_id(self.hir_id);
188+
tcx.hir().read(self.hir_id);
189189
}
190190
}
191191
}
@@ -219,6 +219,6 @@ pub struct FromId<T>(pub hir::HirId, pub T);
219219

220220
impl<T> DepGraphRead for FromId<T> {
221221
fn read(&self, tcx: TyCtxt<'_, '_, '_>) {
222-
tcx.hir().read_by_hir_id(self.0);
222+
tcx.hir().read(self.0);
223223
}
224224
}

0 commit comments

Comments
 (0)