]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/method-two-trait-defer-resolution-2.rs
cae783e7ea84a2a337c26f3bd9a7738c6b649094
[rust.git] / src / test / run-pass / method-two-trait-defer-resolution-2.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that we pick which version of `Foo` to run based on whether
12 // the type we (ultimately) inferred for `x` is copyable or not.
13 //
14 // In this case, the two versions are both impls of same trait, and
15 // hence we we can resolve method even without knowing yet which
16 // version will run (note that the `push` occurs after the call to
17 // `foo()`).
18
19 trait Foo {
20     fn foo(&self) -> int;
21 }
22
23 impl<T:Copy> Foo for Vec<T> {
24     fn foo(&self) -> int {1}
25 }
26
27 impl<T> Foo for Vec<Box<T>> {
28     fn foo(&self) -> int {2}
29 }
30
31 fn call_foo_copy() -> int {
32     let mut x = Vec::new();
33     let y = x.foo();
34     x.push(0u);
35     y
36 }
37
38 fn call_foo_other() -> int {
39     let mut x = Vec::new();
40     let y = x.foo();
41     x.push(box 0i);
42     y
43 }
44
45 fn main() {
46     assert_eq!(call_foo_copy(), 1);
47     assert_eq!(call_foo_other(), 2);
48 }