forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account for missing lifetime in opaque return type
When encountering an opaque closure return type that needs to bound a lifetime to the function's arguments, including borrows and type params, provide appropriate suggestions that lead to working code. Get the user from ```rust fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce() where G: Get<T> { move || { *dest = g.get(); } } ``` to ```rust fn foo<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() +'a where G: Get<T> { move || { *dest = g.get(); } } ```
- Loading branch information
Showing
5 changed files
with
359 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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 | ||
G: Get<T> | ||
{ | ||
move || { //~ ERROR cannot infer an appropriate lifetime | ||
*dest = g.get(); | ||
} | ||
} | ||
|
||
// After applying suggestion for `foo`: | ||
fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ | ||
//~^ ERROR the parameter type `G` may not live long enough | ||
where | ||
G: Get<T> | ||
{ | ||
move || { | ||
*dest = g.get(); | ||
} | ||
} | ||
|
||
|
||
// After applying suggestion for `bar`: | ||
fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ //~ ERROR undeclared lifetime | ||
where | ||
G: Get<T> | ||
{ | ||
move || { | ||
*dest = g.get(); | ||
} | ||
} | ||
|
||
// After applying suggestion for `baz`: | ||
fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ | ||
//~^ ERROR the parameter type `G` may not live long enough | ||
where | ||
G: Get<T> | ||
{ | ||
move || { | ||
*dest = g.get(); | ||
} | ||
} | ||
|
||
// After applying suggestion for `qux`: | ||
// FIXME: we should suggest be suggesting to change `dest` to `&'a mut T`. | ||
fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a | ||
where | ||
G: Get<T> | ||
{ | ||
move || { //~ ERROR cannot infer an appropriate lifetime | ||
*dest = g.get(); | ||
} | ||
} | ||
|
||
// Potential incorrect attempt: | ||
fn bak<'a, G, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a | ||
//~^ ERROR the parameter type `G` may not live long enough | ||
where | ||
G: Get<T> | ||
{ | ||
move || { | ||
*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 | ||
G: Get<T> | ||
{ | ||
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 | ||
G: Get<T> | ||
{ | ||
move || { | ||
*dest = g.get(); | ||
} | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.