Skip to content

Commit 609d0bd

Browse files
committed
dump data for prefix path segments
1 parent c2bb7ca commit 609d0bd

File tree

2 files changed

+22
-131
lines changed

2 files changed

+22
-131
lines changed

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 9 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use syntax::visit::{self, Visitor};
3838
use syntax::print::pprust::{
3939
bounds_to_string,
4040
generic_params_to_string,
41-
path_to_string,
4241
ty_to_string
4342
};
4443
use syntax::ptr::P;
@@ -218,95 +217,21 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
218217
self.dumper.compilation_opts(data);
219218
}
220219

221-
// Return all non-empty prefixes of a path.
222-
// For each prefix, we return the span for the last segment in the prefix and
223-
// a str representation of the entire prefix.
224-
fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
225-
let segments = &path.segments[if path.is_global() { 1 } else { 0 }..];
226-
227-
let mut result = Vec::with_capacity(segments.len());
228-
let mut segs = Vec::with_capacity(segments.len());
229-
230-
for (i, seg) in segments.iter().enumerate() {
231-
segs.push(seg.clone());
232-
let sub_path = ast::Path {
233-
span: seg.ident.span, // span for the last segment
234-
segments: segs,
235-
};
236-
let qualname = if i == 0 && path.is_global() {
237-
format!("::{}", path_to_string(&sub_path))
238-
} else {
239-
path_to_string(&sub_path)
240-
};
241-
result.push((seg.ident.span, qualname));
242-
segs = sub_path.segments;
243-
}
244-
245-
result
246-
}
247-
248220
fn write_sub_paths(&mut self, path: &ast::Path) {
249-
let sub_paths = self.process_path_prefixes(path);
250-
for (span, _) in sub_paths {
251-
let span = self.span_from_span(span);
252-
self.dumper.dump_ref(Ref {
253-
kind: RefKind::Mod,
254-
span,
255-
ref_id: ::null_id(),
256-
});
221+
for seg in &path.segments {
222+
if let Some(data) = self.save_ctxt.get_path_segment_data(seg) {
223+
self.dumper.dump_ref(data);
224+
}
257225
}
258226
}
259227

260228
// As write_sub_paths, but does not process the last ident in the path (assuming it
261229
// will be processed elsewhere). See note on write_sub_paths about global.
262230
fn write_sub_paths_truncated(&mut self, path: &ast::Path) {
263-
let sub_paths = self.process_path_prefixes(path);
264-
let len = sub_paths.len();
265-
if len <= 1 {
266-
return;
267-
}
268-
269-
for (span, _) in sub_paths.into_iter().take(len - 1) {
270-
let span = self.span_from_span(span);
271-
self.dumper.dump_ref(Ref {
272-
kind: RefKind::Mod,
273-
span,
274-
ref_id: ::null_id(),
275-
});
276-
}
277-
}
278-
279-
// As write_sub_paths, but expects a path of the form module_path::trait::method
280-
// Where trait could actually be a struct too.
281-
fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
282-
let sub_paths = self.process_path_prefixes(path);
283-
let len = sub_paths.len();
284-
if len <= 1 {
285-
return;
286-
}
287-
let sub_paths = &sub_paths[..(len - 1)];
288-
289-
// write the trait part of the sub-path
290-
let (ref span, _) = sub_paths[len - 2];
291-
let span = self.span_from_span(*span);
292-
self.dumper.dump_ref(Ref {
293-
kind: RefKind::Type,
294-
ref_id: ::null_id(),
295-
span,
296-
});
297-
298-
// write the other sub-paths
299-
if len <= 2 {
300-
return;
301-
}
302-
let sub_paths = &sub_paths[..len - 2];
303-
for &(ref span, _) in sub_paths {
304-
let span = self.span_from_span(*span);
305-
self.dumper.dump_ref(Ref {
306-
kind: RefKind::Mod,
307-
span,
308-
ref_id: ::null_id(),
309-
});
231+
for seg in &path.segments[..path.segments.len() - 1] {
232+
if let Some(data) = self.save_ctxt.get_path_segment_data(seg) {
233+
self.dumper.dump_ref(data);
234+
}
310235
}
311236
}
312237

@@ -876,29 +801,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
876801
}
877802
}
878803

879-
// Modules or types in the path prefix.
880-
match self.save_ctxt.get_path_def(id) {
881-
HirDef::Method(did) => {
882-
let ti = self.tcx.associated_item(did);
883-
if ti.kind == ty::AssociatedKind::Method && ti.method_has_self_argument {
884-
self.write_sub_path_trait_truncated(path);
885-
}
886-
}
887-
HirDef::Fn(..) |
888-
HirDef::Const(..) |
889-
HirDef::Static(..) |
890-
HirDef::StructCtor(..) |
891-
HirDef::VariantCtor(..) |
892-
HirDef::AssociatedConst(..) |
893-
HirDef::Local(..) |
894-
HirDef::Upvar(..) |
895-
HirDef::Struct(..) |
896-
HirDef::Union(..) |
897-
HirDef::Variant(..) |
898-
HirDef::TyAlias(..) |
899-
HirDef::AssociatedTy(..) => self.write_sub_paths_truncated(path),
900-
_ => {}
901-
}
804+
self.write_sub_paths_truncated(path);
902805
}
903806

904807
fn process_struct_lit(

src/librustc_save_analysis/lib.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -684,31 +684,28 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
684684
}
685685
}
686686

687-
pub fn get_path_data(&self, id: NodeId, path: &ast::Path) -> Option<Ref> {
687+
pub fn get_path_data(&self, _id: NodeId, path: &ast::Path) -> Option<Ref> {
688+
path.segments.last().and_then(|seg| self.get_path_segment_data(seg))
689+
}
690+
691+
pub fn get_path_segment_data(&self, path_seg: &ast::PathSegment) -> Option<Ref> {
688692
// Returns true if the path is function type sugar, e.g., `Fn(A) -> B`.
689-
fn fn_type(path: &ast::Path) -> bool {
690-
if path.segments.len() != 1 {
691-
return false;
692-
}
693-
if let Some(ref generic_args) = path.segments[0].args {
693+
fn fn_type(seg: &ast::PathSegment) -> bool {
694+
if let Some(ref generic_args) = seg.args {
694695
if let ast::GenericArgs::Parenthesized(_) = **generic_args {
695696
return true;
696697
}
697698
}
698699
false
699700
}
700701

701-
if path.segments.is_empty() {
702-
return None;
703-
}
702+
let def = self.get_path_def(path_seg.id);
703+
let span = path_seg.ident.span;
704+
filter!(self.span_utils, span);
705+
let span = self.span_from_span(span);
704706

705-
let def = self.get_path_def(id);
706-
let last_seg = &path.segments[path.segments.len() - 1];
707-
let sub_span = last_seg.ident.span;
708-
filter!(self.span_utils, sub_span);
709707
match def {
710708
HirDef::Upvar(id, ..) | HirDef::Local(id) => {
711-
let span = self.span_from_span(sub_span);
712709
Some(Ref {
713710
kind: RefKind::Variable,
714711
span,
@@ -719,20 +716,16 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
719716
HirDef::Const(..) |
720717
HirDef::AssociatedConst(..) |
721718
HirDef::VariantCtor(..) => {
722-
let span = self.span_from_span(sub_span);
723719
Some(Ref {
724720
kind: RefKind::Variable,
725721
span,
726722
ref_id: id_from_def_id(def.def_id()),
727723
})
728724
}
729-
HirDef::Trait(def_id) if fn_type(path) => {
730-
// Function type bounds are desugared in the parser, so we have to
731-
// special case them here.
732-
let fn_span = path.segments.first().unwrap().ident.span;
725+
HirDef::Trait(def_id) if fn_type(path_seg) => {
733726
Some(Ref {
734727
kind: RefKind::Type,
735-
span: self.span_from_span(fn_span),
728+
span,
736729
ref_id: id_from_def_id(def_id),
737730
})
738731
}
@@ -748,7 +741,6 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
748741
HirDef::Trait(def_id) |
749742
HirDef::Existential(def_id) |
750743
HirDef::TyParam(def_id) => {
751-
let span = self.span_from_span(sub_span);
752744
Some(Ref {
753745
kind: RefKind::Type,
754746
span,
@@ -759,7 +751,6 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
759751
// This is a reference to a tuple struct where the def_id points
760752
// to an invisible constructor function. That is not a very useful
761753
// def, so adjust to point to the tuple struct itself.
762-
let span = self.span_from_span(sub_span);
763754
let parent_def_id = self.tcx.parent_def_id(def_id).unwrap();
764755
Some(Ref {
765756
kind: RefKind::Type,
@@ -778,23 +769,20 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
778769
} else {
779770
None
780771
};
781-
let span = self.span_from_span(sub_span);
782772
Some(Ref {
783773
kind: RefKind::Function,
784774
span,
785775
ref_id: id_from_def_id(def_id.unwrap_or(decl_id)),
786776
})
787777
}
788778
HirDef::Fn(def_id) => {
789-
let span = self.span_from_span(sub_span);
790779
Some(Ref {
791780
kind: RefKind::Function,
792781
span,
793782
ref_id: id_from_def_id(def_id),
794783
})
795784
}
796785
HirDef::Mod(def_id) => {
797-
let span = self.span_from_span(sub_span);
798786
Some(Ref {
799787
kind: RefKind::Mod,
800788
span,

0 commit comments

Comments
 (0)