]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/associated-types-project-from-type-param-via-bound-in-where-clause.rs
rustdoc: pretty-print Unevaluated expressions in types.
[rust.git] / src / test / run-pass / associated-types-project-from-type-param-via-bound-in-where-clause.rs
1 // Copyright 2015 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 // Various uses of `T::Item` syntax where the bound that supplies
12 // `Item` originates in a where-clause, not the declaration of
13 // `T`. Issue #20300.
14
15 #![feature(const_fn)]
16
17 use std::marker::{PhantomData};
18 use std::sync::atomic::{AtomicUsize};
19 use std::sync::atomic::Ordering::SeqCst;
20
21 static COUNTER: AtomicUsize = AtomicUsize::new(0);
22
23 // Preamble.
24 trait Trait { type Item; }
25 struct Struct;
26 impl Trait for Struct {
27     type Item = u32;
28 }
29
30 // Where-clause attached on the method which declares `T`.
31 struct A;
32 impl A {
33     fn foo<T>(_x: T::Item) where T: Trait {
34         COUNTER.fetch_add(1, SeqCst);
35     }
36 }
37
38 // Where-clause attached on the method to a parameter from the struct.
39 struct B<T>(PhantomData<T>);
40 impl<T> B<T> {
41     fn foo(_x: T::Item) where T: Trait {
42         COUNTER.fetch_add(10, SeqCst);
43     }
44 }
45
46 // Where-clause attached to free fn.
47 fn c<T>(_: T::Item) where T : Trait {
48     COUNTER.fetch_add(100, SeqCst);
49 }
50
51 // Where-clause attached to defaulted and non-defaulted trait method.
52 trait AnotherTrait {
53     fn method<T>(&self, _: T::Item) where T: Trait;
54     fn default_method<T>(&self, _: T::Item) where T: Trait {
55         COUNTER.fetch_add(1000, SeqCst);
56     }
57 }
58 struct D;
59 impl AnotherTrait for D {
60     fn method<T>(&self, _: T::Item) where T: Trait {
61         COUNTER.fetch_add(10000, SeqCst);
62     }
63 }
64
65 // Where-clause attached to trait and impl containing the method.
66 trait YetAnotherTrait<T>
67     where T : Trait
68 {
69     fn method(&self, _: T::Item);
70     fn default_method(&self, _: T::Item) {
71         COUNTER.fetch_add(100000, SeqCst);
72     }
73 }
74 struct E<T>(PhantomData<T>);
75 impl<T> YetAnotherTrait<T> for E<T>
76     where T : Trait
77 {
78     fn method(&self, _: T::Item) {
79         COUNTER.fetch_add(1000000, SeqCst);
80     }
81 }
82
83 // Where-clause attached to inherent impl containing the method.
84 struct F<T>(PhantomData<T>);
85 impl<T> F<T> where T : Trait {
86     fn method(&self, _: T::Item) {
87         COUNTER.fetch_add(10000000, SeqCst);
88     }
89 }
90
91 // Where-clause attached to struct.
92 #[allow(dead_code)]
93 struct G<T> where T : Trait {
94     data: T::Item,
95     phantom: PhantomData<T>,
96 }
97
98 fn main() {
99     A::foo::<Struct>(22);
100     B::<Struct>::foo(22);
101     c::<Struct>(22);
102     D.method::<Struct>(22);
103     D.default_method::<Struct>(22);
104     E(PhantomData::<Struct>).method(22);
105     E(PhantomData::<Struct>).default_method(22);
106     F(PhantomData::<Struct>).method(22);
107     G::<Struct> { data: 22, phantom: PhantomData };
108     assert_eq!(COUNTER.load(SeqCst), 11111111);
109 }