Skip to content

Commit

Permalink
In ty::impl_traits, treat structs properly
Browse files Browse the repository at this point in the history
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
  • Loading branch information
catamorphism committed Aug 29, 2012
1 parent a19dce6 commit a70e37b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
19 changes: 4 additions & 15 deletions src/rustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
_ => ~[]
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/run-pass/issue-2936.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit a70e37b

Please sign in to comment.