File: method-two-trait-defer-resolution-2.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (46 lines) | stat: -rw-r--r-- 1,253 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//@ run-pass
// Test that when we write `x.foo()`, we do not have to know the
// 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
// codegen the call as `Foo::foo(&x)` and let the specific impl get
// chosen later.

trait Foo {
    fn foo(&self) -> isize;
}

trait MyCopy { fn foo(&self) { } } //~ WARN method `foo` is never used
impl MyCopy for i32 { }

impl<T:MyCopy> Foo for Vec<T> {
    fn foo(&self) -> isize {1}
}

impl Foo for Vec<Box<i32>> {
    fn foo(&self) -> isize {2}
}

fn call_foo_copy() -> isize {
    let mut x = Vec::new();
    let y = x.foo();
    x.push(0_i32);
    y
}

fn call_foo_other() -> isize {
    let mut x: Vec<_> = Vec::new();
    let y = x.foo();
    let z: Box<i32> = Box::new(0);
    x.push(z);
    y
}

fn main() {
    assert_eq!(call_foo_copy(), 1);
    assert_eq!(call_foo_other(), 2);
}