Skip to content

Commit 51ace54

Browse files
committed
auto merge of #11254 : huonw/rust/impl-names, r=brson
The resulting symbol names aren't very pretty at all: trait Trait { fn method(&self); } impl<'a> Trait for ~[(&'a int, fn())] { fn method(&self) {} } gives Trait$$UP$$VEC$$TUP_2$$BP$int$$FN$$::method::...hash...::v0.0 However, at least it contain some reference to the Self type, unlike `Trait$__extensions__::method:...`, which is what the symbol name used to be for anything other than `impl Trait for foo::bar::Baz` (which became, and still becomes, `Trait$Baz::method`).
2 parents 2897549 + 8f26d0b commit 51ace54

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

src/librustc/metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
11401140
let impl_vtables = ty::lookup_impl_vtables(tcx, def_id);
11411141
encode_impl_vtables(ebml_w, ecx, &impl_vtables);
11421142
}
1143-
let elt = ast_map::impl_pretty_name(opt_trait, ty, item.ident);
1143+
let elt = ast_map::impl_pretty_name(opt_trait, ty);
11441144
encode_path(ecx, ebml_w, path, elt);
11451145
ebml_w.end_tag();
11461146

src/libsyntax/ast_map.rs

+61-16
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,71 @@ pub fn path_elt_to_str(pe: path_elt, itr: @ident_interner) -> ~str {
8282
}
8383
}
8484

85-
pub fn impl_pretty_name(trait_ref: &Option<trait_ref>,
86-
ty: &Ty, default: Ident) -> path_elt {
87-
let itr = get_ident_interner();
88-
let ty_ident = match ty.node {
89-
ty_path(ref path, _, _) => path.segments.last().identifier,
90-
_ => default
85+
/// write a "pretty" version of `ty` to `out`. This is designed so
86+
/// that symbols of `impl`'d methods give some hint of where they came
87+
/// from, even if it's hard to read (previously they would all just be
88+
/// listed as `__extensions__::method_name::hash`, with no indication
89+
/// of the type).
90+
// XXX: these dollar signs and the names in general are actually a
91+
// relic of $ being one of the very few valid symbol names on
92+
// unix. These kinds of details shouldn't be exposed way up here
93+
// in the ast.
94+
fn pretty_ty(ty: &Ty, itr: @ident_interner, out: &mut ~str) {
95+
let (prefix, subty) = match ty.node {
96+
ty_uniq(ty) => ("$UP$", &*ty),
97+
ty_box(mt { ty, .. }) => ("$SP$", &*ty),
98+
ty_ptr(mt { ty, mutbl }) => (if mutbl == MutMutable {"$RPmut$"} else {"$RP$"},
99+
&*ty),
100+
ty_rptr(_, mt { ty, mutbl }) => (if mutbl == MutMutable {"$BPmut$"} else {"$BP$"},
101+
&*ty),
102+
103+
ty_vec(ty) => ("$VEC$", &*ty),
104+
ty_fixed_length_vec(ty, _) => ("$FIXEDVEC$", &*ty),
105+
106+
// these can't be represented as <prefix><contained ty>, so
107+
// need custom handling.
108+
ty_nil => { out.push_str("$NIL$"); return }
109+
ty_path(ref path, _, _) => {
110+
out.push_str(itr.get(path.segments.last().identifier.name));
111+
return
112+
}
113+
ty_tup(ref tys) => {
114+
out.push_str(format!("$TUP_{}$", tys.len()));
115+
for subty in tys.iter() {
116+
pretty_ty(*subty, itr, out);
117+
out.push_char('$');
118+
}
119+
return
120+
}
121+
122+
// meh, better than nothing.
123+
ty_bot => { out.push_str("$BOT$"); return }
124+
ty_closure(..) => { out.push_str("$CLOSURE$"); return }
125+
ty_bare_fn(..) => { out.push_str("$FN$"); return }
126+
ty_typeof(..) => { out.push_str("$TYPEOF$"); return }
127+
ty_infer(..) => { out.push_str("$INFER$"); return }
128+
91129
};
130+
131+
out.push_str(prefix);
132+
pretty_ty(subty, itr, out);
133+
}
134+
135+
pub fn impl_pretty_name(trait_ref: &Option<trait_ref>, ty: &Ty) -> path_elt {
136+
let itr = get_ident_interner();
137+
92138
let hash = (trait_ref, ty).hash();
139+
let mut pretty;
93140
match *trait_ref {
94-
None => path_pretty_name(ty_ident, hash),
141+
None => pretty = ~"",
95142
Some(ref trait_ref) => {
96-
// XXX: this dollar sign is actually a relic of being one of the
97-
// very few valid symbol names on unix. These kinds of
98-
// details shouldn't be exposed way up here in the ast.
99-
let s = format!("{}${}",
100-
itr.get(trait_ref.path.segments.last().identifier.name),
101-
itr.get(ty_ident.name));
102-
path_pretty_name(Ident::new(itr.gensym(s)), hash)
143+
pretty = itr.get(trait_ref.path.segments.last().identifier.name).to_owned();
144+
pretty.push_char('$');
103145
}
104-
}
146+
};
147+
pretty_ty(ty, itr, &mut pretty);
148+
149+
path_pretty_name(Ident::new(itr.gensym(pretty)), hash)
105150
}
106151

107152
#[deriving(Clone)]
@@ -265,7 +310,7 @@ impl Visitor<()> for Ctx {
265310
// Right now the ident on impls is __extensions__ which isn't
266311
// very pretty when debugging, so attempt to select a better
267312
// name to use.
268-
let elt = impl_pretty_name(maybe_trait, ty, i.ident);
313+
let elt = impl_pretty_name(maybe_trait, ty);
269314

270315
let impl_did = ast_util::local_def(i.id);
271316
for m in ms.iter() {

src/test/compile-fail/ambig_impl_unify.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ trait foo {
1313
}
1414

1515
impl foo for ~[uint] {
16-
fn foo(&self) -> int {1} //~ NOTE candidate #1 is `foo$__extensions__::foo`
16+
fn foo(&self) -> int {1} //~ NOTE candidate #1 is `foo$$UP$$VEC$uint::foo`
1717
}
1818

1919
impl foo for ~[int] {
20-
fn foo(&self) -> int {2} //~ NOTE candidate #2 is `foo$__extensions__::foo`
20+
fn foo(&self) -> int {2} //~ NOTE candidate #2 is `foo$$UP$$VEC$int::foo`
2121
}
2222

2323
fn main() {

0 commit comments

Comments
 (0)