Closed as duplicate of#88114
Closed as duplicate of#88114
Description
Given the following code:
#![allow(unused)]
// #![warn(rust_2021_incompatible_closure_captures)]
#![feature(capture_disjoint_fields)]
use futures::future::BoxFuture;
use futures::FutureExt;
struct Connect {
host: String,
insecure: bool,
}
pub struct GremlinContext;
pub enum Command {
Print(Option<String>),
Exec(Box<dyn FnOnce(&GremlinContext) -> BoxFuture<'static, Vec<Command>> + Send>),
}
struct ConnectionOptions<'a> {
host: &'a str,
insecure: bool,
}
fn handle(args: Vec<String>) -> Vec<Command> {
let connect = Connect {
host: "".to_string(),
insecure: false,
};
let task = |_ctx: &GremlinContext| {
let future = async move {
let options = ConnectionOptions {
host: connect.host.as_str(),
insecure: connect.insecure,
};
vec![Command::Print(Some("Connected!".into()))]
};
future.boxed()
};
vec![Command::Exec(Box::new(task))]
}
fn main() {}
With feature
disabled, and the warning enabled, there is no warning issued. With the feature enabled, this fails with the error:
error[E0597]: `connect.insecure` does not live long enough
--> src/main.rs:33:27
|
29 | let task = |_ctx: &GremlinContext| {
| ----------------------- value captured here
...
33 | insecure: connect.insecure,
| ^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
40 | vec![Command::Exec(Box::new(task))]
| -------------- cast requires that `connect.insecure` is borrowed for `'static`
41 | }
| - `connect.insecure` dropped here while still borrowed
I think it is missing a suggestion for let _ = &connect
in the closure.
Found in the 2021 crater run for https://crater-reports.s3.amazonaws.com/pr-87190-3/try%23a7a572ce3edd6d476191fbfe92c9c1986e009b34/reg/gremlin-cli-0.1.0/log.txt
rustc 1.56.0-nightly (ac50a5335 2021-08-27)
binary: rustc
commit-hash: ac50a53359328a5d7f2f558833e63d59d372e4f7
commit-date: 2021-08-27
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 13.0.0
cc @rust-lang/wg-rfc-2229