Skip to content

Commit abc7423

Browse files
authored
Rollup merge of rust-lang#62091 - ljedrz:hiridification_almost_there, r=Zoxc
HirIdification: almost there I'm beginning to run out of stuff to HirIdify 😉. This time I targeted mainly `hir::map::{find, get_parent_node}`, but a few other bits got changed too. r? @Zoxc
2 parents 6070e2e + 87438a1 commit abc7423

File tree

35 files changed

+119
-128
lines changed

35 files changed

+119
-128
lines changed

src/librustc/hir/map/blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a> Code<'a> {
8787
match map.get(id) {
8888
map::Node::Block(_) => {
8989
// Use the parent, hopefully an expression node.
90-
Code::from_node(map, map.get_parent_node_by_hir_id(id))
90+
Code::from_node(map, map.get_parent_node(id))
9191
}
9292
map::Node::Expr(expr) => Some(Code::Expr(expr)),
9393
node => FnLikeNode::from_node(node).map(Code::FnLike)

src/librustc/hir/map/mod.rs

+25-38
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<'hir> Map<'hir> {
292292
}
293293

294294
fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
295-
let node = if let Some(node) = self.find_by_hir_id(hir_id) {
295+
let node = if let Some(node) = self.find(hir_id) {
296296
node
297297
} else {
298298
return None
@@ -347,7 +347,7 @@ impl<'hir> Map<'hir> {
347347
if variant_data.ctor_hir_id().is_none() {
348348
return None;
349349
}
350-
let ctor_of = match self.find_by_hir_id(self.get_parent_node_by_hir_id(hir_id)) {
350+
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
351351
Some(Node::Item(..)) => def::CtorOf::Struct,
352352
Some(Node::Variant(..)) => def::CtorOf::Variant,
353353
_ => unreachable!(),
@@ -424,7 +424,7 @@ impl<'hir> Map<'hir> {
424424
/// which this is the body of, i.e., a `fn`, `const` or `static`
425425
/// item (possibly associated), a closure, or a `hir::AnonConst`.
426426
pub fn body_owner(&self, BodyId { hir_id }: BodyId) -> HirId {
427-
let parent = self.get_parent_node_by_hir_id(hir_id);
427+
let parent = self.get_parent_node(hir_id);
428428
assert!(self.lookup(parent).map_or(false, |e| e.is_body_owner(hir_id)));
429429
parent
430430
}
@@ -485,7 +485,7 @@ impl<'hir> Map<'hir> {
485485
match self.get(id) {
486486
Node::Item(&Item { node: ItemKind::Trait(..), .. }) |
487487
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => id,
488-
Node::GenericParam(_) => self.get_parent_node_by_hir_id(id),
488+
Node::GenericParam(_) => self.get_parent_node(id),
489489
_ => bug!("ty_param_owner: {} not a type parameter", self.node_to_string(id))
490490
}
491491
}
@@ -563,7 +563,7 @@ impl<'hir> Map<'hir> {
563563
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
564564
pub fn get(&self, id: HirId) -> Node<'hir> {
565565
// read recorded by `find`
566-
self.find_by_hir_id(id).unwrap_or_else(||
566+
self.find(id).unwrap_or_else(||
567567
bug!("couldn't find hir id {} in the HIR map", id))
568568
}
569569

@@ -595,13 +595,7 @@ impl<'hir> Map<'hir> {
595595
}
596596

597597
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
598-
pub fn find(&self, id: NodeId) -> Option<Node<'hir>> {
599-
let hir_id = self.node_to_hir_id(id);
600-
self.find_by_hir_id(hir_id)
601-
}
602-
603-
// FIXME(@ljedrz): replace the `NodeId` variant.
604-
pub fn find_by_hir_id(&self, hir_id: HirId) -> Option<Node<'hir>> {
598+
pub fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
605599
let result = self.find_entry(hir_id).and_then(|entry| {
606600
if let Node::Crate = entry.node {
607601
None
@@ -615,24 +609,17 @@ impl<'hir> Map<'hir> {
615609
result
616610
}
617611

618-
/// Similar to `get_parent`; returns the parent node-ID, or just `hir_id` if there
619-
/// is no parent. Note that the parent may be `CRATE_NODE_ID`, which is not itself
612+
/// Similar to `get_parent`; returns the parent HIR Id, or just `hir_id` if there
613+
/// is no parent. Note that the parent may be `CRATE_HIR_ID`, which is not itself
620614
/// present in the map, so passing the return value of `get_parent_node` to
621615
/// `get` may in fact panic.
622-
/// This function returns the immediate parent in the AST, whereas `get_parent`
616+
/// This function returns the immediate parent in the HIR, whereas `get_parent`
623617
/// returns the enclosing item. Note that this might not be the actual parent
624-
/// node in the AST -- some kinds of nodes are not in the map and these will
618+
/// node in the HIR -- some kinds of nodes are not in the map and these will
625619
/// never appear as the parent node. Thus, you can always walk the parent nodes
626-
/// from a node to the root of the AST (unless you get back the same ID here,
620+
/// from a node to the root of the HIR (unless you get back the same ID here,
627621
/// which can happen if the ID is not in the map itself or is just weird).
628-
pub fn get_parent_node(&self, id: NodeId) -> NodeId {
629-
let hir_id = self.node_to_hir_id(id);
630-
let parent_hir_id = self.get_parent_node_by_hir_id(hir_id);
631-
self.hir_to_node_id(parent_hir_id)
632-
}
633-
634-
// FIXME(@ljedrz): replace the `NodeId` variant.
635-
pub fn get_parent_node_by_hir_id(&self, hir_id: HirId) -> HirId {
622+
pub fn get_parent_node(&self, hir_id: HirId) -> HirId {
636623
if self.dep_graph.is_fully_enabled() {
637624
let hir_id_owner = hir_id.owner;
638625
let def_path_hash = self.definitions.def_path_hash(hir_id_owner);
@@ -646,7 +633,7 @@ impl<'hir> Map<'hir> {
646633

647634
/// Check if the node is an argument. An argument is a local variable whose
648635
/// immediate parent is an item or a closure.
649-
pub fn is_argument(&self, id: NodeId) -> bool {
636+
pub fn is_argument(&self, id: HirId) -> bool {
650637
match self.find(id) {
651638
Some(Node::Binding(_)) => (),
652639
_ => return false,
@@ -687,7 +674,7 @@ impl<'hir> Map<'hir> {
687674
{
688675
let mut id = start_id;
689676
loop {
690-
let parent_id = self.get_parent_node_by_hir_id(id);
677+
let parent_id = self.get_parent_node(id);
691678
if parent_id == CRATE_HIR_ID {
692679
return Ok(CRATE_HIR_ID);
693680
}
@@ -872,28 +859,28 @@ impl<'hir> Map<'hir> {
872859
}
873860

874861
pub fn expect_item(&self, id: HirId) -> &'hir Item {
875-
match self.find_by_hir_id(id) { // read recorded by `find`
862+
match self.find(id) { // read recorded by `find`
876863
Some(Node::Item(item)) => item,
877864
_ => bug!("expected item, found {}", self.node_to_string(id))
878865
}
879866
}
880867

881868
pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem {
882-
match self.find_by_hir_id(id) {
869+
match self.find(id) {
883870
Some(Node::ImplItem(item)) => item,
884871
_ => bug!("expected impl item, found {}", self.node_to_string(id))
885872
}
886873
}
887874

888875
pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem {
889-
match self.find_by_hir_id(id) {
876+
match self.find(id) {
890877
Some(Node::TraitItem(item)) => item,
891878
_ => bug!("expected trait item, found {}", self.node_to_string(id))
892879
}
893880
}
894881

895882
pub fn expect_variant_data(&self, id: HirId) -> &'hir VariantData {
896-
match self.find_by_hir_id(id) {
883+
match self.find(id) {
897884
Some(Node::Item(i)) => {
898885
match i.node {
899886
ItemKind::Struct(ref struct_def, _) |
@@ -908,21 +895,21 @@ impl<'hir> Map<'hir> {
908895
}
909896

910897
pub fn expect_variant(&self, id: HirId) -> &'hir Variant {
911-
match self.find_by_hir_id(id) {
898+
match self.find(id) {
912899
Some(Node::Variant(variant)) => variant,
913900
_ => bug!("expected variant, found {}", self.node_to_string(id)),
914901
}
915902
}
916903

917904
pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem {
918-
match self.find_by_hir_id(id) {
905+
match self.find(id) {
919906
Some(Node::ForeignItem(item)) => item,
920907
_ => bug!("expected foreign item, found {}", self.node_to_string(id))
921908
}
922909
}
923910

924911
pub fn expect_expr(&self, id: HirId) -> &'hir Expr {
925-
match self.find_by_hir_id(id) { // read recorded by find
912+
match self.find(id) { // read recorded by find
926913
Some(Node::Expr(expr)) => expr,
927914
_ => bug!("expected expr, found {}", self.node_to_string(id))
928915
}
@@ -1028,8 +1015,8 @@ impl<'hir> Map<'hir> {
10281015
Some(Node::Pat(pat)) => pat.span,
10291016
Some(Node::Arm(arm)) => arm.span,
10301017
Some(Node::Block(block)) => block.span,
1031-
Some(Node::Ctor(..)) => match self.find_by_hir_id(
1032-
self.get_parent_node_by_hir_id(hir_id))
1018+
Some(Node::Ctor(..)) => match self.find(
1019+
self.get_parent_node(hir_id))
10331020
{
10341021
Some(Node::Item(item)) => item.span,
10351022
Some(Node::Variant(variant)) => variant.span,
@@ -1100,7 +1087,7 @@ impl<'a> NodesMatchingSuffix<'a> {
11001087
// chain, then returns `None`.
11011088
fn find_first_mod_parent<'a>(map: &'a Map<'_>, mut id: HirId) -> Option<(HirId, Name)> {
11021089
loop {
1103-
if let Node::Item(item) = map.find_by_hir_id(id)? {
1090+
if let Node::Item(item) = map.find(id)? {
11041091
if item_is_mod(&item) {
11051092
return Some((id, item.ident.name))
11061093
}
@@ -1273,7 +1260,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
12731260
})
12741261
};
12751262

1276-
match map.find_by_hir_id(id) {
1263+
match map.find(id) {
12771264
Some(Node::Item(item)) => {
12781265
let item_str = match item.node {
12791266
ItemKind::ExternCrate(..) => "extern crate",

src/librustc/infer/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'tcx> TyCtxt<'tcx> {
8686
)
8787
};
8888
let span = scope.span(self, region_scope_tree);
89-
let tag = match self.hir().find_by_hir_id(scope.hir_id(region_scope_tree)) {
89+
let tag = match self.hir().find(scope.hir_id(region_scope_tree)) {
9090
Some(Node::Block(_)) => "block",
9191
Some(Node::Expr(expr)) => match expr.node {
9292
hir::ExprKind::Call(..) => "call",
@@ -182,7 +182,7 @@ impl<'tcx> TyCtxt<'tcx> {
182182

183183
let scope = region.free_region_binding_scope(self);
184184
let node = self.hir().as_local_hir_id(scope).unwrap_or(hir::DUMMY_HIR_ID);
185-
let tag = match self.hir().find_by_hir_id(node) {
185+
let tag = match self.hir().find(node) {
186186
Some(Node::Block(_)) | Some(Node::Expr(_)) => "body",
187187
Some(Node::Item(it)) => Self::item_scope_tag(&it),
188188
Some(Node::TraitItem(it)) => Self::trait_item_scope_tag(&it),

src/librustc/infer/opaque_types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
777777
.local_def_id_from_hir_id(opaque_parent_hir_id)
778778
};
779779
let (in_definition_scope, origin) =
780-
match tcx.hir().find_by_hir_id(opaque_hir_id)
780+
match tcx.hir().find(opaque_hir_id)
781781
{
782782
Some(Node::Item(item)) => match item.node {
783783
// Anonymous `impl Trait`

src/librustc/middle/dead.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use syntax_pos;
2727
// function, then we should explore its block to check for codes that
2828
// may need to be marked as live.
2929
fn should_explore<'tcx>(tcx: TyCtxt<'tcx>, hir_id: hir::HirId) -> bool {
30-
match tcx.hir().find_by_hir_id(hir_id) {
30+
match tcx.hir().find(hir_id) {
3131
Some(Node::Item(..)) |
3232
Some(Node::ImplItem(..)) |
3333
Some(Node::ForeignItem(..)) |
@@ -145,7 +145,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
145145
// tuple struct constructor function
146146
let id = self.struct_constructors.get(&id).cloned().unwrap_or(id);
147147

148-
if let Some(node) = self.tcx.hir().find_by_hir_id(id) {
148+
if let Some(node) = self.tcx.hir().find(id) {
149149
self.live_symbols.insert(id);
150150
self.visit_node(node);
151151
}

src/librustc/middle/liveness.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ use std::{fmt, u32};
110110
use std::io::prelude::*;
111111
use std::io;
112112
use std::rc::Rc;
113-
use syntax::ast::{self, NodeId};
113+
use syntax::ast;
114114
use syntax::ptr::P;
115115
use syntax::symbol::{kw, sym};
116116
use syntax_pos::Span;
@@ -369,7 +369,7 @@ fn visit_fn<'tcx>(
369369
// Don't run unused pass for #[derive()]
370370
if let FnKind::Method(..) = fk {
371371
let parent = ir.tcx.hir().get_parent_item(id);
372-
if let Some(Node::Item(i)) = ir.tcx.hir().find_by_hir_id(parent) {
372+
if let Some(Node::Item(i)) = ir.tcx.hir().find(parent) {
373373
if i.attrs.iter().any(|a| a.check_name(sym::automatically_derived)) {
374374
return;
375375
}
@@ -1327,12 +1327,11 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
13271327
}
13281328
}
13291329

1330-
fn access_var(&mut self, hir_id: HirId, nid: NodeId, succ: LiveNode, acc: u32, span: Span)
1330+
fn access_var(&mut self, hir_id: HirId, var_hid: HirId, succ: LiveNode, acc: u32, span: Span)
13311331
-> LiveNode {
13321332
let ln = self.live_node(hir_id, span);
13331333
if acc != 0 {
13341334
self.init_from_succ(ln, succ);
1335-
let var_hid = self.ir.tcx.hir().node_to_hir_id(nid);
13361335
let var = self.variable(var_hid, span);
13371336
self.acc(ln, var, acc);
13381337
}
@@ -1345,8 +1344,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
13451344
Res::Local(hid) => {
13461345
let upvars = self.ir.tcx.upvars(self.ir.body_owner);
13471346
if !upvars.map_or(false, |upvars| upvars.contains_key(&hid)) {
1348-
let nid = self.ir.tcx.hir().hir_to_node_id(hid);
1349-
self.access_var(hir_id, nid, succ, acc, path.span)
1347+
self.access_var(hir_id, hid, succ, acc, path.span)
13501348
} else {
13511349
succ
13521350
}

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ impl<'tcx> cmt_<'tcx> {
15261526
"non-place".into()
15271527
}
15281528
Categorization::Local(vid) => {
1529-
if tcx.hir().is_argument(tcx.hir().hir_to_node_id(vid)) {
1529+
if tcx.hir().is_argument(vid) {
15301530
"argument"
15311531
} else {
15321532
"local variable"

src/librustc/middle/reachable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn method_might_be_inlined<'tcx>(
5353
return true
5454
}
5555
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) {
56-
match tcx.hir().find_by_hir_id(impl_hir_id) {
56+
match tcx.hir().find(impl_hir_id) {
5757
Some(Node::Item(item)) =>
5858
item_might_be_inlined(tcx, &item, codegen_fn_attrs),
5959
Some(..) | None =>
@@ -147,7 +147,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
147147
None => { return false; }
148148
};
149149

150-
match self.tcx.hir().find_by_hir_id(hir_id) {
150+
match self.tcx.hir().find(hir_id) {
151151
Some(Node::Item(item)) => {
152152
match item.node {
153153
hir::ItemKind::Fn(..) =>
@@ -205,7 +205,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
205205
continue
206206
}
207207

208-
if let Some(ref item) = self.tcx.hir().find_by_hir_id(search_item) {
208+
if let Some(ref item) = self.tcx.hir().find(search_item) {
209209
self.propagate_node(item, search_item);
210210
}
211211
}

src/librustc/middle/resolve_lifetime.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
662662
if let Some(Region::LateBound(_, def_id, _)) = def {
663663
if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) {
664664
// Ensure that the parent of the def is an item, not HRTB
665-
let parent_id = self.tcx.hir().get_parent_node_by_hir_id(hir_id);
665+
let parent_id = self.tcx.hir().get_parent_node(hir_id);
666666
let parent_impl_id = hir::ImplItemId { hir_id: parent_id };
667667
let parent_trait_id = hir::TraitItemId { hir_id: parent_id };
668668
let krate = self.tcx.hir().forest.krate();
@@ -1489,7 +1489,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14891489
}
14901490
};
14911491
if let Node::Lifetime(hir_lifetime) = self.tcx.hir().get(lifetime.hir_id) {
1492-
if let Some(parent) = self.tcx.hir().find_by_hir_id(
1492+
if let Some(parent) = self.tcx.hir().find(
14931493
self.tcx.hir().get_parent_item(hir_lifetime.hir_id))
14941494
{
14951495
match parent {
@@ -2051,7 +2051,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
20512051
// and whether there's a `self` argument (treated specially).
20522052
let mut assoc_item_kind = None;
20532053
let mut impl_self = None;
2054-
let parent = self.tcx.hir().get_parent_node_by_hir_id(output.hir_id);
2054+
let parent = self.tcx.hir().get_parent_node(output.hir_id);
20552055
let body = match self.tcx.hir().get(parent) {
20562056
// `fn` definitions and methods.
20572057
Node::Item(&hir::Item {

src/librustc/traits/error_reporting.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
938938
err: &mut DiagnosticBuilder<'tcx>,
939939
) {
940940
if let &ObligationCauseCode::VariableType(hir_id) = code {
941-
let parent_node = self.tcx.hir().get_parent_node_by_hir_id(hir_id);
942-
if let Some(Node::Local(ref local)) = self.tcx.hir().find_by_hir_id(parent_node) {
941+
let parent_node = self.tcx.hir().get_parent_node(hir_id);
942+
if let Some(Node::Local(ref local)) = self.tcx.hir().find(parent_node) {
943943
if let Some(ref expr) = local.init {
944944
if let hir::ExprKind::Index(_, _) = expr.node {
945945
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
@@ -1013,8 +1013,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10131013
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
10141014
) {
10151015
let hir = self.tcx.hir();
1016-
let parent_node = hir.get_parent_node_by_hir_id(obligation.cause.body_id);
1017-
let node = hir.find_by_hir_id(parent_node);
1016+
let parent_node = hir.get_parent_node(obligation.cause.body_id);
1017+
let node = hir.find(parent_node);
10181018
if let Some(hir::Node::Item(hir::Item {
10191019
node: hir::ItemKind::Fn(decl, _, _, body_id),
10201020
..

0 commit comments

Comments
 (0)