Skip to content

Commit 629f5ff

Browse files
committed
include a Name and Span for each item in the HIR of the impl
1 parent 26d1500 commit 629f5ff

File tree

17 files changed

+131
-83
lines changed

17 files changed

+131
-83
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ pub trait Visitor<'v> : Sized {
205205
fn visit_impl_item(&mut self, ii: &'v ImplItem) {
206206
walk_impl_item(self, ii)
207207
}
208+
fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) {
209+
walk_impl_item_ref(self, ii)
210+
}
208211
fn visit_trait_ref(&mut self, t: &'v TraitRef) {
209212
walk_trait_ref(self, t)
210213
}
@@ -399,13 +402,13 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
399402
visitor.visit_id(item.id);
400403
visitor.visit_trait_ref(trait_ref)
401404
}
402-
ItemImpl(.., ref type_parameters, ref opt_trait_reference, ref typ, ref impl_item_ids) => {
405+
ItemImpl(.., ref type_parameters, ref opt_trait_reference, ref typ, ref impl_item_refs) => {
403406
visitor.visit_id(item.id);
404407
visitor.visit_generics(type_parameters);
405408
walk_list!(visitor, visit_trait_ref, opt_trait_reference);
406409
visitor.visit_ty(typ);
407-
for &impl_item_id in impl_item_ids {
408-
visitor.visit_nested_impl_item(impl_item_id);
410+
for impl_item_ref in impl_item_refs {
411+
visitor.visit_impl_item_ref(impl_item_ref);
409412
}
410413
}
411414
ItemStruct(ref struct_definition, ref generics) |
@@ -763,6 +766,12 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
763766
}
764767
}
765768

769+
pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
770+
visitor.visit_nested_impl_item(impl_item_ref.id);
771+
visitor.visit_name(impl_item_ref.span, impl_item_ref.name);
772+
}
773+
774+
766775
pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V, struct_definition: &'v VariantData) {
767776
visitor.visit_id(struct_definition.id());
768777
walk_list!(visitor, visit_struct_field, struct_definition.fields());

src/librustc/hir/lowering.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'a> LoweringContext<'a> {
116116
}
117117

118118
fn visit_impl_item(&mut self, item: &ImplItem) {
119-
let id = self.lctx.lower_impl_item_id(item);
119+
let id = self.lctx.lower_impl_item_ref(item).id;
120120
self.impl_items.insert(id, self.lctx.lower_impl_item(item));
121121
visit::walk_impl_item(self, item);
122122
}
@@ -641,7 +641,7 @@ impl<'a> LoweringContext<'a> {
641641
}
642642
ItemKind::Impl(unsafety, polarity, ref generics, ref ifce, ref ty, ref impl_items) => {
643643
let new_impl_items = impl_items.iter()
644-
.map(|item| self.lower_impl_item_id(item))
644+
.map(|item| self.lower_impl_item_ref(item))
645645
.collect();
646646
let ifce = ifce.as_ref().map(|trait_ref| self.lower_trait_ref(trait_ref));
647647
hir::ItemImpl(self.lower_unsafety(unsafety),
@@ -717,8 +717,24 @@ impl<'a> LoweringContext<'a> {
717717
})
718718
}
719719

720-
fn lower_impl_item_id(&mut self, i: &ImplItem) -> hir::ImplItemId {
721-
hir::ImplItemId { id: i.id }
720+
fn lower_impl_item_ref(&mut self, i: &ImplItem) -> hir::ImplItemRef {
721+
hir::ImplItemRef {
722+
id: hir::ImplItemId { node_id: i.id },
723+
name: i.ident.name,
724+
span: i.span,
725+
vis: self.lower_visibility(&i.vis),
726+
defaultness: self.lower_defaultness(i.defaultness),
727+
kind: match i.node {
728+
ImplItemKind::Const(..) => hir::AssociatedItemKind::Const,
729+
ImplItemKind::Type(..) => hir::AssociatedItemKind::Type,
730+
ImplItemKind::Method(ref sig, _) => hir::AssociatedItemKind::Method {
731+
has_self: sig.decl.get_self().is_some(),
732+
},
733+
ImplItemKind::Macro(..) => unimplemented!(),
734+
},
735+
// since `default impl` is not yet implemented, this is always true in impls
736+
has_value: true,
737+
}
722738
}
723739

724740
fn lower_mod(&mut self, m: &Mod) -> hir::Mod {

src/librustc/hir/map/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
9393
/// deep walking so that we walk nested items in the context of
9494
/// their outer items.
9595
96-
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'v>> {
96+
fn nested_visit_map(&mut self) -> Option<&map::Map<'ast>> {
9797
panic!("visit_nested_xxx must be manually implemented in this visitor")
9898
}
9999

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl<'ast> Map<'ast> {
384384
}
385385

386386
pub fn impl_item(&self, id: ImplItemId) -> &'ast ImplItem {
387-
self.read(id.id);
387+
self.read(id.node_id);
388388

389389
// NB: intentionally bypass `self.forest.krate()` so that we
390390
// do not trigger a read of the whole krate here

src/librustc/hir/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ pub enum TraitItem_ {
10571057
// so it can fetched later.
10581058
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, RustcEncodable, RustcDecodable, Hash, Debug)]
10591059
pub struct ImplItemId {
1060-
pub id: NodeId,
1060+
pub node_id: NodeId,
10611061
}
10621062

10631063
/// Represents anything within an `impl` block
@@ -1546,7 +1546,7 @@ pub enum Item_ {
15461546
Generics,
15471547
Option<TraitRef>, // (optional) trait this impl implements
15481548
P<Ty>, // self
1549-
HirVec<ImplItemId>),
1549+
HirVec<ImplItemRef>),
15501550
}
15511551

15521552
impl Item_ {
@@ -1570,6 +1570,30 @@ impl Item_ {
15701570
}
15711571
}
15721572

1573+
/// A reference from an impl to one of its associated items. This
1574+
/// contains the item's id, naturally, but also the item's name and
1575+
/// some other high-level details (like whether it is an associated
1576+
/// type or method, and whether it is public). This allows other
1577+
/// passes to find the impl they want without loading the id (which
1578+
/// means fewer edges in the incremental compilation graph).
1579+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1580+
pub struct ImplItemRef {
1581+
pub id: ImplItemId,
1582+
pub name: Name,
1583+
pub kind: AssociatedItemKind,
1584+
pub span: Span,
1585+
pub vis: Visibility,
1586+
pub defaultness: Defaultness,
1587+
pub has_value: bool,
1588+
}
1589+
1590+
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1591+
pub enum AssociatedItemKind {
1592+
Const,
1593+
Method { has_self: bool },
1594+
Type,
1595+
}
1596+
15731597
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
15741598
pub struct ForeignItem {
15751599
pub name: Name,

src/librustc/hir/print.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,8 @@ impl<'a> State<'a> {
808808
space(&mut self.s)?;
809809
self.bopen()?;
810810
self.print_inner_attributes(&item.attrs)?;
811-
for &impl_item in impl_items {
812-
self.print_impl_item_id(impl_item)?;
811+
for impl_item in impl_items {
812+
self.print_impl_item_ref(impl_item)?;
813813
}
814814
self.bclose(item.span)?;
815815
}
@@ -1020,10 +1020,10 @@ impl<'a> State<'a> {
10201020
self.ann.post(self, NodeSubItem(ti.id))
10211021
}
10221022

1023-
pub fn print_impl_item_id(&mut self, item_id: hir::ImplItemId) -> io::Result<()> {
1023+
pub fn print_impl_item_ref(&mut self, item_ref: &hir::ImplItemRef) -> io::Result<()> {
10241024
if let Some(krate) = self.krate {
10251025
// skip nested items if krate context was not provided
1026-
let item = &krate.impl_item(item_id);
1026+
let item = &krate.impl_item(item_ref.id);
10271027
self.print_impl_item(item)
10281028
} else {
10291029
Ok(())

src/librustc/middle/dead.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,12 @@ impl<'v, 'k> ItemLikeVisitor<'v> for LifeSeeder<'k> {
359359
}
360360
}
361361
}
362-
hir::ItemImpl(.., ref opt_trait, _, ref impl_item_ids) => {
363-
for &impl_item_id in impl_item_ids {
364-
let impl_item = self.krate.impl_item(impl_item_id);
362+
hir::ItemImpl(.., ref opt_trait, _, ref impl_item_refs) => {
363+
for impl_item_ref in impl_item_refs {
364+
let impl_item = self.krate.impl_item(impl_item_ref.id);
365365
if opt_trait.is_some() ||
366366
has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
367-
self.worklist.push(impl_item_id.id);
367+
self.worklist.push(impl_item_ref.id.node_id);
368368
}
369369
}
370370
}

src/librustc/middle/reachable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ struct CollectPrivateImplItemsVisitor<'a> {
328328
impl<'a, 'v> ItemLikeVisitor<'v> for CollectPrivateImplItemsVisitor<'a> {
329329
fn visit_item(&mut self, item: &hir::Item) {
330330
// We need only trait impls here, not inherent impls, and only non-exported ones
331-
if let hir::ItemImpl(.., Some(_), _, ref impl_items) = item.node {
331+
if let hir::ItemImpl(.., Some(_), _, ref impl_item_refs) = item.node {
332332
if !self.access_levels.is_reachable(item.id) {
333-
for impl_item in impl_items {
334-
self.worklist.push(impl_item.id);
333+
for impl_item_ref in impl_item_refs {
334+
self.worklist.push(impl_item_ref.id.node_id);
335335
}
336336
}
337337
}

src/librustc/middle/stability.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,10 @@ pub fn check_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
525525
// For implementations of traits, check the stability of each item
526526
// individually as it's possible to have a stable trait with unstable
527527
// items.
528-
hir::ItemImpl(.., Some(ref t), _, ref impl_item_ids) => {
528+
hir::ItemImpl(.., Some(ref t), _, ref impl_item_refs) => {
529529
let trait_did = tcx.expect_def(t.ref_id).def_id();
530-
for &impl_item_id in impl_item_ids {
531-
let impl_item = tcx.map.impl_item(impl_item_id);
530+
for impl_item_ref in impl_item_refs {
531+
let impl_item = tcx.map.impl_item(impl_item_ref.id);
532532
let item = tcx.associated_items(trait_did)
533533
.find(|item| item.name == impl_item.name).unwrap();
534534
if warn_about_defns {

src/librustc/ty/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,9 +2190,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21902190
self.map.local_def_id(trait_item.id)
21912191
}).collect())
21922192
}
2193-
hir::ItemImpl(.., ref impl_items) => {
2194-
Rc::new(impl_items.iter().map(|impl_item| {
2195-
self.map.local_def_id(impl_item.id)
2193+
hir::ItemImpl(.., ref impl_item_refs) => {
2194+
Rc::new(impl_item_refs.iter().map(|impl_item_ref| {
2195+
self.map.local_def_id(impl_item_ref.id.node_id)
21962196
}).collect())
21972197
}
21982198
_ => span_bug!(item.span, "associated_item_def_ids: not impl or trait")

src/librustc_lint/builtin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,16 @@ impl LateLintPass for MissingDoc {
387387
"a trait"
388388
}
389389
hir::ItemTy(..) => "a type alias",
390-
hir::ItemImpl(.., Some(ref trait_ref), _, ref impl_items) => {
390+
hir::ItemImpl(.., Some(ref trait_ref), _, ref impl_item_refs) => {
391391
// If the trait is private, add the impl items to private_traits so they don't get
392392
// reported for missing docs.
393393
let real_trait = cx.tcx.expect_def(trait_ref.ref_id).def_id();
394394
if let Some(node_id) = cx.tcx.map.as_local_node_id(real_trait) {
395395
match cx.tcx.map.find(node_id) {
396396
Some(hir_map::NodeItem(item)) => {
397397
if item.vis == hir::Visibility::Inherited {
398-
for itm in impl_items {
399-
self.private_traits.insert(itm.id);
398+
for impl_item_ref in impl_item_refs {
399+
self.private_traits.insert(impl_item_ref.id.node_id);
400400
}
401401
}
402402
}

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,12 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
277277
.and_then(|impl_node_id| self.tcx.map.find(impl_node_id))
278278
.map(|node| {
279279
if let hir_map::NodeItem(item) = node {
280-
if let hir::ItemImpl(_, _, _, _, _, ref impl_item_ids) = item.node {
281-
span = impl_item_ids.first()
282-
.map(|&impl_item_id| {
283-
self.tcx.map.impl_item(impl_item_id)
284-
.span
285-
});
280+
if let hir::ItemImpl(.., ref impl_item_refs) = item.node {
281+
span = impl_item_refs.first()
282+
.map(|iiref| {
283+
self.tcx.map.impl_item(iiref.id)
284+
.span
285+
});
286286
}
287287
}
288288
});

0 commit comments

Comments
 (0)