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
15 use std
::marker
::{PhantomData}
;
16 use std
::sync
::atomic
::{AtomicUsize, ATOMIC_USIZE_INIT}
;
17 use std
::sync
::atomic
::Ordering
::SeqCst
;
19 static COUNTER
: AtomicUsize
= ATOMIC_USIZE_INIT
;
22 trait Trait { type Item; }
24 impl Trait
for Struct
{
28 // Where-clause attached on the method which declares `T`.
31 fn foo
<T
>(_x
: T
::Item
) where T
: Trait
{
32 COUNTER
.fetch_add(1, SeqCst
);
36 // Where-clause attached on the method to a parameter from the struct.
37 struct B
<T
>(PhantomData
<T
>);
39 fn foo(_x
: T
::Item
) where T
: Trait
{
40 COUNTER
.fetch_add(10, SeqCst
);
44 // Where-clause attached to free fn.
45 fn c
<T
>(_
: T
::Item
) where T
: Trait
{
46 COUNTER
.fetch_add(100, SeqCst
);
49 // Where-clause attached to defaulted and non-defaulted trait method.
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
);
57 impl AnotherTrait
for D
{
58 fn method
<T
>(&self, _
: T
::Item
) where T
: Trait
{
59 COUNTER
.fetch_add(10000, SeqCst
);
63 // Where-clause attached to trait and impl containing the method.
64 trait YetAnotherTrait
<T
>
67 fn method(&self, _
: T
::Item
);
68 fn default_method(&self, _
: T
::Item
) {
69 COUNTER
.fetch_add(100000, SeqCst
);
72 struct E
<T
>(PhantomData
<T
>);
73 impl<T
> YetAnotherTrait
<T
> for E
<T
>
76 fn method(&self, _
: T
::Item
) {
77 COUNTER
.fetch_add(1000000, SeqCst
);
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
);
89 // Where-clause attached to struct.
91 struct G
<T
> where T
: Trait
{
93 phantom
: PhantomData
<T
>,
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);