2020-05-24 12:34:03 -05:00
|
|
|
pub trait Get<T> {
|
|
|
|
fn get(self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
x: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Get<usize> for Foo {
|
|
|
|
fn get(self) -> usize {
|
|
|
|
self.x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
|
|
|
|
where
|
2022-03-06 08:44:48 -06:00
|
|
|
G: Get<T>,
|
2020-05-24 12:34:03 -05:00
|
|
|
{
|
2021-02-27 20:31:56 -06:00
|
|
|
move || {
|
2022-03-06 08:44:48 -06:00
|
|
|
//~^ ERROR hidden type for `impl Trait` captures lifetime
|
2020-05-24 12:34:03 -05:00
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// After applying suggestion for `foo`:
|
|
|
|
fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
|
|
|
|
where
|
2022-03-06 08:44:48 -06:00
|
|
|
G: Get<T>,
|
2020-05-24 12:34:03 -05:00
|
|
|
{
|
|
|
|
move || {
|
2022-04-01 12:13:25 -05:00
|
|
|
//~^ ERROR the parameter type `G` may not live long enough
|
2020-05-24 12:34:03 -05:00
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// After applying suggestion for `bar`:
|
2022-03-06 08:44:48 -06:00
|
|
|
fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
|
|
|
|
//~^ ERROR undeclared lifetime name `'a`
|
2020-05-24 12:34:03 -05:00
|
|
|
where
|
2022-03-06 08:44:48 -06:00
|
|
|
G: Get<T>,
|
2020-05-24 12:34:03 -05:00
|
|
|
{
|
|
|
|
move || {
|
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// After applying suggestion for `baz`:
|
|
|
|
fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
|
|
|
|
where
|
2022-03-06 08:44:48 -06:00
|
|
|
G: Get<T>,
|
2020-05-24 12:34:03 -05:00
|
|
|
{
|
|
|
|
move || {
|
2022-04-01 12:13:25 -05:00
|
|
|
//~^ ERROR the parameter type `G` may not live long enough
|
2020-05-24 12:34:03 -05:00
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:33:23 -05:00
|
|
|
// Same as above, but show that we pay attention to lifetime names from parent item
|
|
|
|
impl<'a> Foo {
|
|
|
|
fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
|
|
|
|
move || {
|
2022-04-01 12:13:25 -05:00
|
|
|
//~^ ERROR the parameter type `G` may not live long enough
|
2020-05-27 17:33:23 -05:00
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 12:34:03 -05:00
|
|
|
// After applying suggestion for `qux`:
|
|
|
|
fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
|
|
|
|
where
|
2022-03-06 08:44:48 -06:00
|
|
|
G: Get<T>,
|
2020-05-24 12:34:03 -05:00
|
|
|
{
|
Improve output of argument anonymous borrow missing annotation involving opaque return type
Go from
```
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> file8.rs:22:5
|
22 | / move || {
23 | | *dest = g.get();
24 | | }
| |_____^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the function body at 18:1...
--> file8.rs:18:1
|
18 | / fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
19 | | where
20 | | G: Get<T>
21 | | {
... |
24 | | }
25 | | }
| |_^
note: ...so that the types are compatible
--> file8.rs:22:5
|
22 | / move || { //~ ERROR cannot infer an appropriate lifetime
23 | | *dest = g.get();
24 | | }
| |_____^
= note: expected `&mut T`
found `&mut T`
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 18:8...
--> file8.rs:18:8
|
18 | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
| ^^
note: ...so that return value is valid for the call
--> file8.rs:18:45
|
18 | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
| ^^^^^^^^^^^^^^^^^^^^^^^
```
to
```
error[E0621]: explicit lifetime required in the type of `dest`
--> file8.rs:18:45
|
18 | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
| ------ ^^^^^^^^^^^^^^^^^^^^^^^ lifetime `'a` required
| |
| help: add explicit lifetime `'a` to the type of `dest`: `&'a mut T`
```
2020-05-24 13:52:12 -05:00
|
|
|
move || {
|
2022-04-01 12:13:25 -05:00
|
|
|
//~^ ERROR the parameter type `G` may not live long enough
|
|
|
|
//~| ERROR explicit lifetime required
|
2020-05-24 12:34:03 -05:00
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Potential incorrect attempt:
|
|
|
|
fn bak<'a, G, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
|
|
|
|
where
|
2022-03-06 08:44:48 -06:00
|
|
|
G: Get<T>,
|
2020-05-24 12:34:03 -05:00
|
|
|
{
|
|
|
|
move || {
|
2022-04-01 12:13:25 -05:00
|
|
|
//~^ ERROR the parameter type `G` may not live long enough
|
2020-05-24 12:34:03 -05:00
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to tie the lifetime of `G` with the lifetime of `&mut T` and the returned closure:
|
|
|
|
fn ok<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
|
|
|
|
where
|
2022-03-06 08:44:48 -06:00
|
|
|
G: Get<T>,
|
2020-05-24 12:34:03 -05:00
|
|
|
{
|
|
|
|
move || {
|
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This also works. The `'_` isn't necessary but it's where we arrive to following the suggestions:
|
|
|
|
fn ok2<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + '_ + 'a
|
|
|
|
where
|
2022-03-06 08:44:48 -06:00
|
|
|
G: Get<T>,
|
2020-05-24 12:34:03 -05:00
|
|
|
{
|
|
|
|
move || {
|
|
|
|
*dest = g.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|