Skip to content

HirIdification: almost there #62091

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 6 commits into from
Jun 25, 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
2 changes: 1 addition & 1 deletion src/librustc/hir/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a> Code<'a> {
match map.get(id) {
map::Node::Block(_) => {
// Use the parent, hopefully an expression node.
Code::from_node(map, map.get_parent_node_by_hir_id(id))
Code::from_node(map, map.get_parent_node(id))
}
map::Node::Expr(expr) => Some(Code::Expr(expr)),
node => FnLikeNode::from_node(node).map(Code::FnLike)
Expand Down
63 changes: 25 additions & 38 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl<'hir> Map<'hir> {
}

fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
let node = if let Some(node) = self.find_by_hir_id(hir_id) {
let node = if let Some(node) = self.find(hir_id) {
node
} else {
return None
Expand Down Expand Up @@ -347,7 +347,7 @@ impl<'hir> Map<'hir> {
if variant_data.ctor_hir_id().is_none() {
return None;
}
let ctor_of = match self.find_by_hir_id(self.get_parent_node_by_hir_id(hir_id)) {
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
Some(Node::Item(..)) => def::CtorOf::Struct,
Some(Node::Variant(..)) => def::CtorOf::Variant,
_ => unreachable!(),
Expand Down Expand Up @@ -424,7 +424,7 @@ impl<'hir> Map<'hir> {
/// which this is the body of, i.e., a `fn`, `const` or `static`
/// item (possibly associated), a closure, or a `hir::AnonConst`.
pub fn body_owner(&self, BodyId { hir_id }: BodyId) -> HirId {
let parent = self.get_parent_node_by_hir_id(hir_id);
let parent = self.get_parent_node(hir_id);
assert!(self.lookup(parent).map_or(false, |e| e.is_body_owner(hir_id)));
parent
}
Expand Down Expand Up @@ -485,7 +485,7 @@ impl<'hir> Map<'hir> {
match self.get(id) {
Node::Item(&Item { node: ItemKind::Trait(..), .. }) |
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => id,
Node::GenericParam(_) => self.get_parent_node_by_hir_id(id),
Node::GenericParam(_) => self.get_parent_node(id),
_ => bug!("ty_param_owner: {} not a type parameter", self.node_to_string(id))
}
}
Expand Down Expand Up @@ -563,7 +563,7 @@ impl<'hir> Map<'hir> {
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
pub fn get(&self, id: HirId) -> Node<'hir> {
// read recorded by `find`
self.find_by_hir_id(id).unwrap_or_else(||
self.find(id).unwrap_or_else(||
bug!("couldn't find hir id {} in the HIR map", id))
}

Expand Down Expand Up @@ -595,13 +595,7 @@ impl<'hir> Map<'hir> {
}

/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
pub fn find(&self, id: NodeId) -> Option<Node<'hir>> {
let hir_id = self.node_to_hir_id(id);
self.find_by_hir_id(hir_id)
}

// FIXME(@ljedrz): replace the `NodeId` variant.
pub fn find_by_hir_id(&self, hir_id: HirId) -> Option<Node<'hir>> {
pub fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
let result = self.find_entry(hir_id).and_then(|entry| {
if let Node::Crate = entry.node {
None
Expand All @@ -615,24 +609,17 @@ impl<'hir> Map<'hir> {
result
}

/// Similar to `get_parent`; returns the parent node-ID, or just `hir_id` if there
/// is no parent. Note that the parent may be `CRATE_NODE_ID`, which is not itself
/// Similar to `get_parent`; returns the parent HIR Id, or just `hir_id` if there
/// is no parent. Note that the parent may be `CRATE_HIR_ID`, which is not itself
/// present in the map, so passing the return value of `get_parent_node` to
/// `get` may in fact panic.
/// This function returns the immediate parent in the AST, whereas `get_parent`
/// This function returns the immediate parent in the HIR, whereas `get_parent`
/// returns the enclosing item. Note that this might not be the actual parent
/// node in the AST -- some kinds of nodes are not in the map and these will
/// node in the HIR -- some kinds of nodes are not in the map and these will
/// never appear as the parent node. Thus, you can always walk the parent nodes
/// from a node to the root of the AST (unless you get back the same ID here,
/// from a node to the root of the HIR (unless you get back the same ID here,
/// which can happen if the ID is not in the map itself or is just weird).
pub fn get_parent_node(&self, id: NodeId) -> NodeId {
let hir_id = self.node_to_hir_id(id);
let parent_hir_id = self.get_parent_node_by_hir_id(hir_id);
self.hir_to_node_id(parent_hir_id)
}

// FIXME(@ljedrz): replace the `NodeId` variant.
pub fn get_parent_node_by_hir_id(&self, hir_id: HirId) -> HirId {
pub fn get_parent_node(&self, hir_id: HirId) -> HirId {
if self.dep_graph.is_fully_enabled() {
let hir_id_owner = hir_id.owner;
let def_path_hash = self.definitions.def_path_hash(hir_id_owner);
Expand All @@ -646,7 +633,7 @@ impl<'hir> Map<'hir> {

/// 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 {
pub fn is_argument(&self, id: HirId) -> bool {
match self.find(id) {
Some(Node::Binding(_)) => (),
_ => return false,
Expand Down Expand Up @@ -687,7 +674,7 @@ impl<'hir> Map<'hir> {
{
let mut id = start_id;
loop {
let parent_id = self.get_parent_node_by_hir_id(id);
let parent_id = self.get_parent_node(id);
if parent_id == CRATE_HIR_ID {
return Ok(CRATE_HIR_ID);
}
Expand Down Expand Up @@ -872,28 +859,28 @@ impl<'hir> Map<'hir> {
}

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

pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem {
match self.find_by_hir_id(id) {
match self.find(id) {
Some(Node::ImplItem(item)) => item,
_ => bug!("expected impl item, found {}", self.node_to_string(id))
}
}

pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem {
match self.find_by_hir_id(id) {
match self.find(id) {
Some(Node::TraitItem(item)) => item,
_ => bug!("expected trait item, found {}", self.node_to_string(id))
}
}

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

pub fn expect_variant(&self, id: HirId) -> &'hir Variant {
match self.find_by_hir_id(id) {
match self.find(id) {
Some(Node::Variant(variant)) => variant,
_ => bug!("expected variant, found {}", self.node_to_string(id)),
}
}

pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem {
match self.find_by_hir_id(id) {
match self.find(id) {
Some(Node::ForeignItem(item)) => item,
_ => bug!("expected foreign item, found {}", self.node_to_string(id))
}
}

pub fn expect_expr(&self, id: HirId) -> &'hir Expr {
match self.find_by_hir_id(id) { // read recorded by find
match self.find(id) { // read recorded by find
Some(Node::Expr(expr)) => expr,
_ => bug!("expected expr, found {}", self.node_to_string(id))
}
Expand Down Expand Up @@ -1028,8 +1015,8 @@ impl<'hir> Map<'hir> {
Some(Node::Pat(pat)) => pat.span,
Some(Node::Arm(arm)) => arm.span,
Some(Node::Block(block)) => block.span,
Some(Node::Ctor(..)) => match self.find_by_hir_id(
self.get_parent_node_by_hir_id(hir_id))
Some(Node::Ctor(..)) => match self.find(
self.get_parent_node(hir_id))
{
Some(Node::Item(item)) => item.span,
Some(Node::Variant(variant)) => variant.span,
Expand Down Expand Up @@ -1100,7 +1087,7 @@ impl<'a> NodesMatchingSuffix<'a> {
// chain, then returns `None`.
fn find_first_mod_parent<'a>(map: &'a Map<'_>, mut id: HirId) -> Option<(HirId, Name)> {
loop {
if let Node::Item(item) = map.find_by_hir_id(id)? {
if let Node::Item(item) = map.find(id)? {
if item_is_mod(&item) {
return Some((id, item.ident.name))
}
Expand Down Expand Up @@ -1273,7 +1260,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
})
};

match map.find_by_hir_id(id) {
match map.find(id) {
Some(Node::Item(item)) => {
let item_str = match item.node {
ItemKind::ExternCrate(..) => "extern crate",
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<'tcx> TyCtxt<'tcx> {
)
};
let span = scope.span(self, region_scope_tree);
let tag = match self.hir().find_by_hir_id(scope.hir_id(region_scope_tree)) {
let tag = match self.hir().find(scope.hir_id(region_scope_tree)) {
Some(Node::Block(_)) => "block",
Some(Node::Expr(expr)) => match expr.node {
hir::ExprKind::Call(..) => "call",
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<'tcx> TyCtxt<'tcx> {

let scope = region.free_region_binding_scope(self);
let node = self.hir().as_local_hir_id(scope).unwrap_or(hir::DUMMY_HIR_ID);
let tag = match self.hir().find_by_hir_id(node) {
let tag = match self.hir().find(node) {
Some(Node::Block(_)) | Some(Node::Expr(_)) => "body",
Some(Node::Item(it)) => Self::item_scope_tag(&it),
Some(Node::TraitItem(it)) => Self::trait_item_scope_tag(&it),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
.local_def_id_from_hir_id(opaque_parent_hir_id)
};
let (in_definition_scope, origin) =
match tcx.hir().find_by_hir_id(opaque_hir_id)
match tcx.hir().find(opaque_hir_id)
{
Some(Node::Item(item)) => match item.node {
// Anonymous `impl Trait`
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use syntax_pos;
// function, then we should explore its block to check for codes that
// may need to be marked as live.
fn should_explore<'tcx>(tcx: TyCtxt<'tcx>, hir_id: hir::HirId) -> bool {
match tcx.hir().find_by_hir_id(hir_id) {
match tcx.hir().find(hir_id) {
Some(Node::Item(..)) |
Some(Node::ImplItem(..)) |
Some(Node::ForeignItem(..)) |
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
// tuple struct constructor function
let id = self.struct_constructors.get(&id).cloned().unwrap_or(id);

if let Some(node) = self.tcx.hir().find_by_hir_id(id) {
if let Some(node) = self.tcx.hir().find(id) {
self.live_symbols.insert(id);
self.visit_node(node);
}
Expand Down
10 changes: 4 additions & 6 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ use std::{fmt, u32};
use std::io::prelude::*;
use std::io;
use std::rc::Rc;
use syntax::ast::{self, NodeId};
use syntax::ast;
use syntax::ptr::P;
use syntax::symbol::{kw, sym};
use syntax_pos::Span;
Expand Down Expand Up @@ -369,7 +369,7 @@ fn visit_fn<'tcx>(
// Don't run unused pass for #[derive()]
if let FnKind::Method(..) = fk {
let parent = ir.tcx.hir().get_parent_item(id);
if let Some(Node::Item(i)) = ir.tcx.hir().find_by_hir_id(parent) {
if let Some(Node::Item(i)) = ir.tcx.hir().find(parent) {
if i.attrs.iter().any(|a| a.check_name(sym::automatically_derived)) {
return;
}
Expand Down Expand Up @@ -1327,12 +1327,11 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}
}

fn access_var(&mut self, hir_id: HirId, nid: NodeId, succ: LiveNode, acc: u32, span: Span)
fn access_var(&mut self, hir_id: HirId, var_hid: HirId, succ: LiveNode, acc: u32, span: Span)
-> LiveNode {
let ln = self.live_node(hir_id, span);
if acc != 0 {
self.init_from_succ(ln, succ);
let var_hid = self.ir.tcx.hir().node_to_hir_id(nid);
let var = self.variable(var_hid, span);
self.acc(ln, var, acc);
}
Expand All @@ -1345,8 +1344,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
Res::Local(hid) => {
let upvars = self.ir.tcx.upvars(self.ir.body_owner);
if !upvars.map_or(false, |upvars| upvars.contains_key(&hid)) {
let nid = self.ir.tcx.hir().hir_to_node_id(hid);
self.access_var(hir_id, nid, succ, acc, path.span)
self.access_var(hir_id, hid, succ, acc, path.span)
} else {
succ
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ impl<'tcx> cmt_<'tcx> {
"non-place".into()
}
Categorization::Local(vid) => {
if tcx.hir().is_argument(tcx.hir().hir_to_node_id(vid)) {
if tcx.hir().is_argument(vid) {
"argument"
} else {
"local variable"
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn method_might_be_inlined<'tcx>(
return true
}
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) {
match tcx.hir().find_by_hir_id(impl_hir_id) {
match tcx.hir().find(impl_hir_id) {
Some(Node::Item(item)) =>
item_might_be_inlined(tcx, &item, codegen_fn_attrs),
Some(..) | None =>
Expand Down Expand Up @@ -147,7 +147,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
None => { return false; }
};

match self.tcx.hir().find_by_hir_id(hir_id) {
match self.tcx.hir().find(hir_id) {
Some(Node::Item(item)) => {
match item.node {
hir::ItemKind::Fn(..) =>
Expand Down Expand Up @@ -205,7 +205,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
continue
}

if let Some(ref item) = self.tcx.hir().find_by_hir_id(search_item) {
if let Some(ref item) = self.tcx.hir().find(search_item) {
self.propagate_node(item, search_item);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
if let Some(Region::LateBound(_, def_id, _)) = def {
if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) {
// Ensure that the parent of the def is an item, not HRTB
let parent_id = self.tcx.hir().get_parent_node_by_hir_id(hir_id);
let parent_id = self.tcx.hir().get_parent_node(hir_id);
let parent_impl_id = hir::ImplItemId { hir_id: parent_id };
let parent_trait_id = hir::TraitItemId { hir_id: parent_id };
let krate = self.tcx.hir().forest.krate();
Expand Down Expand Up @@ -1489,7 +1489,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
}
};
if let Node::Lifetime(hir_lifetime) = self.tcx.hir().get(lifetime.hir_id) {
if let Some(parent) = self.tcx.hir().find_by_hir_id(
if let Some(parent) = self.tcx.hir().find(
self.tcx.hir().get_parent_item(hir_lifetime.hir_id))
{
match parent {
Expand Down Expand Up @@ -2051,7 +2051,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
// and whether there's a `self` argument (treated specially).
let mut assoc_item_kind = None;
let mut impl_self = None;
let parent = self.tcx.hir().get_parent_node_by_hir_id(output.hir_id);
let parent = self.tcx.hir().get_parent_node(output.hir_id);
let body = match self.tcx.hir().get(parent) {
// `fn` definitions and methods.
Node::Item(&hir::Item {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
err: &mut DiagnosticBuilder<'tcx>,
) {
if let &ObligationCauseCode::VariableType(hir_id) = code {
let parent_node = self.tcx.hir().get_parent_node_by_hir_id(hir_id);
if let Some(Node::Local(ref local)) = self.tcx.hir().find_by_hir_id(parent_node) {
let parent_node = self.tcx.hir().get_parent_node(hir_id);
if let Some(Node::Local(ref local)) = self.tcx.hir().find(parent_node) {
if let Some(ref expr) = local.init {
if let hir::ExprKind::Index(_, _) = expr.node {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
Expand Down Expand Up @@ -1013,8 +1013,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
) {
let hir = self.tcx.hir();
let parent_node = hir.get_parent_node_by_hir_id(obligation.cause.body_id);
let node = hir.find_by_hir_id(parent_node);
let parent_node = hir.get_parent_node(obligation.cause.body_id);
let node = hir.find(parent_node);
if let Some(hir::Node::Item(hir::Item {
node: hir::ItemKind::Fn(decl, _, _, body_id),
..
Expand Down
Loading