In ty::impl_traits, treat structs properly

Treat structs just like impls: use their associated list of
trait refs to get the list of traits that one of them implements.
I don't understand what was happening before, but it was wrong.

Closes #2936
This commit is contained in:
Tim Chevalier 2012-08-28 19:49:36 -07:00
parent a19dce6c16
commit a70e37b214
2 changed files with 24 additions and 15 deletions

View File

@ -2826,22 +2826,11 @@ fn impl_traits(cx: ctxt, id: ast::def_id) -> ~[t] {
node_id_to_type(cx, trait_ref.ref_id)
}
}
Some(ast_map::node_item(@{node: ast::item_class(*),
Some(ast_map::node_item(@{node: ast::item_class(sd,_),
_},_)) => {
match cx.def_map.find(id.node) {
Some(def_ty(trait_id)) => {
// XXX: Doesn't work cross-crate.
debug!("(impl_traits) found trait id %?", trait_id);
~[node_id_to_type(cx, trait_id.node)]
}
Some(x) => {
cx.sess.bug(fmt!("impl_traits: trait ref is in trait map \
but is bound to %?", x));
}
None => {
~[]
}
}
do vec::map(sd.traits) |trait_ref| {
node_id_to_type(cx, trait_ref.ref_id)
}
}
_ => ~[]
}

View File

@ -0,0 +1,20 @@
trait bar<T> {
fn get_bar() -> T;
}
fn foo<T, U: bar<T>>(b: U) -> T {
b.get_bar()
}
struct cbar : bar<int> {
x: int;
new(x: int) { self.x = x; }
fn get_bar() -> int {
self.x
}
}
fn main() {
let x: int = foo::<int, cbar>(cbar(5));
assert x == 5;
}