Skip to content

rust-2021-incompatible-closure-captures fires in 2021 edition code #101284

Closed
@lnicola

Description

@lnicola

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=92d18abb330e3fbc8a1425f3458830da

#![warn(rust_2021_compatibility)]
use std::sync::Arc;

pub struct Warns {
    _test: Arc<String>, // Removing this Arc results in no warning
    foo: String,
}

impl Warns {
    pub fn test(self) -> std::io::Result<()> {
        let closure = move || {
            let _ = self.foo;
            Ok(())
        };
        closure()
    }
}

The current output is:

warning: changes to closure capture in Rust 2021 will affect drop order
  --> src/lib.rs:11:23
   |
11 |         let closure = move || {
   |                       ^^^^^^^
12 |             let _ = self.foo;
   |                     ---- in Rust 2018, this causes the closure to capture `self`, but in Rust 2021, it has no effect
...
16 |     }
   |     - in Rust 2018, `self` is dropped here along with the closure, but in Rust 2021 `self` is not part of the closure
   |
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(rust_2021_compatibility)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^
   = note: `#[warn(rust_2021_incompatible_closure_captures)]` implied by `#[warn(rust_2021_compatibility)]`
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
help: add a dummy let to cause `self` to be fully captured
   |
11 ~         let closure = move || {
12 +             let _ = &self;
   |

warning: `playground` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.52s

Ideally the output should look like:

[no warnings]

The code is already being compiled for the 2021 edition, there's no need to issue any warnings about behaviour that was different in 2018.
The compiler should assume that the code is working as expected.

This might affect other warnings in the same category, I'm not sure.

Tested in 1.63, beta and nightly.

Metadata

Metadata

Assignees

Labels

A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-edition-2021Area: The 2021 editionA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.D-editionDiagnostics: An error or lint that should account for edition differences.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