]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/where-for-self.rs
Imported Upstream version 1.0.0~beta.3
[rustc.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
15 static mut COUNT: u32 = 1;
16
17 trait Bar<'a> {
18 fn bar(&self);
19 }
20
21 trait Baz<'a>
22 {
23 fn baz(&self);
24 }
25
26 impl<'a, 'b> Bar<'b> for &'a u32 {
27 fn bar(&self) {
28 unsafe { COUNT *= 2; }
29 }
30 }
31
32 impl<'a, 'b> Baz<'b> for &'a u32 {
33 fn baz(&self) {
34 unsafe { COUNT *= 3; }
35 }
36 }
37
38 // Test we can use the syntax for HRL including the self type.
39 fn foo1<T>(x: &T)
40 where for<'a, 'b> &'a T: Bar<'b>
41 {
42 x.bar()
43 }
44
45 // Test we can quantify multiple bounds (i.e., the precedence is sensible).
46 fn foo2<T>(x: &T)
47 where for<'a, 'b> &'a T: Bar<'b> + Baz<'b>
48 {
49 x.baz();
50 x.bar()
51 }
52
53 fn main() {
54 let x = 42;
55 foo1(&x);
56 foo2(&x);
57 unsafe {
58 assert!(COUNT == 12);
59 }
60 }