Skip to content
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

Remove NodeId from even more HIR nodes #58836

Merged
merged 8 commits into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 35 additions & 29 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use crate::ty::{self, TyCtxt};
use crate::ty::query::Providers;
use crate::middle::privacy;
use crate::session::config;
use crate::util::nodemap::{NodeSet, FxHashSet};
use crate::util::nodemap::{HirIdSet, FxHashSet};

use rustc_target::spec::abi::Abi;
use syntax::ast;
use crate::hir;
use crate::hir::def_id::LOCAL_CRATE;
use crate::hir::intravisit::{Visitor, NestedVisitorMap};
Expand Down Expand Up @@ -70,10 +69,10 @@ struct ReachableContext<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
tables: &'a ty::TypeckTables<'tcx>,
// The set of items which must be exported in the linkage sense.
reachable_symbols: NodeSet,
reachable_symbols: HirIdSet,
// A worklist of item IDs. Each item ID in this worklist will be inlined
// and will be scanned for further references.
worklist: Vec<ast::NodeId>,
worklist: Vec<hir::HirId>,
// Whether any output of this compilation is a library
any_library: bool,
}
Expand Down Expand Up @@ -104,27 +103,28 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> {

match def {
Some(Def::Local(node_id)) | Some(Def::Upvar(node_id, ..)) => {
self.reachable_symbols.insert(node_id);
let hir_id = self.tcx.hir().node_to_hir_id(node_id);
self.reachable_symbols.insert(hir_id);
}
Some(def) => {
if let Some((node_id, def_id)) = def.opt_def_id().and_then(|def_id| {
self.tcx.hir().as_local_node_id(def_id).map(|node_id| (node_id, def_id))
if let Some((hir_id, def_id)) = def.opt_def_id().and_then(|def_id| {
self.tcx.hir().as_local_hir_id(def_id).map(|hir_id| (hir_id, def_id))
}) {
if self.def_id_represents_local_inlined_item(def_id) {
self.worklist.push(node_id);
self.worklist.push(hir_id);
} else {
match def {
// If this path leads to a constant, then we need to
// recurse into the constant to continue finding
// items that are reachable.
Def::Const(..) | Def::AssociatedConst(..) => {
self.worklist.push(node_id);
self.worklist.push(hir_id);
}

// If this wasn't a static, then the destination is
// surely reachable.
_ => {
self.reachable_symbols.insert(node_id);
self.reachable_symbols.insert(hir_id);
}
}
}
Expand Down Expand Up @@ -204,14 +204,14 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
continue
}

if let Some(ref item) = self.tcx.hir().find(search_item) {
if let Some(ref item) = self.tcx.hir().find_by_hir_id(search_item) {
self.propagate_node(item, search_item);
}
}
}

fn propagate_node(&mut self, node: &Node<'tcx>,
search_item: ast::NodeId) {
search_item: hir::HirId) {
if !self.any_library {
// If we are building an executable, only explicitly extern
// types need to be exported.
Expand All @@ -221,7 +221,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
} else {
false
};
let def_id = self.tcx.hir().local_def_id(item.id);
let def_id = self.tcx.hir().local_def_id_from_hir_id(item.hir_id);
let codegen_attrs = self.tcx.codegen_fn_attrs(def_id);
let is_extern = codegen_attrs.contains_extern_indicator();
let std_internal = codegen_attrs.flags.contains(
Expand All @@ -242,7 +242,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
Node::Item(item) => {
match item.node {
hir::ItemKind::Fn(.., body) => {
let def_id = self.tcx.hir().local_def_id(item.id);
let def_id = self.tcx.hir().local_def_id_from_hir_id(item.hir_id);
if item_might_be_inlined(self.tcx,
&item,
self.tcx.codegen_fn_attrs(def_id)) {
Expand Down Expand Up @@ -295,7 +295,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
self.visit_nested_body(body);
}
hir::ImplItemKind::Method(_, body) => {
let did = self.tcx.hir().get_parent_did(search_item);
let did = self.tcx.hir().get_parent_did_by_hir_id(search_item);
if method_might_be_inlined(self.tcx, impl_item, did) {
self.visit_nested_body(body)
}
Expand All @@ -317,7 +317,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
_ => {
bug!(
"found unexpected node kind in worklist: {} ({:?})",
self.tcx.hir().node_to_string(search_item),
self.tcx.hir().hir_to_string(search_item),
node,
);
}
Expand All @@ -336,7 +336,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
struct CollectPrivateImplItemsVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
access_levels: &'a privacy::AccessLevels,
worklist: &'a mut Vec<ast::NodeId>,
worklist: &'a mut Vec<hir::HirId>,
}

impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx> {
Expand All @@ -348,13 +348,18 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
let codegen_attrs = self.tcx.codegen_fn_attrs(def_id);
if codegen_attrs.contains_extern_indicator() ||
codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
self.worklist.push(item.id);
self.worklist.push(item.hir_id);
}

// We need only trait impls here, not inherent impls, and only non-exported ones
if let hir::ItemKind::Impl(.., Some(ref trait_ref), _, ref impl_item_refs) = item.node {
if !self.access_levels.is_reachable(item.id) {
self.worklist.extend(impl_item_refs.iter().map(|r| r.id.node_id));
Zoxc marked this conversation as resolved.
Show resolved Hide resolved
let node_id = self.tcx.hir().hir_to_node_id(item.hir_id);
if !self.access_levels.is_reachable(node_id) {
// FIXME(@ljedrz): rework back to a nice extend when item Ids contain HirId
for impl_item_ref in impl_item_refs {
let hir_id = self.tcx.hir().node_to_hir_id(impl_item_ref.id.node_id);
self.worklist.push(hir_id);
}

let trait_def_id = match trait_ref.path.def {
Def::Trait(def_id) => def_id,
Expand All @@ -368,11 +373,11 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
let provided_trait_methods = self.tcx.provided_trait_methods(trait_def_id);
self.worklist.reserve(provided_trait_methods.len());
for default_method in provided_trait_methods {
let node_id = self.tcx
.hir()
.as_local_node_id(default_method.def_id)
.unwrap();
self.worklist.push(node_id);
let hir_id = self.tcx
.hir()
.as_local_hir_id(default_method.def_id)
.unwrap();
self.worklist.push(hir_id);
}
}
}
Expand All @@ -388,7 +393,7 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
// We introduce a new-type here, so we can have a specialized HashStable
// implementation for it.
#[derive(Clone)]
pub struct ReachableSet(pub Lrc<NodeSet>);
pub struct ReachableSet(pub Lrc<HirIdSet>);

fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) -> ReachableSet {
debug_assert!(crate_num == LOCAL_CRATE);
Expand All @@ -412,11 +417,12 @@ fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) ->
// If other crates link to us, they're going to expect to be able to
// use the lang items, so we need to be sure to mark them as
// exported.
reachable_context.worklist.extend(access_levels.map.iter().map(|(id, _)| *id));
reachable_context.worklist.extend(
access_levels.map.iter().map(|(id, _)| tcx.hir().node_to_hir_id(*id)));
for item in tcx.lang_items().items().iter() {
if let Some(did) = *item {
if let Some(node_id) = tcx.hir().as_local_node_id(did) {
reachable_context.worklist.push(node_id);
if let Some(hir_id) = tcx.hir().as_local_hir_id(did) {
reachable_context.worklist.push(hir_id);
}
}
}
Expand Down
Loading