]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/method-two-trait-defer-resolution-2.rs
Imported Upstream version 1.0.0~beta.3
[rustc.git] / src / test / run-pass / method-two-trait-defer-resolution-2.rs
CommitLineData
1a4d82fc
JJ
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
9346a6ac 11// Test that when we write `x.foo()`, we do not have to know the
c34b1796
AL
12// complete type of `x` in order to type-check the method call. In
13// this case, we know that `x: Vec<_1>`, but we don't know what type
14// `_1` is (because the call to `push` comes later). To pick between
15// the impls, we would have to know `_1`, since we have to know
16// whether `_1: MyCopy` or `_1 == Box<i32>`. However (and this is the
17// point of the test), we don't have to pick between the two impls --
18// it is enough to know that `foo` comes from the `Foo` trait. We can
19// translate the call as `Foo::foo(&x)` and let the specific impl get
20// chosen later.
21
1a4d82fc
JJ
22
23#![allow(unknown_features)]
24#![feature(box_syntax)]
25
26trait Foo {
c34b1796 27 fn foo(&self) -> isize;
1a4d82fc
JJ
28}
29
c34b1796
AL
30trait MyCopy { fn foo(&self) { } }
31impl MyCopy for i32 { }
32
33impl<T:MyCopy> Foo for Vec<T> {
34 fn foo(&self) -> isize {1}
1a4d82fc
JJ
35}
36
c34b1796
AL
37impl Foo for Vec<Box<i32>> {
38 fn foo(&self) -> isize {2}
1a4d82fc
JJ
39}
40
c34b1796 41fn call_foo_copy() -> isize {
1a4d82fc
JJ
42 let mut x = Vec::new();
43 let y = x.foo();
c34b1796 44 x.push(0_i32);
1a4d82fc
JJ
45 y
46}
47
c34b1796
AL
48fn call_foo_other() -> isize {
49 let mut x: Vec<_> = Vec::new();
1a4d82fc 50 let y = x.foo();
c34b1796
AL
51 let z: Box<i32> = box 0;
52 x.push(z);
1a4d82fc
JJ
53 y
54}
55
56fn main() {
57 assert_eq!(call_foo_copy(), 1);
58 assert_eq!(call_foo_other(), 2);
59}