Suggest move for closures and async blocks in more cases.
This commit is contained in:
parent
42abbd8878
commit
aaebbe196b
9 changed files with 84 additions and 68 deletions
|
|
@ -1,12 +1,18 @@
|
|||
// edition:2018
|
||||
// run-rustfix
|
||||
|
||||
fn foo() -> Box<impl std::future::Future<Output = u32>> {
|
||||
fn test_boxed() -> Box<impl std::future::Future<Output = u32>> {
|
||||
let x = 0u32;
|
||||
Box::new(async move { x } )
|
||||
//~^ ERROR E0373
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _foo = foo();
|
||||
fn test_ref(x: &u32) -> impl std::future::Future<Output = u32> + '_ {
|
||||
async move { *x }
|
||||
//~^ ERROR E0373
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = test_boxed();
|
||||
let _ = test_ref(&0u32);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,18 @@
|
|||
// edition:2018
|
||||
// run-rustfix
|
||||
|
||||
fn foo() -> Box<impl std::future::Future<Output = u32>> {
|
||||
fn test_boxed() -> Box<impl std::future::Future<Output = u32>> {
|
||||
let x = 0u32;
|
||||
Box::new(async { x } )
|
||||
//~^ ERROR E0373
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _foo = foo();
|
||||
fn test_ref(x: &u32) -> impl std::future::Future<Output = u32> + '_ {
|
||||
async { *x }
|
||||
//~^ ERROR E0373
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = test_boxed();
|
||||
let _ = test_ref(&0u32);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
|
||||
error[E0373]: async block may outlive the current function, but it borrows `x`, which is owned by the current function
|
||||
--> $DIR/async-borrowck-escaping-block-error.rs:6:20
|
||||
|
|
||||
LL | Box::new(async { x } )
|
||||
|
|
@ -7,16 +7,35 @@ LL | Box::new(async { x } )
|
|||
| | `x` is borrowed here
|
||||
| may outlive borrowed value `x`
|
||||
|
|
||||
note: generator is returned here
|
||||
--> $DIR/async-borrowck-escaping-block-error.rs:4:13
|
||||
note: async block is returned here
|
||||
--> $DIR/async-borrowck-escaping-block-error.rs:4:20
|
||||
|
|
||||
LL | fn foo() -> Box<impl std::future::Future<Output = u32>> {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
LL | fn test_boxed() -> Box<impl std::future::Future<Output = u32>> {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: to force the async block to take ownership of `x` (and any other referenced variables), use the `move` keyword
|
||||
|
|
||||
LL | Box::new(async move { x } )
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error: aborting due to previous error
|
||||
error[E0373]: async block may outlive the current function, but it borrows `x`, which is owned by the current function
|
||||
--> $DIR/async-borrowck-escaping-block-error.rs:11:11
|
||||
|
|
||||
LL | async { *x }
|
||||
| ^^^-^^
|
||||
| | |
|
||||
| | `x` is borrowed here
|
||||
| may outlive borrowed value `x`
|
||||
|
|
||||
note: async block is returned here
|
||||
--> $DIR/async-borrowck-escaping-block-error.rs:11:5
|
||||
|
|
||||
LL | async { *x }
|
||||
| ^^^^^^^^^^^^
|
||||
help: to force the async block to take ownership of `x` (and any other referenced variables), use the `move` keyword
|
||||
|
|
||||
LL | async move { *x }
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0373`.
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// edition:2018
|
||||
#![feature(async_closure,async_await)]
|
||||
#![feature(async_closure)]
|
||||
fn foo() -> Box<dyn std::future::Future<Output = u32>> {
|
||||
let x = 0u32;
|
||||
Box::new((async || x)())
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ struct List {
|
|||
impl List {
|
||||
fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> {
|
||||
self.data.iter().filter(|s| s.starts_with(prefix)).map(|s| s.as_ref())
|
||||
//~^ ERROR does not live long enough
|
||||
//~^ ERROR E0373
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
error[E0597]: `prefix` does not live long enough
|
||||
--> $DIR/does-not-live-long-enough.rs:6:51
|
||||
error[E0373]: closure may outlive the current function, but it borrows `prefix`, which is owned by the current function
|
||||
--> $DIR/does-not-live-long-enough.rs:6:33
|
||||
|
|
||||
LL | self.data.iter().filter(|s| s.starts_with(prefix)).map(|s| s.as_ref())
|
||||
| ^^^ ------ `prefix` is borrowed here
|
||||
| |
|
||||
| may outlive borrowed value `prefix`
|
||||
|
|
||||
note: closure is returned here
|
||||
--> $DIR/does-not-live-long-enough.rs:5:55
|
||||
|
|
||||
LL | fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> {
|
||||
| -- lifetime `'a` defined here --------------------------- opaque type requires that `prefix` is borrowed for `'a`
|
||||
LL | self.data.iter().filter(|s| s.starts_with(prefix)).map(|s| s.as_ref())
|
||||
| --- ^^^^^^ borrowed value does not live long enough
|
||||
| |
|
||||
| value captured here
|
||||
LL |
|
||||
LL | }
|
||||
| - `prefix` dropped here while still borrowed
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: to force the closure to take ownership of `prefix` (and any other referenced variables), use the `move` keyword
|
||||
|
|
||||
help: you can add a bound to the opaque type to make it last less than `'static` and match `'a`
|
||||
|
|
||||
LL | fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> + 'a {
|
||||
| ^^^^
|
||||
LL | self.data.iter().filter(move |s| s.starts_with(prefix)).map(|s| s.as_ref())
|
||||
| ^^^^^^^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0597`.
|
||||
For more information about this error, try `rustc --explain E0373`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue