Skip to content

missing rust_2021_incompatible_closure_captures with async block boxed future thingy #88444

Closed as duplicate of#88114
@ehuss

Description

@ehuss

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

Metadata

Metadata

Assignees

Labels

A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-edition-2021Area: The 2021 editionA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions