2014-12-14 06:17:23 -06:00
|
|
|
// Test a case where you have an impl of `Foo<X>` for all `X` that
|
|
|
|
// is being applied to `for<'a> Foo<&'a mut X>`. Issue #19730.
|
|
|
|
|
|
|
|
trait Foo<X> {
|
2021-02-09 16:59:32 -06:00
|
|
|
fn foo(&mut self, x: X) {}
|
2014-12-14 06:17:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Bar<X> {
|
2021-02-09 16:59:32 -06:00
|
|
|
fn bar(&mut self, x: X) {}
|
2014-12-14 06:17:23 -06:00
|
|
|
}
|
|
|
|
|
2021-02-09 16:59:32 -06:00
|
|
|
impl<'a, X, F> Foo<X> for &'a mut F where F: Foo<X> + Bar<X> {}
|
2014-12-14 06:17:23 -06:00
|
|
|
|
2021-02-09 16:59:32 -06:00
|
|
|
impl<'a, X, F> Bar<X> for &'a mut F where F: Bar<X> {}
|
2014-12-14 06:17:23 -06:00
|
|
|
|
2022-04-01 12:13:25 -05:00
|
|
|
fn no_hrtb<'b, T>(mut t: T) //~ WARN function cannot return
|
2021-02-09 16:59:32 -06:00
|
|
|
where
|
|
|
|
T: Bar<&'b isize>,
|
2014-12-14 06:17:23 -06:00
|
|
|
{
|
2014-12-05 20:12:25 -06:00
|
|
|
// OK -- `T : Bar<&'b isize>`, and thus the impl above ensures that
|
|
|
|
// `&mut T : Bar<&'b isize>`.
|
2014-12-14 06:17:23 -06:00
|
|
|
no_hrtb(&mut t);
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:13:25 -05:00
|
|
|
fn bar_hrtb<T>(mut t: T) //~ WARN function cannot return
|
2021-02-09 16:59:32 -06:00
|
|
|
where
|
|
|
|
T: for<'b> Bar<&'b isize>,
|
2014-12-14 06:17:23 -06:00
|
|
|
{
|
2014-12-05 20:12:25 -06:00
|
|
|
// OK -- `T : for<'b> Bar<&'b isize>`, and thus the impl above
|
|
|
|
// ensures that `&mut T : for<'b> Bar<&'b isize>`. This is an
|
2014-12-14 06:17:23 -06:00
|
|
|
// example of a "perfect forwarding" impl.
|
|
|
|
bar_hrtb(&mut t);
|
|
|
|
}
|
|
|
|
|
2022-04-01 12:13:25 -05:00
|
|
|
fn foo_hrtb_bar_not<'b, T>(mut t: T) //~ WARN function cannot return
|
2021-02-09 16:59:32 -06:00
|
|
|
where
|
|
|
|
T: for<'a> Foo<&'a isize> + Bar<&'b isize>,
|
2014-12-14 06:17:23 -06:00
|
|
|
{
|
|
|
|
// Not OK -- The forwarding impl for `Foo` requires that `Bar` also
|
|
|
|
// be implemented. Thus to satisfy `&mut T : for<'a> Foo<&'a
|
2014-12-05 20:12:25 -06:00
|
|
|
// isize>`, we require `T : for<'a> Bar<&'a isize>`, but the where
|
|
|
|
// clause only specifies `T : Bar<&'b isize>`.
|
2021-02-09 16:59:32 -06:00
|
|
|
foo_hrtb_bar_not(&mut t);
|
|
|
|
//~^ ERROR implementation of `Bar` is not general enough
|
2022-04-01 12:13:25 -05:00
|
|
|
//~^^ ERROR lifetime may not live long enough
|
2014-12-14 06:17:23 -06:00
|
|
|
}
|
|
|
|
|
2022-04-01 12:13:25 -05:00
|
|
|
fn foo_hrtb_bar_hrtb<T>(mut t: T) //~ WARN function cannot return
|
2021-02-09 16:59:32 -06:00
|
|
|
where
|
|
|
|
T: for<'a> Foo<&'a isize> + for<'b> Bar<&'b isize>,
|
2014-12-14 06:17:23 -06:00
|
|
|
{
|
2021-02-09 16:59:32 -06:00
|
|
|
// OK -- now we have `T : for<'b> Bar<&'b isize>`.
|
2014-12-14 06:17:23 -06:00
|
|
|
foo_hrtb_bar_hrtb(&mut t);
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:59:32 -06:00
|
|
|
fn main() {}
|