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