summaryrefslogtreecommitdiffstats
path: root/src/test/ui/associated-types/associated-types-return.rs
blob: 997a48b0379a9582698e448f6a97ba4c38bc25f3 (plain)
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 equality constraints on associated types in a where clause.


pub trait Foo {
    type A;
    fn boo(&self) -> <Self as Foo>::A;
}

#[derive(PartialEq, Debug)]
pub struct Bar;

impl Foo for isize {
    type A = usize;
    fn boo(&self) -> usize { 42 }
}

impl Foo for Bar {
    type A = isize;
    fn boo(&self) -> isize { 43 }
}

impl Foo for char {
    type A = Bar;
    fn boo(&self) -> Bar { Bar }
}

fn foo1<I: Foo<A=Bar>>(x: I) -> Bar {
    x.boo()
}

fn foo2<I: Foo>(x: I) -> <I as Foo>::A {
    x.boo()
}

pub fn main() {
    let a = 42;
    assert_eq!(foo2(a), 42);

    let a = Bar;
    assert_eq!(foo2(a), 43);

    let a = 'a';
    foo1(a);
    assert_eq!(foo2(a), Bar);
}