]> git.lizzy.rs Git - rust.git/blob - src/test/ui/methods/method-two-trait-defer-resolution-2.rs
Override rustc version in ui and mir-opt tests to get stable hashes
[rust.git] / src / test / ui / methods / method-two-trait-defer-resolution-2.rs
1 // run-pass
2 // Test that when we write `x.foo()`, we do not have to know the
3 // complete type of `x` in order to type-check the method call. In
4 // this case, we know that `x: Vec<_1>`, but we don't know what type
5 // `_1` is (because the call to `push` comes later). To pick between
6 // the impls, we would have to know `_1`, since we have to know
7 // whether `_1: MyCopy` or `_1 == Box<i32>`.  However (and this is the
8 // point of the test), we don't have to pick between the two impls --
9 // it is enough to know that `foo` comes from the `Foo` trait. We can
10 // codegen the call as `Foo::foo(&x)` and let the specific impl get
11 // chosen later.
12
13 trait Foo {
14     fn foo(&self) -> isize;
15 }
16
17 trait MyCopy { fn foo(&self) { } }
18 impl MyCopy for i32 { }
19
20 impl<T:MyCopy> Foo for Vec<T> {
21     fn foo(&self) -> isize {1}
22 }
23
24 impl Foo for Vec<Box<i32>> {
25     fn foo(&self) -> isize {2}
26 }
27
28 fn call_foo_copy() -> isize {
29     let mut x = Vec::new();
30     let y = x.foo();
31     x.push(0_i32);
32     y
33 }
34
35 fn call_foo_other() -> isize {
36     let mut x: Vec<_> = Vec::new();
37     let y = x.foo();
38     let z: Box<i32> = Box::new(0);
39     x.push(z);
40     y
41 }
42
43 fn main() {
44     assert_eq!(call_foo_copy(), 1);
45     assert_eq!(call_foo_other(), 2);
46 }