Skip to content

Commit c480386

Browse files
committed
dump refs for path segments in save-analysis
Requires adding path segments to the hir map
1 parent 609d0bd commit c480386

File tree

9 files changed

+51
-20
lines changed

9 files changed

+51
-20
lines changed

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,7 @@ impl<'a> LoweringContext<'a> {
18611861
let def = self.expect_full_def(segment.id);
18621862
hir::PathSegment::new(
18631863
segment.ident,
1864+
Some(segment.id),
18641865
Some(def),
18651866
generic_args,
18661867
infer_types,

src/librustc/hir/map/collector.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,13 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
392392
});
393393
}
394394

395+
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment) {
396+
if let Some(id) = path_segment.id {
397+
self.insert(id, Node::PathSegment(path_segment));
398+
}
399+
intravisit::walk_path_segment(self, path_span, path_segment);
400+
}
401+
395402
fn visit_ty(&mut self, ty: &'hir Ty) {
396403
self.insert(ty.id, Node::Ty(ty));
397404

src/librustc/hir/map/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'hir> Map<'hir> {
204204
if let Some(entry) = self.map[id.as_usize()] {
205205
self.dep_graph.read_index(entry.dep_node);
206206
} else {
207-
bug!("called `HirMap::read()` with invalid `NodeId`")
207+
bug!("called `HirMap::read()` with invalid `NodeId`: {:?}", id)
208208
}
209209
}
210210

@@ -344,6 +344,7 @@ impl<'hir> Map<'hir> {
344344
Node::AnonConst(_) |
345345
Node::Expr(_) |
346346
Node::Stmt(_) |
347+
Node::PathSegment(_) |
347348
Node::Ty(_) |
348349
Node::TraitRef(_) |
349350
Node::Pat(_) |
@@ -884,6 +885,7 @@ impl<'hir> Map<'hir> {
884885
Some(Node::AnonConst(constant)) => self.body(constant.body).value.span,
885886
Some(Node::Expr(expr)) => expr.span,
886887
Some(Node::Stmt(stmt)) => stmt.span,
888+
Some(Node::PathSegment(seg)) => seg.ident.span,
887889
Some(Node::Ty(ty)) => ty.span,
888890
Some(Node::TraitRef(tr)) => tr.path.span,
889891
Some(Node::Binding(pat)) => pat.span,
@@ -1098,6 +1100,7 @@ impl<'a> print::State<'a> {
10981100
Node::AnonConst(a) => self.print_anon_const(&a),
10991101
Node::Expr(a) => self.print_expr(&a),
11001102
Node::Stmt(a) => self.print_stmt(&a),
1103+
Node::PathSegment(_) => bug!("cannot print PathSegment"),
11011104
Node::Ty(a) => self.print_type(&a),
11021105
Node::TraitRef(a) => self.print_trait_ref(&a),
11031106
Node::Binding(a) |
@@ -1215,6 +1218,9 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
12151218
Some(Node::Stmt(_)) => {
12161219
format!("stmt {}{}", map.node_to_pretty_string(id), id_str)
12171220
}
1221+
Some(Node::PathSegment(_)) => {
1222+
format!("path segment {}{}", map.node_to_pretty_string(id), id_str)
1223+
}
12181224
Some(Node::Ty(_)) => {
12191225
format!("type {}{}", map.node_to_pretty_string(id), id_str)
12201226
}

src/librustc/hir/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ impl fmt::Display for Path {
347347
pub struct PathSegment {
348348
/// The identifier portion of this path segment.
349349
pub ident: Ident,
350+
pub id: Option<NodeId>,
350351
pub def: Option<Def>,
351352

352353
/// Type/lifetime parameters attached to this path. They come in
@@ -368,15 +369,23 @@ impl PathSegment {
368369
pub fn from_ident(ident: Ident) -> PathSegment {
369370
PathSegment {
370371
ident,
372+
id: None,
371373
def: None,
372374
infer_types: true,
373375
args: None,
374376
}
375377
}
376378

377-
pub fn new(ident: Ident, def: Option<Def>, args: GenericArgs, infer_types: bool) -> Self {
379+
pub fn new(
380+
ident: Ident,
381+
id: Option<NodeId>,
382+
def: Option<Def>,
383+
args: GenericArgs,
384+
infer_types: bool,
385+
) -> Self {
378386
PathSegment {
379387
ident,
388+
id,
380389
def,
381390
infer_types,
382391
args: if args.is_empty() {
@@ -2514,6 +2523,7 @@ pub enum Node<'hir> {
25142523
AnonConst(&'hir AnonConst),
25152524
Expr(&'hir Expr),
25162525
Stmt(&'hir Stmt),
2526+
PathSegment(&'hir PathSegment),
25172527
Ty(&'hir Ty),
25182528
TraitRef(&'hir TraitRef),
25192529
Binding(&'hir Pat),

src/librustc/ich/impls_hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ impl_stable_hash_for!(struct hir::Path {
174174

175175
impl_stable_hash_for!(struct hir::PathSegment {
176176
ident -> (ident.name),
177+
id,
177178
def,
178179
infer_types,
179180
args

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,18 +392,18 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
392392
e.emit();
393393
}
394394

395-
for &(ref tree, id) in items {
396-
let prefix = ast::Path {
397-
segments: module_path.iter()
398-
.map(|ident| {
399-
let mut seg = ast::PathSegment::from_ident(ident.0);
400-
seg.id = self.session.next_node_id();
401-
seg
402-
})
403-
.collect(),
404-
span: path.span,
405-
};
395+
let prefix = ast::Path {
396+
segments: module_path.into_iter()
397+
.map(|(ident, id)| {
398+
let mut seg = ast::PathSegment::from_ident(ident);
399+
seg.id = id.expect("Missing node id");
400+
seg
401+
})
402+
.collect(),
403+
span: path.span,
404+
};
406405

406+
for &(ref tree, id) in items {
407407
self.build_reduced_graph_for_use_tree(
408408
root_use_tree,
409409
root_id,

src/librustc_resolve/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,6 +3614,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
36143614

36153615
for (i, &(ident, id)) in path.iter().enumerate() {
36163616
debug!("resolve_path ident {} {:?}", i, ident);
3617+
36173618
let is_last = i == path.len() - 1;
36183619
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
36193620
let name = ident.name;
@@ -3713,10 +3714,12 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
37133714
let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(def);
37143715
if let Some(next_module) = binding.module() {
37153716
module = Some(ModuleOrUniformRoot::Module(next_module));
3716-
if !is_last && record_used {
3717+
if record_used {
37173718
if let Some(id) = id {
3718-
assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
3719-
self.record_def(id, PathResolution::new(def));
3719+
if !self.def_map.contains_key(&id) {
3720+
assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
3721+
self.record_def(id, PathResolution::new(def));
3722+
}
37203723
}
37213724
}
37223725
} else if def == Def::ToolMod && i + 1 != path.len() {

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,9 +812,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
812812
variant: &'l ty::VariantDef,
813813
base: &'l Option<P<ast::Expr>>,
814814
) {
815-
self.write_sub_paths_truncated(path);
816-
817815
if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
816+
self.write_sub_paths_truncated(path);
818817
down_cast_data!(struct_lit_data, RefData, ex.span);
819818
if !generated_code(ex.span) {
820819
self.dumper.dump_ref(struct_lit_data);
@@ -1232,8 +1231,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
12321231
value: String::new(),
12331232
parent,
12341233
});
1234+
self.write_sub_paths_truncated(&path);
12351235
}
1236-
self.write_sub_paths_truncated(&path);
12371236
}
12381237
ast::UseTreeKind::Glob => {
12391238
let path = ast::Path {
@@ -1268,8 +1267,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
12681267
value: names.join(", "),
12691268
parent,
12701269
});
1270+
self.write_sub_paths(&path);
12711271
}
1272-
self.write_sub_paths(&path);
12731272
}
12741273
ast::UseTreeKind::Nested(ref nested_items) => {
12751274
let prefix = ast::Path {

src/librustc_save_analysis/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
632632
Node::Visibility(&Spanned {
633633
node: hir::VisibilityKind::Restricted { ref path, .. }, .. }) => path.def,
634634

635+
Node::PathSegment(seg) => match seg.def {
636+
Some(def) => def,
637+
None => HirDef::Err,
638+
},
635639
Node::Expr(&hir::Expr {
636640
node: hir::ExprKind::Struct(ref qpath, ..),
637641
..

0 commit comments

Comments
 (0)