]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/where-for-self.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / test / run-pass / where-for-self.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 // Test that we can quantify lifetimes outside a constraint (i.e., including
12 // the self type) in a where clause.
13
14 use std::marker::PhantomFn;
15
16 static mut COUNT: u32 = 1;
17
18 trait Bar<'a>
19     : PhantomFn<&'a ()>
20 {
21     fn bar(&self);
22 }
23
24 trait Baz<'a>
25     : PhantomFn<&'a ()>
26 {
27     fn baz(&self);
28 }
29
30 impl<'a, 'b> Bar<'b> for &'a u32 {
31     fn bar(&self) {
32         unsafe { COUNT *= 2; }
33     }
34 }
35
36 impl<'a, 'b> Baz<'b> for &'a u32 {
37     fn baz(&self) {
38         unsafe { COUNT *= 3; }
39     }
40 }
41
42 // Test we can use the syntax for HRL including the self type.
43 fn foo1<T>(x: &T)
44     where for<'a, 'b> &'a T: Bar<'b>
45 {
46     x.bar()
47 }
48
49 // Test we can quantify multiple bounds (i.e., the precedence is sensible).
50 fn foo2<T>(x: &T)
51     where for<'a, 'b> &'a T: Bar<'b> + Baz<'b>
52 {
53     x.baz();
54     x.bar()
55 }
56
57 fn main() {
58     let x = 42u32;
59     foo1(&x);
60     foo2(&x);
61     unsafe {
62         assert!(COUNT == 12);
63     }
64 }
65