Skip to content

Commit 5719ab3

Browse files
committed
dump refs for path segments in save-analysis
Requires adding path segments to the hir map
1 parent e65e8a0 commit 5719ab3

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
@@ -1857,6 +1857,7 @@ impl<'a> LoweringContext<'a> {
18571857
let def = self.expect_full_def(segment.id);
18581858
hir::PathSegment::new(
18591859
segment.ident,
1860+
Some(segment.id),
18601861
Some(def),
18611862
generic_args,
18621863
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
@@ -337,6 +337,7 @@ impl fmt::Display for Path {
337337
pub struct PathSegment {
338338
/// The identifier portion of this path segment.
339339
pub ident: Ident,
340+
pub id: Option<NodeId>,
340341
pub def: Option<Def>,
341342

342343
/// Type/lifetime parameters attached to this path. They come in
@@ -358,15 +359,23 @@ impl PathSegment {
358359
pub fn from_ident(ident: Ident) -> PathSegment {
359360
PathSegment {
360361
ident,
362+
id: None,
361363
def: None,
362364
infer_types: true,
363365
args: None,
364366
}
365367
}
366368

367-
pub fn new(ident: Ident, def: Option<Def>, args: GenericArgs, infer_types: bool) -> Self {
369+
pub fn new(
370+
ident: Ident,
371+
id: Option<NodeId>,
372+
def: Option<Def>,
373+
args: GenericArgs,
374+
infer_types: bool,
375+
) -> Self {
368376
PathSegment {
369377
ident,
378+
id,
370379
def,
371380
infer_types,
372381
args: if args.is_empty() {
@@ -2501,6 +2510,7 @@ pub enum Node<'hir> {
25012510
AnonConst(&'hir AnonConst),
25022511
Expr(&'hir Expr),
25032512
Stmt(&'hir Stmt),
2513+
PathSegment(&'hir PathSegment),
25042514
Ty(&'hir Ty),
25052515
TraitRef(&'hir TraitRef),
25062516
Binding(&'hir Pat),

src/librustc/ich/impls_hir.rs

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

173173
impl_stable_hash_for!(struct hir::PathSegment {
174174
ident -> (ident.name),
175+
id,
175176
def,
176177
infer_types,
177178
args

src/librustc_resolve/build_reduced_graph.rs

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

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

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

src/librustc_resolve/lib.rs

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

36363636
for (i, &(ident, id)) in path.iter().enumerate() {
36373637
debug!("resolve_path ident {} {:?}", i, ident);
3638+
36383639
let is_last = i == path.len() - 1;
36393640
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
36403641
let name = ident.name;
@@ -3734,10 +3735,12 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
37343735
let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(def);
37353736
if let Some(next_module) = binding.module() {
37363737
module = Some(ModuleOrUniformRoot::Module(next_module));
3737-
if !is_last && record_used {
3738+
if record_used {
37383739
if let Some(id) = id {
3739-
assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
3740-
self.record_def(id, PathResolution::new(def));
3740+
if !self.def_map.contains_key(&id) {
3741+
assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
3742+
self.record_def(id, PathResolution::new(def));
3743+
}
37413744
}
37423745
}
37433746
} 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)