Skip to content

Commit

Permalink
Use callee ID when kind-checking expressions that may be overloaded
Browse files Browse the repository at this point in the history
And fix up test cases that should have failed if not for this bug.

Closes #2587
  • Loading branch information
catamorphism committed Sep 6, 2012
1 parent c5e2cf2 commit 46990ad
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
14 changes: 11 additions & 3 deletions src/rustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,25 @@ fn check_arm(a: arm, cx: ctx, v: visit::vt<ctx>) {

fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
debug!("kind::check_expr(%s)", expr_to_str(e, cx.tcx.sess.intr()));
let id_to_use = match e.node {
expr_index(*)|expr_assign_op(*)|
expr_unary(*)|expr_binary(*) => e.callee_id,
_ => e.id
};

// Handle any kind bounds on type parameters
do option::iter(cx.tcx.node_type_substs.find(e.id)) |ts| {
do option::iter(cx.tcx.node_type_substs.find(id_to_use)) |ts| {
let bounds = match e.node {
expr_path(_) => {
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(e.id));
ty::lookup_item_type(cx.tcx, did).bounds
}
_ => {
// Type substitions should only occur on paths and
// Type substitutions should only occur on paths and
// method calls, so this needs to be a method call.

// Even though the callee_id may have been the id with
// node_type_substs, e.id is correct here.
ty::method_call_bounds(cx.tcx, cx.method_map, e.id).expect(
~"non path/method call expr has type substs??")
}
Expand All @@ -265,7 +273,7 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
*bounds, (*bounds).len());
}
do vec::iter2(ts, *bounds) |ty, bound| {
check_bounds(cx, e.id, e.span, ty, bound)
check_bounds(cx, id_to_use, e.span, ty, bound)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
// A test case for #2548.

// xfail-test

struct foo {
x: @mut int;

new(x: @mut int) { self.x = x; }

drop {
io::println("Goodbye, World!");
*self.x += 1;
}
}

fn foo(x: @mut int) -> foo {
foo { x: x }
}

fn main() {
let x = @mut 0;

{
let mut res = foo(x);

let mut v = ~[mut];
v <- ~[mut res] + v;
v <- ~[mut res] + v; //~ ERROR instantiating a type parameter with an incompatible type (needs `copy`, got `owned`, missing `copy`)
assert (v.len() == 2);
}

assert *x == 1;
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/monad.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
trait vec_monad<A> {
fn bind<B>(f: fn(A) -> ~[B]) -> ~[B];
fn bind<B: copy>(f: fn(A) -> ~[B]) -> ~[B];
}

impl<A> ~[A]: vec_monad<A> {
fn bind<B>(f: fn(A) -> ~[B]) -> ~[B] {
fn bind<B: copy>(f: fn(A) -> ~[B]) -> ~[B] {
let mut r = ~[];
for self.each |elt| { r += f(elt); }
r
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-pass/static-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ impl uint: uint_utils {
trait vec_utils<T> {
fn length_() -> uint;
fn iter_(f: fn(T));
fn map_<U>(f: fn(T) -> U) -> ~[U];
fn map_<U: copy>(f: fn(T) -> U) -> ~[U];
}

impl<T> ~[T]: vec_utils<T> {
fn length_() -> uint { vec::len(self) }
fn iter_(f: fn(T)) { for self.each |x| { f(x); } }
fn map_<U>(f: fn(T) -> U) -> ~[U] {
fn map_<U: copy>(f: fn(T) -> U) -> ~[U] {
let mut r = ~[];
for self.each |elt| { r += ~[f(elt)]; }
r
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/trait-generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ impl int: to_str {
fn to_str() -> ~str { int::str(self) }
}
impl ~str: to_str {
fn to_str() -> ~str { self }
fn to_str() -> ~str { copy self }
}
impl (): to_str {
fn to_str() -> ~str { ~"()" }
}

trait map<T> {
fn map<U>(f: fn(T) -> U) -> ~[U];
fn map<U: copy>(f: fn(T) -> U) -> ~[U];
}
impl<T> ~[T]: map<T> {
fn map<U>(f: fn(T) -> U) -> ~[U] {
fn map<U: copy>(f: fn(T) -> U) -> ~[U] {
let mut r = ~[];
for self.each |x| { r += ~[f(x)]; }
r
Expand Down

0 comments on commit 46990ad

Please sign in to comment.