Skip to content

Commit 1b7488d

Browse files
committed
scrap find_node_for_hir_id in favor of hir_to_node_id
Michael Woerister pointed out that `hir_to_node_id` (introduced in August 2017's 28ddd7a) supersedes the functionality of `find_node_for_hir_id` (just a hash lookup compared to that linear search).
1 parent 078486b commit 1b7488d

File tree

7 files changed

+8
-20
lines changed

7 files changed

+8
-20
lines changed

src/librustc/hir/map/definitions.rs

-8
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,6 @@ impl Definitions {
487487
self.node_to_hir_id[node_id]
488488
}
489489

490-
pub fn find_node_for_hir_id(&self, hir_id: hir::HirId) -> ast::NodeId {
491-
self.node_to_hir_id
492-
.iter()
493-
.position(|x| *x == hir_id)
494-
.map(|idx| ast::NodeId::new(idx))
495-
.unwrap()
496-
}
497-
498490
#[inline]
499491
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
500492
let space_index = def_index.address_space().index();

src/librustc/middle/expr_use_visitor.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
609609
match local.init {
610610
None => {
611611
local.pat.each_binding(|_, hir_id, span, _| {
612-
// FIXME: converting HirId → NodeId is said to be relatively expensive
613-
let node_id = self.mc.tcx.hir.definitions().find_node_for_hir_id(hir_id);
612+
let node_id = self.mc.tcx.hir.hir_to_node_id(hir_id);
614613
self.delegate.decl_without_init(node_id, span);
615614
})
616615
}

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
488488
// FIXME
489489
None if self.is_tainted_by_errors() => Err(()),
490490
None => {
491-
let id = self.tcx.hir.definitions().find_node_for_hir_id(id);
491+
let id = self.tcx.hir.hir_to_node_id(id);
492492
bug!("no type for node {}: {} in mem_categorization",
493493
id, self.tcx.hir.node_to_string(id));
494494
}

src/librustc/ty/context.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,7 @@ fn validate_hir_id_for_typeck_tables(local_id_root: Option<DefId>,
266266
if let Some(local_id_root) = local_id_root {
267267
if hir_id.owner != local_id_root.index {
268268
ty::tls::with(|tcx| {
269-
let node_id = tcx.hir
270-
.definitions()
271-
.find_node_for_hir_id(hir_id);
269+
let node_id = tcx.hir.hir_to_node_id(hir_id);
272270

273271
bug!("node {} with HirId::owner {:?} cannot be placed in \
274272
TypeckTables with local_id_root {:?}",
@@ -527,7 +525,7 @@ impl<'tcx> TypeckTables<'tcx> {
527525
None => {
528526
bug!("node_id_to_type: no type for node `{}`",
529527
tls::with(|tcx| {
530-
let id = tcx.hir.definitions().find_node_for_hir_id(id);
528+
let id = tcx.hir.hir_to_node_id(id);
531529
tcx.hir.node_to_string(id)
532530
}))
533531
}
@@ -2616,8 +2614,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
26162614
msg: &str)
26172615
-> DiagnosticBuilder<'tcx>
26182616
{
2619-
// FIXME: converting HirId → NodeId is said to be relatively expensive
2620-
let node_id = self.hir.definitions().find_node_for_hir_id(hir_id);
2617+
let node_id = self.hir.hir_to_node_id(hir_id);
26212618
let (level, src) = self.lint_level_at_node(lint, node_id);
26222619
lint::struct_lint_level(self.sess, lint, level, src, Some(span.into()), msg)
26232620
}

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2110,7 +2110,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
21102110
Some(&t) => t,
21112111
None if self.is_tainted_by_errors() => self.tcx.types.err,
21122112
None => {
2113-
let node_id = self.tcx.hir.definitions().find_node_for_hir_id(id);
2113+
let node_id = self.tcx.hir.hir_to_node_id(id);
21142114
bug!("no type for node {}: {} in fcx {}",
21152115
node_id, self.tcx.hir.node_to_string(node_id),
21162116
self.tag());

src/librustc_typeck/check/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl Locatable for DefIndex {
560560

561561
impl Locatable for hir::HirId {
562562
fn to_span(&self, tcx: &TyCtxt) -> Span {
563-
let node_id = tcx.hir.definitions().find_node_for_hir_id(*self);
563+
let node_id = tcx.hir.hir_to_node_id(*self);
564564
tcx.hir.span(node_id)
565565
}
566566
}

src/librustc_typeck/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
106106
}
107107
assert_eq!(def_id.krate, LOCAL_CRATE);
108108
let hir_id = tcx.hir.definitions().def_index_to_hir_id(def_id.index);
109-
let id = tcx.hir.definitions().find_node_for_hir_id(hir_id);
109+
let id = tcx.hir.hir_to_node_id(hir_id);
110110
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
111111
let msg = "unused extern crate";
112112
tcx.lint_node(lint, id, span, msg);

0 commit comments

Comments
 (0)