Skip to content

Commit

Permalink
Rollup merge of rust-lang#59413 - Zoxc:hirid, r=oli-obk
Browse files Browse the repository at this point in the history
HirIdify hir::ItemId

Version of rust-lang#59092.

r? @oli-obk
  • Loading branch information
Centril authored Mar 28, 2019
2 parents e04b7b8 + f7c66fb commit 95ef5eb
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub trait Visitor<'v> : Sized {
/// but cannot supply a `Map`; see `nested_visit_map` for advice.
#[allow(unused_variables)]
fn visit_nested_item(&mut self, id: ItemId) {
let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item(id.id));
let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item_by_hir_id(id.id));
if let Some(item) = opt_item {
self.visit_item(item);
}
Expand Down
125 changes: 75 additions & 50 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ use rustc_data_structures::thin_vec::ThinVec;
use rustc_data_structures::sync::Lrc;

use std::collections::{BTreeSet, BTreeMap};
use std::fmt::Debug;
use std::mem;
use smallvec::SmallVec;
use syntax::attr;
Expand Down Expand Up @@ -82,7 +81,7 @@ pub struct LoweringContext<'a> {
resolver: &'a mut dyn Resolver,

/// The items being lowered are collected here.
items: BTreeMap<NodeId, hir::Item>,
items: BTreeMap<hir::HirId, hir::Item>,

trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
Expand Down Expand Up @@ -321,7 +320,7 @@ enum AnonymousLifetimeMode {
PassThrough,
}

struct ImplTraitTypeIdVisitor<'a> { ids: &'a mut SmallVec<[hir::ItemId; 1]> }
struct ImplTraitTypeIdVisitor<'a> { ids: &'a mut SmallVec<[NodeId; 1]> }

impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
fn visit_ty(&mut self, ty: &'a Ty) {
Expand All @@ -330,7 +329,7 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
| TyKind::BareFn(_)
=> return,

TyKind::ImplTrait(id, _) => self.ids.push(hir::ItemId { id }),
TyKind::ImplTrait(id, _) => self.ids.push(id),
_ => {},
}
visit::walk_ty(self, ty);
Expand Down Expand Up @@ -361,9 +360,40 @@ impl<'a> LoweringContext<'a> {
lctx: &'lcx mut LoweringContext<'interner>,
}

impl MiscCollector<'_, '_> {
fn allocate_use_tree_hir_id_counters(
&mut self,
tree: &UseTree,
owner: DefIndex,
) {
match tree.kind {
UseTreeKind::Simple(_, id1, id2) => {
for &id in &[id1, id2] {
self.lctx.resolver.definitions().create_def_with_parent(
owner,
id,
DefPathData::Misc,
DefIndexAddressSpace::High,
Mark::root(),
tree.prefix.span,
);
self.lctx.allocate_hir_id_counter(id);
}
}
UseTreeKind::Glob => (),
UseTreeKind::Nested(ref trees) => {
for &(ref use_tree, id) in trees {
let hir_id = self.lctx.allocate_hir_id_counter(id).hir_id;
self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
}
}
}
}
}

impl<'lcx, 'interner> Visitor<'lcx> for MiscCollector<'lcx, 'interner> {
fn visit_item(&mut self, item: &'lcx Item) {
self.lctx.allocate_hir_id_counter(item.id, item);
let hir_id = self.lctx.allocate_hir_id_counter(item.id).hir_id;

match item.node {
ItemKind::Struct(_, ref generics)
Expand All @@ -383,18 +413,21 @@ impl<'a> LoweringContext<'a> {
.count();
self.lctx.type_def_lifetime_params.insert(def_id, count);
}
ItemKind::Use(ref use_tree) => {
self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
}
_ => {}
}
visit::walk_item(self, item);
}

fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
self.lctx.allocate_hir_id_counter(item.id, item);
self.lctx.allocate_hir_id_counter(item.id);
visit::walk_trait_item(self, item);
}

fn visit_impl_item(&mut self, item: &'lcx ImplItem) {
self.lctx.allocate_hir_id_counter(item.id, item);
self.lctx.allocate_hir_id_counter(item.id);
visit::walk_impl_item(self, item);
}
}
Expand Down Expand Up @@ -434,17 +467,16 @@ impl<'a> LoweringContext<'a> {
}

fn visit_item(&mut self, item: &'lcx Item) {
let mut item_lowered = true;
let mut item_hir_id = None;
self.lctx.with_hir_id_owner(item.id, |lctx| {
if let Some(hir_item) = lctx.lower_item(item) {
lctx.insert_item(item.id, hir_item);
} else {
item_lowered = false;
item_hir_id = Some(hir_item.hir_id);
lctx.insert_item(hir_item);
}
});

if item_lowered {
let item_generics = match self.lctx.items.get(&item.id).unwrap().node {
if let Some(hir_id) = item_hir_id {
let item_generics = match self.lctx.items.get(&hir_id).unwrap().node {
hir::ItemKind::Impl(_, _, _, ref generics, ..)
| hir::ItemKind::Trait(_, _, ref generics, ..) => {
generics.params.clone()
Expand Down Expand Up @@ -516,20 +548,21 @@ impl<'a> LoweringContext<'a> {
}
}

fn insert_item(&mut self, id: NodeId, item: hir::Item) {
fn insert_item(&mut self, item: hir::Item) {
let id = item.hir_id;
// FIXME: Use debug_asset-rt
assert_eq!(id.local_id, hir::ItemLocalId::from_u32(0));
self.items.insert(id, item);
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
}

fn allocate_hir_id_counter<T: Debug>(&mut self, owner: NodeId, debug: &T) -> LoweredNodeId {
if self.item_local_id_counters.insert(owner, 0).is_some() {
bug!(
"Tried to allocate item_local_id_counter for {:?} twice",
debug
);
}
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> LoweredNodeId {
// Setup the counter if needed
self.item_local_id_counters.entry(owner).or_insert(0);
// Always allocate the first `HirId` for the owner itself.
self.lower_node_id_with_owner(owner, owner)
let lowered = self.lower_node_id_with_owner(owner, owner);
debug_assert_eq!(lowered.hir_id.local_id.as_u32(), 0);
lowered
}

fn lower_node_id_generic<F>(&mut self, ast_node_id: NodeId, alloc_hir_id: F) -> LoweredNodeId
Expand Down Expand Up @@ -1381,7 +1414,7 @@ impl<'a> LoweringContext<'a> {
.opt_def_index(exist_ty_node_id)
.unwrap();

self.allocate_hir_id_counter(exist_ty_node_id, &"existential impl trait");
self.allocate_hir_id_counter(exist_ty_node_id);

let hir_bounds = self.with_hir_id_owner(exist_ty_node_id, lower_bounds);

Expand Down Expand Up @@ -1422,10 +1455,10 @@ impl<'a> LoweringContext<'a> {
// Insert the item into the global list. This usually happens
// automatically for all AST items. But this existential type item
// does not actually exist in the AST.
lctx.insert_item(exist_ty_id.node_id, exist_ty_item);
lctx.insert_item(exist_ty_item);

// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.node_id }, lifetimes)
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.hir_id }, lifetimes)
})
}

Expand Down Expand Up @@ -2002,9 +2035,9 @@ impl<'a> LoweringContext<'a> {
)
}

fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[hir::ItemId; 1]>) {
fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[NodeId; 1]>) {
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(l.id);
let mut ids = SmallVec::<[hir::ItemId; 1]>::new();
let mut ids = SmallVec::<[NodeId; 1]>::new();
if self.sess.features_untracked().impl_trait_in_bindings {
if let Some(ref ty) = l.ty {
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
Expand Down Expand Up @@ -3065,7 +3098,6 @@ impl<'a> LoweringContext<'a> {
}
}

let parent_def_index = self.current_hir_id_owner.last().unwrap().0;
let mut defs = self.expect_full_def_from_use(id);
// We want to return *something* from this function, so hold onto the first item
// for later.
Expand All @@ -3084,14 +3116,6 @@ impl<'a> LoweringContext<'a> {
seg.id = self.sess.next_node_id();
}
let span = path.span;
self.resolver.definitions().create_def_with_parent(
parent_def_index,
new_node_id,
DefPathData::Misc,
DefIndexAddressSpace::High,
Mark::root(),
span);
self.allocate_hir_id_counter(new_node_id, &path);

self.with_hir_id_owner(new_node_id, |this| {
let new_id = this.lower_node_id(new_node_id);
Expand All @@ -3114,7 +3138,6 @@ impl<'a> LoweringContext<'a> {
let vis = respan(vis.span, vis_kind);

this.insert_item(
new_id.node_id,
hir::Item {
hir_id: new_id.hir_id,
ident,
Expand Down Expand Up @@ -3174,8 +3197,6 @@ impl<'a> LoweringContext<'a> {

// Add all the nested `PathListItem`s to the HIR.
for &(ref use_tree, id) in trees {
self.allocate_hir_id_counter(id, &use_tree);

let LoweredNodeId {
node_id: new_id,
hir_id: new_hir_id,
Expand Down Expand Up @@ -3219,7 +3240,6 @@ impl<'a> LoweringContext<'a> {
let vis = respan(vis.span, vis_kind);

this.insert_item(
new_id,
hir::Item {
hir_id: new_hir_id,
ident,
Expand Down Expand Up @@ -3443,43 +3463,47 @@ impl<'a> LoweringContext<'a> {
}

fn lower_item_id(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
match i.node {
let node_ids = match i.node {
ItemKind::Use(ref use_tree) => {
let mut vec = smallvec![hir::ItemId { id: i.id }];
let mut vec = smallvec![i.id];
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
vec
}
ItemKind::MacroDef(..) => SmallVec::new(),
ItemKind::Fn(..) |
ItemKind::Impl(.., None, _, _) => smallvec![hir::ItemId { id: i.id }],
ItemKind::Impl(.., None, _, _) => smallvec![i.id],
ItemKind::Static(ref ty, ..) => {
let mut ids = smallvec![hir::ItemId { id: i.id }];
let mut ids = smallvec![i.id];
if self.sess.features_untracked().impl_trait_in_bindings {
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
visitor.visit_ty(ty);
}
ids
},
ItemKind::Const(ref ty, ..) => {
let mut ids = smallvec![hir::ItemId { id: i.id }];
let mut ids = smallvec![i.id];
if self.sess.features_untracked().impl_trait_in_bindings {
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
visitor.visit_ty(ty);
}
ids
},
_ => smallvec![hir::ItemId { id: i.id }],
}
_ => smallvec![i.id],
};

node_ids.into_iter().map(|node_id| hir::ItemId {
id: self.allocate_hir_id_counter(node_id).hir_id
}).collect()
}

fn lower_item_id_use_tree(&mut self,
tree: &UseTree,
base_id: NodeId,
vec: &mut SmallVec<[hir::ItemId; 1]>)
vec: &mut SmallVec<[NodeId; 1]>)
{
match tree.kind {
UseTreeKind::Nested(ref nested_vec) => for &(ref nested, id) in nested_vec {
vec.push(hir::ItemId { id });
vec.push(id);
self.lower_item_id_use_tree(nested, id, vec);
},
UseTreeKind::Glob => {}
Expand All @@ -3488,7 +3512,7 @@ impl<'a> LoweringContext<'a> {
.skip(1)
.zip([id1, id2].iter())
{
vec.push(hir::ItemId { id });
vec.push(id);
}
},
}
Expand Down Expand Up @@ -4604,6 +4628,7 @@ impl<'a> LoweringContext<'a> {
let mut ids: SmallVec<[hir::Stmt; 1]> = item_ids
.into_iter()
.map(|item_id| {
let item_id = hir::ItemId { id: self.lower_node_id(item_id).hir_id };
let LoweredNodeId { node_id: _, hir_id } = self.next_id();

hir::Stmt {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ impl<'hir> Map<'hir> {
let module = &self.forest.krate.modules[&node_id];

for id in &module.items {
visitor.visit_item(self.expect_item(*id));
visitor.visit_item(self.expect_item_by_hir_id(*id));
}

for id in &module.trait_items {
Expand Down Expand Up @@ -1293,7 +1293,7 @@ pub fn map_crate<'hir>(sess: &crate::session::Session,
impl<'hir> print::PpAnn for Map<'hir> {
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) -> io::Result<()> {
match nested {
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
Nested::Item(id) => state.print_item(self.expect_item_by_hir_id(id.id)),
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
Nested::Body(id) => state.print_expr(&self.body(id).value),
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ pub struct WhereEqPredicate {
pub struct ModuleItems {
// Use BTreeSets here so items are in the same order as in the
// list of all items in Crate
pub items: BTreeSet<NodeId>,
pub items: BTreeSet<HirId>,
pub trait_items: BTreeSet<TraitItemId>,
pub impl_items: BTreeSet<ImplItemId>,
}
Expand All @@ -722,7 +722,7 @@ pub struct Crate {
// does, because it can affect the order in which errors are
// detected, which in turn can make compile-fail tests yield
// slightly different results.
pub items: BTreeMap<NodeId, Item>,
pub items: BTreeMap<HirId, Item>,

pub trait_items: BTreeMap<TraitItemId, TraitItem>,
pub impl_items: BTreeMap<ImplItemId, ImplItem>,
Expand All @@ -741,7 +741,7 @@ pub struct Crate {
}

impl Crate {
pub fn item(&self, id: NodeId) -> &Item {
pub fn item(&self, id: HirId) -> &Item {
&self.items[&id]
}

Expand Down Expand Up @@ -2215,7 +2215,7 @@ impl VariantData {
// so it can fetched later.
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct ItemId {
pub id: NodeId,
pub id: HirId,
}

/// An item
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub trait PpAnn {
fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) -> io::Result<()> {
Ok(())
}
fn try_fetch_item(&self, _: ast::NodeId) -> Option<&hir::Item> {
fn try_fetch_item(&self, _: hir::HirId) -> Option<&hir::Item> {
None
}
}
Expand All @@ -58,7 +58,7 @@ impl PpAnn for NoAnn {}
pub const NO_ANN: &dyn PpAnn = &NoAnn;

impl PpAnn for hir::Crate {
fn try_fetch_item(&self, item: ast::NodeId) -> Option<&hir::Item> {
fn try_fetch_item(&self, item: hir::HirId) -> Option<&hir::Item> {
Some(self.item(item))
}
fn nested(&self, state: &mut State<'_>, nested: Nested) -> io::Result<()> {
Expand Down
Loading

0 comments on commit 95ef5eb

Please sign in to comment.