Skip to content

Commit fcdb4de

Browse files
committed
rustc: track fields in the HIR map.
1 parent 36d33d6 commit fcdb4de

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/librustc/hir/map/collector.rs

+7
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,11 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
242242
fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
243243
self.insert_entry(macro_def.id, NotPresent);
244244
}
245+
246+
fn visit_struct_field(&mut self, field: &'ast StructField) {
247+
self.insert(field.id, NodeField(field));
248+
self.with_parent(field.id, |this| {
249+
intravisit::walk_struct_field(this, field);
250+
});
251+
}
245252
}

src/librustc/hir/map/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub enum Node<'ast> {
4646
NodeTraitItem(&'ast TraitItem),
4747
NodeImplItem(&'ast ImplItem),
4848
NodeVariant(&'ast Variant),
49+
NodeField(&'ast StructField),
4950
NodeExpr(&'ast Expr),
5051
NodeStmt(&'ast Stmt),
5152
NodeTy(&'ast Ty),
@@ -75,6 +76,7 @@ pub enum MapEntry<'ast> {
7576
EntryTraitItem(NodeId, &'ast TraitItem),
7677
EntryImplItem(NodeId, &'ast ImplItem),
7778
EntryVariant(NodeId, &'ast Variant),
79+
EntryField(NodeId, &'ast StructField),
7880
EntryExpr(NodeId, &'ast Expr),
7981
EntryStmt(NodeId, &'ast Stmt),
8082
EntryTy(NodeId, &'ast Ty),
@@ -106,6 +108,7 @@ impl<'ast> MapEntry<'ast> {
106108
NodeTraitItem(n) => EntryTraitItem(p, n),
107109
NodeImplItem(n) => EntryImplItem(p, n),
108110
NodeVariant(n) => EntryVariant(p, n),
111+
NodeField(n) => EntryField(p, n),
109112
NodeExpr(n) => EntryExpr(p, n),
110113
NodeStmt(n) => EntryStmt(p, n),
111114
NodeTy(n) => EntryTy(p, n),
@@ -127,6 +130,7 @@ impl<'ast> MapEntry<'ast> {
127130
EntryTraitItem(id, _) => id,
128131
EntryImplItem(id, _) => id,
129132
EntryVariant(id, _) => id,
133+
EntryField(id, _) => id,
130134
EntryExpr(id, _) => id,
131135
EntryStmt(id, _) => id,
132136
EntryTy(id, _) => id,
@@ -152,6 +156,7 @@ impl<'ast> MapEntry<'ast> {
152156
EntryTraitItem(_, n) => NodeTraitItem(n),
153157
EntryImplItem(_, n) => NodeImplItem(n),
154158
EntryVariant(_, n) => NodeVariant(n),
159+
EntryField(_, n) => NodeField(n),
155160
EntryExpr(_, n) => NodeExpr(n),
156161
EntryStmt(_, n) => NodeStmt(n),
157162
EntryTy(_, n) => NodeTy(n),
@@ -265,6 +270,7 @@ impl<'ast> Map<'ast> {
265270
EntryForeignItem(p, _) |
266271
EntryTraitItem(p, _) |
267272
EntryVariant(p, _) |
273+
EntryField(p, _) |
268274
EntryExpr(p, _) |
269275
EntryStmt(p, _) |
270276
EntryTy(p, _) |
@@ -308,6 +314,7 @@ impl<'ast> Map<'ast> {
308314
EntryTraitItem(p, _) |
309315
EntryImplItem(p, _) |
310316
EntryVariant(p, _) |
317+
EntryField(p, _) |
311318
EntryExpr(p, _) |
312319
EntryStmt(p, _) |
313320
EntryTy(p, _) |
@@ -656,6 +663,7 @@ impl<'ast> Map<'ast> {
656663
NodeImplItem(ii) => ii.name,
657664
NodeTraitItem(ti) => ti.name,
658665
NodeVariant(v) => v.node.name,
666+
NodeField(f) => f.name,
659667
NodeLifetime(lt) => lt.name,
660668
NodeTyParam(tp) => tp.name,
661669
NodeLocal(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
@@ -674,6 +682,7 @@ impl<'ast> Map<'ast> {
674682
Some(NodeTraitItem(ref ti)) => Some(&ti.attrs[..]),
675683
Some(NodeImplItem(ref ii)) => Some(&ii.attrs[..]),
676684
Some(NodeVariant(ref v)) => Some(&v.node.attrs[..]),
685+
Some(NodeField(ref f)) => Some(&f.attrs[..]),
677686
Some(NodeExpr(ref e)) => Some(&*e.attrs),
678687
Some(NodeStmt(ref s)) => Some(s.node.attrs()),
679688
// unit/tuple structs take the attributes straight from
@@ -710,6 +719,7 @@ impl<'ast> Map<'ast> {
710719
Some(NodeTraitItem(trait_method)) => trait_method.span,
711720
Some(NodeImplItem(ref impl_item)) => impl_item.span,
712721
Some(NodeVariant(variant)) => variant.span,
722+
Some(NodeField(field)) => field.span,
713723
Some(NodeExpr(expr)) => expr.span,
714724
Some(NodeStmt(stmt)) => stmt.span,
715725
Some(NodeTy(ty)) => ty.span,
@@ -831,6 +841,7 @@ impl<'a, 'ast> Iterator for NodesMatchingSuffix<'a, 'ast> {
831841
Some(EntryTraitItem(_, n)) => n.name(),
832842
Some(EntryImplItem(_, n)) => n.name(),
833843
Some(EntryVariant(_, n)) => n.name(),
844+
Some(EntryField(_, n)) => n.name(),
834845
_ => continue,
835846
};
836847
if self.matches_names(self.map.get_parent(idx), name) {
@@ -849,6 +860,7 @@ impl<T:Named> Named for Spanned<T> { fn name(&self) -> Name { self.node.name() }
849860
impl Named for Item { fn name(&self) -> Name { self.name } }
850861
impl Named for ForeignItem { fn name(&self) -> Name { self.name } }
851862
impl Named for Variant_ { fn name(&self) -> Name { self.name } }
863+
impl Named for StructField { fn name(&self) -> Name { self.name } }
852864
impl Named for TraitItem { fn name(&self) -> Name { self.name } }
853865
impl Named for ImplItem { fn name(&self) -> Name { self.name } }
854866

@@ -940,6 +952,7 @@ impl<'a> NodePrinter for pprust::State<'a> {
940952
NodeLifetime(a) => self.print_lifetime(&a),
941953
NodeVisibility(a) => self.print_visibility(&a),
942954
NodeTyParam(_) => bug!("cannot print TyParam"),
955+
NodeField(_) => bug!("cannot print StructField"),
943956
// these cases do not carry enough information in the
944957
// ast_map to reconstruct their full structure for pretty
945958
// printing.
@@ -1019,6 +1032,11 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10191032
variant.node.name,
10201033
path_str(), id_str)
10211034
}
1035+
Some(NodeField(ref field)) => {
1036+
format!("field {} in {}{}",
1037+
field.name,
1038+
path_str(), id_str)
1039+
}
10221040
Some(NodeExpr(ref expr)) => {
10231041
format!("expr {}{}", pprust::expr_to_string(&expr), id_str)
10241042
}

src/librustc/middle/reachable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
299299
ast_map::NodeForeignItem(_) |
300300
ast_map::NodeVariant(_) |
301301
ast_map::NodeStructCtor(_) |
302+
ast_map::NodeField(_) |
302303
ast_map::NodeTy(_) => {}
303304
_ => {
304305
bug!("found unexpected thingy in worklist: {}",

0 commit comments

Comments
 (0)