2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2015-03-25 12:02:11 -05:00
|
|
|
// Test that when we write `x.foo()`, we do not have to know the
|
2015-03-30 16:49:30 -05:00
|
|
|
// complete type of `x` in order to type-check the method call. In
|
|
|
|
// this case, we know that `x: Vec<_1>`, but we don't know what type
|
|
|
|
// `_1` is (because the call to `push` comes later). To pick between
|
|
|
|
// the impls, we would have to know `_1`, since we have to know
|
|
|
|
// whether `_1: MyCopy` or `_1 == Box<i32>`. However (and this is the
|
|
|
|
// point of the test), we don't have to pick between the two impls --
|
|
|
|
// it is enough to know that `foo` comes from the `Foo` trait. We can
|
2018-05-08 08:10:16 -05:00
|
|
|
// codegen the call as `Foo::foo(&x)` and let the specific impl get
|
2015-03-30 16:49:30 -05:00
|
|
|
// chosen later.
|
2014-10-17 08:13:12 -05:00
|
|
|
|
|
|
|
trait Foo {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn foo(&self) -> isize;
|
2014-10-17 08:13:12 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 16:49:30 -05:00
|
|
|
trait MyCopy { fn foo(&self) { } }
|
|
|
|
impl MyCopy for i32 { }
|
|
|
|
|
|
|
|
impl<T:MyCopy> Foo for Vec<T> {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn foo(&self) -> isize {1}
|
2014-10-17 08:13:12 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 16:49:30 -05:00
|
|
|
impl Foo for Vec<Box<i32>> {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn foo(&self) -> isize {2}
|
2014-10-17 08:13:12 -05:00
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn call_foo_copy() -> isize {
|
2014-10-17 08:13:12 -05:00
|
|
|
let mut x = Vec::new();
|
|
|
|
let y = x.foo();
|
2015-03-30 16:49:30 -05:00
|
|
|
x.push(0_i32);
|
2014-10-17 08:13:12 -05:00
|
|
|
y
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn call_foo_other() -> isize {
|
2015-03-30 16:49:30 -05:00
|
|
|
let mut x: Vec<_> = Vec::new();
|
2014-10-17 08:13:12 -05:00
|
|
|
let y = x.foo();
|
2021-08-24 19:39:40 -05:00
|
|
|
let z: Box<i32> = Box::new(0);
|
2015-03-30 16:49:30 -05:00
|
|
|
x.push(z);
|
2014-10-17 08:13:12 -05:00
|
|
|
y
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
assert_eq!(call_foo_copy(), 1);
|
|
|
|
assert_eq!(call_foo_other(), 2);
|
|
|
|
}
|