]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/associated-types-return.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / test / run-pass / associated-types-return.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 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 equality constraints on associated types in a where clause.
12
c34b1796 13
1a4d82fc
JJ
14pub trait Foo {
15 type A;
16 fn boo(&self) -> <Self as Foo>::A;
17}
18
62682a34 19#[derive(PartialEq, Debug)]
c34b1796 20pub struct Bar;
1a4d82fc 21
c34b1796
AL
22impl Foo for isize {
23 type A = usize;
24 fn boo(&self) -> usize { 42 }
1a4d82fc
JJ
25}
26
27impl Foo for Bar {
c34b1796
AL
28 type A = isize;
29 fn boo(&self) -> isize { 43 }
1a4d82fc
JJ
30}
31
32impl Foo for char {
33 type A = Bar;
34 fn boo(&self) -> Bar { Bar }
35}
36
37fn foo1<I: Foo<A=Bar>>(x: I) -> Bar {
38 x.boo()
39}
40
41fn foo2<I: Foo>(x: I) -> <I as Foo>::A {
42 x.boo()
43}
44
45pub fn main() {
85aaf69f 46 let a = 42;
62682a34 47 assert_eq!(foo2(a), 42);
1a4d82fc
JJ
48
49 let a = Bar;
62682a34 50 assert_eq!(foo2(a), 43);
1a4d82fc
JJ
51
52 let a = 'a';
53 foo1(a);
62682a34 54 assert_eq!(foo2(a), Bar);
1a4d82fc 55}