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