]> git.lizzy.rs Git - rust.git/blob - src/test/ui/hrtb/hrtb-perfect-forwarding.rs
Auto merge of #98120 - TaKO8Ki:box-diagnostic-metadata-field, r=estebank
[rust.git] / src / test / ui / hrtb / hrtb-perfect-forwarding.rs
1 // Test a case where you have an impl of `Foo<X>` for all `X` that
2 // is being applied to `for<'a> Foo<&'a mut X>`. Issue #19730.
3
4 trait Foo<X> {
5     fn foo(&mut self, x: X) {}
6 }
7
8 trait Bar<X> {
9     fn bar(&mut self, x: X) {}
10 }
11
12 impl<'a, X, F> Foo<X> for &'a mut F where F: Foo<X> + Bar<X> {}
13
14 impl<'a, X, F> Bar<X> for &'a mut F where F: Bar<X> {}
15
16 fn no_hrtb<'b, T>(mut t: T) //~ WARN function cannot return
17 where
18     T: Bar<&'b isize>,
19 {
20     // OK -- `T : Bar<&'b isize>`, and thus the impl above ensures that
21     // `&mut T : Bar<&'b isize>`.
22     no_hrtb(&mut t);
23 }
24
25 fn bar_hrtb<T>(mut t: T) //~ WARN function cannot return
26 where
27     T: for<'b> Bar<&'b isize>,
28 {
29     // OK -- `T : for<'b> Bar<&'b isize>`, and thus the impl above
30     // ensures that `&mut T : for<'b> Bar<&'b isize>`.  This is an
31     // example of a "perfect forwarding" impl.
32     bar_hrtb(&mut t);
33 }
34
35 fn foo_hrtb_bar_not<'b, T>(mut t: T) //~ WARN function cannot return
36 where
37     T: for<'a> Foo<&'a isize> + Bar<&'b isize>,
38 {
39     // Not OK -- The forwarding impl for `Foo` requires that `Bar` also
40     // be implemented. Thus to satisfy `&mut T : for<'a> Foo<&'a
41     // isize>`, we require `T : for<'a> Bar<&'a isize>`, but the where
42     // clause only specifies `T : Bar<&'b isize>`.
43     foo_hrtb_bar_not(&mut t);
44     //~^ ERROR implementation of `Bar` is not general enough
45     //~^^ ERROR lifetime may not live long enough
46 }
47
48 fn foo_hrtb_bar_hrtb<T>(mut t: T) //~ WARN function cannot return
49 where
50     T: for<'a> Foo<&'a isize> + for<'b> Bar<&'b isize>,
51 {
52     // OK -- now we have `T : for<'b> Bar<&'b isize>`.
53     foo_hrtb_bar_hrtb(&mut t);
54 }
55
56 fn main() {}