]> git.proxmox.com Git - rustc.git/blame - src/test/ui/hrtb/hrtb-perfect-forwarding.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / test / ui / hrtb / hrtb-perfect-forwarding.rs
CommitLineData
1a4d82fc
JJ
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
4trait Foo<X> {
6a06907d 5 fn foo(&mut self, x: X) {}
1a4d82fc
JJ
6}
7
8trait Bar<X> {
6a06907d 9 fn bar(&mut self, x: X) {}
1a4d82fc
JJ
10}
11
6a06907d 12impl<'a, X, F> Foo<X> for &'a mut F where F: Foo<X> + Bar<X> {}
1a4d82fc 13
6a06907d 14impl<'a, X, F> Bar<X> for &'a mut F where F: Bar<X> {}
1a4d82fc 15
923072b8 16fn no_hrtb<'b, T>(mut t: T) //~ WARN function cannot return
6a06907d
XL
17where
18 T: Bar<&'b isize>,
1a4d82fc
JJ
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
923072b8 25fn bar_hrtb<T>(mut t: T) //~ WARN function cannot return
6a06907d
XL
26where
27 T: for<'b> Bar<&'b isize>,
1a4d82fc
JJ
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
923072b8 35fn foo_hrtb_bar_not<'b, T>(mut t: T) //~ WARN function cannot return
6a06907d
XL
36where
37 T: for<'a> Foo<&'a isize> + Bar<&'b isize>,
1a4d82fc
JJ
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>`.
6a06907d
XL
43 foo_hrtb_bar_not(&mut t);
44 //~^ ERROR implementation of `Bar` is not general enough
923072b8 45 //~^^ ERROR lifetime may not live long enough
1a4d82fc
JJ
46}
47
923072b8 48fn foo_hrtb_bar_hrtb<T>(mut t: T) //~ WARN function cannot return
6a06907d
XL
49where
50 T: for<'a> Foo<&'a isize> + for<'b> Bar<&'b isize>,
1a4d82fc 51{
6a06907d 52 // OK -- now we have `T : for<'b> Bar<&'b isize>`.
1a4d82fc
JJ
53 foo_hrtb_bar_hrtb(&mut t);
54}
55
6a06907d 56fn main() {}