]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/hrtb-perfect-forwarding.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / compile-fail / hrtb-perfect-forwarding.rs
1 // Copyright 2014 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 a case where you have an impl of `Foo<X>` for all `X` that
12 // is being applied to `for<'a> Foo<&'a mut X>`. Issue #19730.
13
14 trait Foo<X> {
15 fn foo(&mut self, x: X) { }
16 }
17
18 trait Bar<X> {
19 fn bar(&mut self, x: X) { }
20 }
21
22 impl<'a,X,F> Foo<X> for &'a mut F
23 where F : Foo<X> + Bar<X>
24 {
25 }
26
27 impl<'a,X,F> Bar<X> for &'a mut F
28 where F : Bar<X>
29 {
30 }
31
32 fn no_hrtb<'b,T>(mut t: T)
33 where T : Bar<&'b isize>
34 {
35 // OK -- `T : Bar<&'b isize>`, and thus the impl above ensures that
36 // `&mut T : Bar<&'b isize>`.
37 no_hrtb(&mut t);
38 }
39
40 fn bar_hrtb<T>(mut t: T)
41 where T : for<'b> Bar<&'b isize>
42 {
43 // OK -- `T : for<'b> Bar<&'b isize>`, and thus the impl above
44 // ensures that `&mut T : for<'b> Bar<&'b isize>`. This is an
45 // example of a "perfect forwarding" impl.
46 bar_hrtb(&mut t);
47 }
48
49 fn foo_hrtb_bar_not<'b,T>(mut t: T)
50 where T : for<'a> Foo<&'a isize> + Bar<&'b isize>
51 {
52 // Not OK -- The forwarding impl for `Foo` requires that `Bar` also
53 // be implemented. Thus to satisfy `&mut T : for<'a> Foo<&'a
54 // isize>`, we require `T : for<'a> Bar<&'a isize>`, but the where
55 // clause only specifies `T : Bar<&'b isize>`.
56 foo_hrtb_bar_not(&mut t); //~ ERROR `for<'a> T: Bar<&'a isize>` is not satisfied
57 }
58
59 fn foo_hrtb_bar_hrtb<T>(mut t: T)
60 where T : for<'a> Foo<&'a isize> + for<'b> Bar<&'b isize>
61 {
62 // OK -- now we have `T : for<'b> Bar&'b isize>`.
63 foo_hrtb_bar_hrtb(&mut t);
64 }
65
66 fn main() { }