Skip to content

Commit dcbd418

Browse files
committed
save-analysis: handle absolute paths properly
1 parent 1174550 commit dcbd418

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

src/librustc_trans/save/mod.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,33 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
124124
let mut result: Vec<(Span, String)> = vec!();
125125

126126
let mut segs = vec!();
127-
for (seg, span) in path.segments.iter().zip(spans.iter()) {
127+
for (i, (seg, span)) in path.segments.iter().zip(spans.iter()).enumerate() {
128128
segs.push(seg.clone());
129129
let sub_path = ast::Path{span: *span, // span for the last segment
130130
global: path.global,
131131
segments: segs};
132-
let qualname = path_to_string(&sub_path);
132+
let qualname = if i == 0 && path.global {
133+
let mut result = "::".to_string();
134+
result.push_str(&path_to_string(&sub_path)[]);
135+
result
136+
} else {
137+
path_to_string(&sub_path)
138+
};
133139
result.push((*span, qualname));
134140
segs = sub_path.segments;
135141
}
136142

137143
result
138144
}
139145

140-
fn write_sub_paths(&mut self, path: &ast::Path) {
146+
fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
141147
let sub_paths = self.process_path_prefixes(path);
142-
for &(ref span, ref qualname) in sub_paths.iter() {
148+
for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
149+
let qualname = if i == 0 && global && !path.global {
150+
format!("::{}", qualname)
151+
} else {
152+
qualname.clone()
153+
};
143154
self.fmt.sub_mod_ref_str(path.span,
144155
*span,
145156
&qualname[],
@@ -149,15 +160,20 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
149160

150161
// As write_sub_paths, but does not process the last ident in the path (assuming it
151162
// will be processed elsewhere).
152-
fn write_sub_paths_truncated(&mut self, path: &ast::Path) {
163+
fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
153164
let sub_paths = self.process_path_prefixes(path);
154165
let len = sub_paths.len();
155166
if len <= 1 {
156167
return;
157168
}
158169

159-
let sub_paths = &sub_paths[.. (len-1)];
160-
for &(ref span, ref qualname) in sub_paths.iter() {
170+
let sub_paths = sub_paths[..len-1];
171+
for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
172+
let qualname = if i == 0 && global && !path.global {
173+
format!("::{}", qualname)
174+
} else {
175+
qualname.clone()
176+
};
161177
self.fmt.sub_mod_ref_str(path.span,
162178
*span,
163179
&qualname[],
@@ -276,8 +292,8 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
276292
let mut scope_id;
277293
// The qualname for a method is the trait name or name of the struct in an impl in
278294
// which the method is declared in followed by the method's name.
279-
let mut qualname = match ty::impl_of_method(&self.analysis.ty_cx,
280-
ast_util::local_def(method.id)) {
295+
let qualname = match ty::impl_of_method(&self.analysis.ty_cx,
296+
ast_util::local_def(method.id)) {
281297
Some(impl_id) => match self.analysis.ty_cx.map.get(impl_id.node) {
282298
NodeItem(item) => {
283299
scope_id = item.id;
@@ -339,7 +355,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
339355
},
340356
};
341357

342-
qualname.push_str(get_ident(method.pe_ident()).get());
358+
let qualname = format!("::{}{}", qualname, get_ident(method.pe_ident()).get());
343359
let qualname = &qualname[];
344360

345361
// record the decl for this def (if it has one)
@@ -434,7 +450,8 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
434450
}
435451

436452
// Dump generic params bindings, then visit_generics
437-
fn process_generic_params(&mut self, generics:&ast::Generics,
453+
fn process_generic_params(&mut self,
454+
generics:&ast::Generics,
438455
full_span: Span,
439456
prefix: &str,
440457
id: NodeId) {
@@ -464,7 +481,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
464481
decl: &ast::FnDecl,
465482
ty_params: &ast::Generics,
466483
body: &ast::Block) {
467-
let qualname = self.analysis.ty_cx.map.path_to_string(item.id);
484+
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
468485

469486
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Fn);
470487
self.fmt.fn_str(item.span,
@@ -496,7 +513,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
496513
mt: ast::Mutability,
497514
expr: &ast::Expr)
498515
{
499-
let qualname = self.analysis.ty_cx.map.path_to_string(item.id);
516+
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
500517

501518
// If the variable is immutable, save the initialising expression.
502519
let value = match mt {
@@ -524,7 +541,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
524541
typ: &ast::Ty,
525542
expr: &ast::Expr)
526543
{
527-
let qualname = self.analysis.ty_cx.map.path_to_string(item.id);
544+
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
528545

529546
let sub_span = self.span.sub_span_after_keyword(item.span,
530547
keywords::Const);
@@ -546,7 +563,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
546563
item: &ast::Item,
547564
def: &ast::StructDef,
548565
ty_params: &ast::Generics) {
549-
let qualname = self.analysis.ty_cx.map.path_to_string(item.id);
566+
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
550567

551568
let ctor_id = match def.ctor_id {
552569
Some(node_id) => node_id,
@@ -575,7 +592,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
575592
item: &ast::Item,
576593
enum_definition: &ast::EnumDef,
577594
ty_params: &ast::Generics) {
578-
let enum_name = self.analysis.ty_cx.map.path_to_string(item.id);
595+
let enum_name = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
579596
let val = self.span.snippet(item.span);
580597
match self.span.sub_span_after_keyword(item.span, keywords::Enum) {
581598
Some(sub_span) => self.fmt.enum_str(item.span,
@@ -701,7 +718,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
701718
generics: &ast::Generics,
702719
trait_refs: &OwnedSlice<ast::TyParamBound>,
703720
methods: &Vec<ast::TraitItem>) {
704-
let qualname = self.analysis.ty_cx.map.path_to_string(item.id);
721+
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
705722
let val = self.span.snippet(item.span);
706723
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
707724
self.fmt.trait_str(item.span,
@@ -750,7 +767,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
750767
fn process_mod(&mut self,
751768
item: &ast::Item, // The module in question, represented as an item.
752769
m: &ast::Mod) {
753-
let qualname = self.analysis.ty_cx.map.path_to_string(item.id);
770+
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
754771

755772
let cm = self.sess.codemap();
756773
let filename = cm.span_to_filename(m.inner);
@@ -855,7 +872,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
855872
def::DefConst(..) |
856873
def::DefStruct(_) |
857874
def::DefVariant(..) |
858-
def::DefFn(..) => self.write_sub_paths_truncated(path),
875+
def::DefFn(..) => self.write_sub_paths_truncated(path, false),
859876
_ => {},
860877
}
861878
}
@@ -883,7 +900,7 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
883900
None => ()
884901
}
885902

886-
self.write_sub_paths_truncated(path);
903+
self.write_sub_paths_truncated(path, false);
887904

888905
for field in fields.iter() {
889906
match struct_def {
@@ -1069,7 +1086,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
10691086
mod_id,
10701087
get_ident(ident).get(),
10711088
self.cur_scope);
1072-
self.write_sub_paths_truncated(path);
1089+
self.write_sub_paths_truncated(path, true);
10731090
}
10741091
ast::ViewPathGlob(ref path) => {
10751092
// Make a comma-separated list of names of imported modules.
@@ -1092,7 +1109,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
10921109
item.id,
10931110
name_string.as_slice(),
10941111
self.cur_scope);
1095-
self.write_sub_paths(path);
1112+
self.write_sub_paths(path, true);
10961113
}
10971114
ast::ViewPathList(ref path, ref list) => {
10981115
for plid in list.iter() {
@@ -1116,7 +1133,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
11161133
}
11171134
}
11181135

1119-
self.write_sub_paths(path);
1136+
self.write_sub_paths(path, true);
11201137
}
11211138
}
11221139
}
@@ -1163,7 +1180,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
11631180
self.process_trait(item, generics, trait_refs, methods),
11641181
ast::ItemMod(ref m) => self.process_mod(item, m),
11651182
ast::ItemTy(ref ty, ref ty_params) => {
1166-
let qualname = self.analysis.ty_cx.map.path_to_string(item.id);
1183+
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
11671184
let value = ty_to_string(&**ty);
11681185
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
11691186
self.fmt.typedef_str(item.span,
@@ -1223,9 +1240,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
12231240
ast_util::local_def(method_type.id)) {
12241241
Some(def_id) => {
12251242
scope_id = def_id.node;
1226-
let mut s = ty::item_path_str(&self.analysis.ty_cx, def_id);
1227-
s.push_str("::");
1228-
s
1243+
format!("::{}::", ty::item_path_str(&self.analysis.ty_cx, def_id))
12291244
},
12301245
None => {
12311246
self.sess.span_bug(method_type.span,
@@ -1282,7 +1297,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
12821297
None => ()
12831298
}
12841299

1285-
self.write_sub_paths_truncated(path);
1300+
self.write_sub_paths_truncated(path, false);
12861301

12871302
visit::walk_path(self, path);
12881303
},

0 commit comments

Comments
 (0)