Skip to content

Commit

Permalink
Rollup merge of rust-lang#61984 - ljedrz:more_node_id_pruning, r=Zoxc
Browse files Browse the repository at this point in the history
More NodeId pruning

Just another round of the `HirId`ification initiative.

r? @Zoxc
  • Loading branch information
Centril authored Jun 21, 2019
2 parents 68c1b11 + 0a511cc commit 912ab3a
Show file tree
Hide file tree
Showing 63 changed files with 141 additions and 177 deletions.
6 changes: 3 additions & 3 deletions src/librustc/hir/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::hir as ast;
use crate::hir::map;
use crate::hir::{Expr, FnDecl, Node};
use crate::hir::intravisit::FnKind;
use syntax::ast::{Attribute, Ident, NodeId};
use syntax::ast::{Attribute, Ident};
use syntax_pos::Span;

/// An FnLikeNode is a Node that is like a fn, in that it has a decl
Expand Down Expand Up @@ -83,11 +83,11 @@ impl<'a> Code<'a> {
}

/// Attempts to construct a Code from presumed FnLike or Expr node input.
pub fn from_node(map: &map::Map<'a>, id: NodeId) -> Option<Code<'a>> {
pub fn from_node(map: &map::Map<'a>, id: ast::HirId) -> Option<Code<'a>> {
match map.get(id) {
map::Node::Block(_) => {
// Use the parent, hopefully an expression node.
Code::from_node(map, map.get_parent_node(id))
Code::from_node(map, map.get_parent_node_by_hir_id(id))
}
map::Node::Expr(expr) => Some(Code::Expr(expr)),
node => FnLikeNode::from_node(node).map(Code::FnLike)
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,6 @@ impl Definitions {
self.node_to_hir_id[node_id]
}

#[inline]
pub fn def_index_to_node_id(&self, def_index: DefIndex) -> ast::NodeId {
self.as_local_node_id(DefId::local(def_index)).unwrap()
}

/// Retrieves the span of the given `DefId` if `DefId` is in the local crate, the span exists
/// and it's not `DUMMY_SP`.
#[inline]
Expand Down
51 changes: 16 additions & 35 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ impl<'hir> Map<'hir> {
self.definitions.def_index_to_hir_id(def_id.to_def_id().index)
}

fn def_kind(&self, node_id: NodeId) -> Option<DefKind> {
let node = if let Some(node) = self.find(node_id) {
fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
let node = if let Some(node) = self.find_by_hir_id(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(self.get_parent_node(node_id)) {
let ctor_of = match self.find_by_hir_id(self.get_parent_node_by_hir_id(hir_id)) {
Some(Node::Item(..)) => def::CtorOf::Struct,
Some(Node::Variant(..)) => def::CtorOf::Variant,
_ => unreachable!(),
Expand Down Expand Up @@ -458,7 +458,7 @@ impl<'hir> Map<'hir> {
}

pub fn body_owner_kind(&self, id: HirId) -> BodyOwnerKind {
match self.get_by_hir_id(id) {
match self.get(id) {
Node::Item(&Item { node: ItemKind::Const(..), .. }) |
Node::TraitItem(&TraitItem { node: TraitItemKind::Const(..), .. }) |
Node::ImplItem(&ImplItem { node: ImplItemKind::Const(..), .. }) |
Expand All @@ -482,7 +482,7 @@ impl<'hir> Map<'hir> {
}

pub fn ty_param_owner(&self, id: HirId) -> HirId {
match self.get_by_hir_id(id) {
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),
Expand All @@ -491,7 +491,7 @@ impl<'hir> Map<'hir> {
}

pub fn ty_param_name(&self, id: HirId) -> Name {
match self.get_by_hir_id(id) {
match self.get(id) {
Node::Item(&Item { node: ItemKind::Trait(..), .. }) |
Node::Item(&Item { node: ItemKind::TraitAlias(..), .. }) => kw::SelfUpper,
Node::GenericParam(param) => param.name.ident().name,
Expand Down Expand Up @@ -561,20 +561,14 @@ impl<'hir> Map<'hir> {
}

/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
pub fn get(&self, id: NodeId) -> Node<'hir> {
let hir_id = self.node_to_hir_id(id);
self.get_by_hir_id(hir_id)
}

// FIXME(@ljedrz): replace the `NodeId` variant.
pub fn get_by_hir_id(&self, id: HirId) -> Node<'hir> {
pub fn get(&self, id: HirId) -> Node<'hir> {
// read recorded by `find`
self.find_by_hir_id(id).unwrap_or_else(||
bug!("couldn't find hir id {} in the HIR map", id))
}

pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
self.as_local_node_id(id).map(|id| self.get(id)) // read recorded by `get`
self.as_local_hir_id(id).map(|id| self.get(id)) // read recorded by `get`
}

pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics> {
Expand Down Expand Up @@ -846,7 +840,7 @@ impl<'hir> Map<'hir> {
if scope == CRATE_HIR_ID {
return Some(CRATE_HIR_ID);
}
match self.get_by_hir_id(scope) {
match self.get(scope) {
Node::Item(i) => {
match i.node {
ItemKind::Existential(ExistTy { impl_trait_fn: None, .. }) => {}
Expand Down Expand Up @@ -927,28 +921,15 @@ impl<'hir> Map<'hir> {
}
}

pub fn expect_expr(&self, id: NodeId) -> &'hir Expr {
let hir_id = self.node_to_hir_id(id);
self.expect_expr_by_hir_id(hir_id)
}

// FIXME(@ljedrz): replace the `NodeId` variant.
pub fn expect_expr_by_hir_id(&self, id: HirId) -> &'hir Expr {
pub fn expect_expr(&self, id: HirId) -> &'hir Expr {
match self.find_by_hir_id(id) { // read recorded by find
Some(Node::Expr(expr)) => expr,
_ => bug!("expected expr, found {}", self.node_to_string(id))
}
}

/// Returns the name associated with the given `NodeId`'s AST.
pub fn name(&self, id: NodeId) -> Name {
let hir_id = self.node_to_hir_id(id);
self.name_by_hir_id(hir_id)
}

// FIXME(@ljedrz): replace the `NodeId` variant.
pub fn name_by_hir_id(&self, id: HirId) -> Name {
match self.get_by_hir_id(id) {
pub fn name(&self, id: HirId) -> Name {
match self.get(id) {
Node::Item(i) => i.ident.name,
Node::ForeignItem(fi) => fi.ident.name,
Node::ImplItem(ii) => ii.ident.name,
Expand All @@ -958,7 +939,7 @@ impl<'hir> Map<'hir> {
Node::Lifetime(lt) => lt.name.ident().name,
Node::GenericParam(param) => param.name.ident().name,
Node::Binding(&Pat { node: PatKind::Binding(_, _, l, _), .. }) => l.name,
Node::Ctor(..) => self.name_by_hir_id(self.get_parent_item(id)),
Node::Ctor(..) => self.name(self.get_parent_item(id)),
_ => bug!("no name for {}", self.node_to_string(id))
}
}
Expand Down Expand Up @@ -1080,7 +1061,7 @@ impl<'hir> Map<'hir> {
}

pub fn hir_to_pretty_string(&self, id: HirId) -> String {
print::to_string(self, |s| s.print_node(self.get_by_hir_id(id)))
print::to_string(self, |s| s.print_node(self.get(id)))
}
}

Expand Down Expand Up @@ -1407,8 +1388,8 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {

pub fn provide(providers: &mut Providers<'_>) {
providers.def_kind = |tcx, def_id| {
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
tcx.hir().def_kind(node_id)
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
tcx.hir().def_kind(hir_id)
} else {
bug!("calling local def_kind query provider for upstream DefId: {:?}",
def_id
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
hir::MatchSource::TryDesugar => {
if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
let discrim_expr = self.tcx.hir().expect_expr_by_hir_id(discrim_hir_id);
let discrim_expr = self.tcx.hir().expect_expr(discrim_hir_id);
let discrim_ty = if let hir::ExprKind::Call(_, args) = &discrim_expr.node {
let arg_expr = args.first().expect("try desugaring call w/out arg");
self.in_progress_tables.and_then(|tables| {
Expand Down Expand Up @@ -1335,7 +1335,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
// instead we suggest `T: 'a + 'b` in that case.
let mut has_bounds = false;
if let Node::GenericParam(ref param) = hir.get_by_hir_id(id) {
if let Node::GenericParam(ref param) = hir.get(id) {
has_bounds = !param.bounds.is_empty();
}
let sp = hir.span(id);
Expand Down Expand Up @@ -1583,7 +1583,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
format!(" for lifetime parameter `{}` in coherence check", name)
}
infer::UpvarRegion(ref upvar_id, _) => {
let var_name = self.tcx.hir().name_by_hir_id(upvar_id.var_path.hir_id);
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
format!(" for capture of `{}` by closure", var_name)
}
infer::NLL(..) => bug!("NLL variable found in lexical phase"),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
};

if let Some(body_id) = body_id {
let expr = self.tcx.hir().expect_expr_by_hir_id(body_id.hir_id);
let expr = self.tcx.hir().expect_expr(body_id.hir_id);
local_visitor.visit_expr(expr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
let def_id = anon_reg.def_id;
if let Some(hir_id) = self.tcx().hir().as_local_hir_id(def_id) {
let fndecl = match self.tcx().hir().get_by_hir_id(hir_id) {
let fndecl = match self.tcx().hir().get(hir_id) {
Node::Item(&hir::Item {
node: hir::ItemKind::Fn(ref fndecl, ..),
..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
if let Node::Expr(Expr {
node: Closure(_, _, _, closure_span, None),
..
}) = hir.get_by_hir_id(hir_id) {
}) = hir.get(hir_id) {
let sup_sp = sup_origin.span();
let origin_sp = origin.span();
let mut err = self.tcx().sess.struct_span_err(
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/error_reporting/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
"...so that reference does not outlive borrowed content");
}
infer::ReborrowUpvar(span, ref upvar_id) => {
let var_name = self.tcx.hir().name_by_hir_id(upvar_id.var_path.hir_id);
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
err.span_note(span,
&format!("...so that closure can access `{}`", var_name));
}
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
err
}
infer::ReborrowUpvar(span, ref upvar_id) => {
let var_name = self.tcx.hir().name_by_hir_id(upvar_id.var_path.hir_id);
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
let mut err = struct_span_err!(self.tcx.sess,
span,
E0313,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub enum SubregionOrigin<'tcx> {
DerefPointer(Span),

/// Closure bound must not outlive captured variables
ClosureCapture(Span, ast::NodeId),
ClosureCapture(Span, hir::HirId),

/// Index into slice must be within its lifetime
IndexSlice(Span),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,8 +945,8 @@ pub fn may_define_existential_type(
let mut hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
trace!(
"may_define_existential_type(def={:?}, opaque_node={:?})",
tcx.hir().get_by_hir_id(hir_id),
tcx.hir().get_by_hir_id(opaque_hir_id)
tcx.hir().get(hir_id),
tcx.hir().get(opaque_hir_id)
);

// Named existential types can be defined by any siblings or children of siblings.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
);

if self.ir.variable_is_shorthand(var) {
if let Node::Binding(pat) = self.ir.tcx.hir().get_by_hir_id(hir_id) {
if let Node::Binding(pat) = self.ir.tcx.hir().get(hir_id) {
// Handle `ref` and `ref mut`.
let spans = spans.iter()
.map(|_span| (pat.span, format!("{}: _", name)))
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 @@ -344,7 +344,7 @@ impl MutabilityCategory {
tables: &ty::TypeckTables<'_>,
id: hir::HirId,
) -> MutabilityCategory {
let ret = match tcx.hir().get_by_hir_id(id) {
let ret = match tcx.hir().get(id) {
Node::Binding(p) => match p.node {
PatKind::Binding(..) => {
let bm = *tables.pat_binding_modes()
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Scope {
}
let span = tcx.hir().span(hir_id);
if let ScopeData::Remainder(first_statement_index) = self.data {
if let Node::Block(ref blk) = tcx.hir().get_by_hir_id(hir_id) {
if let Node::Block(ref blk) = tcx.hir().get(hir_id) {
// Want span for scope starting after the
// indexed statement and ending at end of
// `blk`; reuse span of `blk` and shift `lo`
Expand Down Expand Up @@ -1368,7 +1368,7 @@ fn region_scope_tree<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx ScopeTree
// If the item is an associated const or a method,
// record its impl/trait parent, as it can also have
// lifetime parameters free in this body.
match tcx.hir().get_by_hir_id(id) {
match tcx.hir().get(id) {
Node::ImplItem(_) |
Node::TraitItem(_) => {
visitor.scope_tree.root_parent = Some(tcx.hir().get_parent_item(id));
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
}
}
};
if let Node::Lifetime(hir_lifetime) = self.tcx.hir().get_by_hir_id(lifetime.hir_id) {
if let Node::Lifetime(hir_lifetime) = self.tcx.hir().get(lifetime.hir_id) {
if let Some(parent) = self.tcx.hir().find_by_hir_id(
self.tcx.hir().get_parent_item(hir_lifetime.hir_id))
{
Expand Down Expand Up @@ -1569,7 +1569,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
Some(LifetimeUseSet::One(lifetime)) => {
let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap();
debug!("hir id first={:?}", hir_id);
if let Some((id, span, name)) = match self.tcx.hir().get_by_hir_id(hir_id) {
if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) {
Node::Lifetime(hir_lifetime) => Some((
hir_lifetime.hir_id,
hir_lifetime.span,
Expand Down Expand Up @@ -1620,7 +1620,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
}
None => {
let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap();
if let Some((id, span, name)) = match self.tcx.hir().get_by_hir_id(hir_id) {
if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) {
Node::Lifetime(hir_lifetime) => Some((
hir_lifetime.hir_id,
hir_lifetime.span,
Expand Down Expand Up @@ -1823,7 +1823,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
// Do not free early-bound regions, only late-bound ones.
} else if let Some(body_id) = outermost_body {
let fn_id = self.tcx.hir().body_owner(body_id);
match self.tcx.hir().get_by_hir_id(fn_id) {
match self.tcx.hir().get(fn_id) {
Node::Item(&hir::Item {
node: hir::ItemKind::Fn(..),
..
Expand Down Expand Up @@ -2052,7 +2052,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
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 body = match self.tcx.hir().get_by_hir_id(parent) {
let body = match self.tcx.hir().get(parent) {
// `fn` definitions and methods.
Node::Item(&hir::Item {
node: hir::ItemKind::Fn(.., body),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ impl<'tcx> TyCtxt<'tcx> {

let mut diag = self.struct_span_lint_hir(lint, id, span, &msg);
if let Some(suggestion) = suggestion {
if let hir::Node::Expr(_) = self.hir().get_by_hir_id(id) {
if let hir::Node::Expr(_) = self.hir().get(id) {
diag.span_suggestion(
span,
"replace the use of the deprecated item",
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2573,7 +2573,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {

if let Some(upvars) = tcx.upvars(def_id) {
for (&var_id, place) in upvars.keys().zip(places) {
let var_name = tcx.hir().name_by_hir_id(var_id);
let var_name = tcx.hir().name(var_id);
struct_fmt.field(&var_name.as_str(), place);
}
}
Expand All @@ -2592,7 +2592,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {

if let Some(upvars) = tcx.upvars(def_id) {
for (&var_id, place) in upvars.keys().zip(places) {
let var_name = tcx.hir().name_by_hir_id(var_id);
let var_name = tcx.hir().name(var_id);
struct_fmt.field(&var_name.as_str(), place);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
code: &ObligationCauseCode<'tcx>,
err: &mut DiagnosticBuilder<'tcx>,
) {
if let &ObligationCauseCode::VariableType(node_id) = code {
let parent_node = self.tcx.hir().get_parent_node(node_id);
if let Some(Node::Local(ref local)) = self.tcx.hir().find(parent_node) {
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) {
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
2 changes: 1 addition & 1 deletion src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub enum ObligationCauseCode<'tcx> {
/// S { ... } must be Sized
StructInitializerSized,
/// Type of each variable must be Sized
VariableType(ast::NodeId),
VariableType(hir::HirId),
/// Argument type must be Sized
SizedArgumentType,
/// Return type must be Sized
Expand Down
Loading

0 comments on commit 912ab3a

Please sign in to comment.