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.
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.
11 // Various uses of `T::Item` syntax where the bound that supplies
12 // `Item` originates in a where-clause, not the declaration of
17 use std::marker::{PhantomData};
18 use std::sync::atomic::{AtomicUsize};
19 use std::sync::atomic::Ordering::SeqCst;
21 static COUNTER: AtomicUsize = AtomicUsize::new(0);
24 trait Trait { type Item; }
26 impl Trait for Struct {
30 // Where-clause attached on the method which declares `T`.
33 fn foo<T>(_x: T::Item) where T: Trait {
34 COUNTER.fetch_add(1, SeqCst);
38 // Where-clause attached on the method to a parameter from the struct.
39 struct B<T>(PhantomData<T>);
41 fn foo(_x: T::Item) where T: Trait {
42 COUNTER.fetch_add(10, SeqCst);
46 // Where-clause attached to free fn.
47 fn c<T>(_: T::Item) where T : Trait {
48 COUNTER.fetch_add(100, SeqCst);
51 // Where-clause attached to defaulted and non-defaulted trait method.
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);
59 impl AnotherTrait for D {
60 fn method<T>(&self, _: T::Item) where T: Trait {
61 COUNTER.fetch_add(10000, SeqCst);
65 // Where-clause attached to trait and impl containing the method.
66 trait YetAnotherTrait<T>
69 fn method(&self, _: T::Item);
70 fn default_method(&self, _: T::Item) {
71 COUNTER.fetch_add(100000, SeqCst);
74 struct E<T>(PhantomData<T>);
75 impl<T> YetAnotherTrait<T> for E<T>
78 fn method(&self, _: T::Item) {
79 COUNTER.fetch_add(1000000, SeqCst);
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);
91 // Where-clause attached to struct.
93 struct G<T> where T : Trait {
95 phantom: PhantomData<T>,
100 B::<Struct>::foo(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);