Skip to content

Spurious E0492 with closure capture #112378

Closed
@zirconium-n

Description

@zirconium-n

Code

const fn foo() {
    let i = 0;
    let _ = || {
        i = 1;
    };
}

Current output

error[E0492]: constant functions cannot refer to interior mutable data
 --> src/lib.rs:5:13
  |
5 |       let _ = || {
  |  _____________^
6 | |         i = 1;
7 | |     };
  | |_____^ this borrow of an interior mutable value may end up in the final value

error[E0594]: cannot assign to `i`, as it is not declared as mutable
 --> src/lib.rs:6:9
  |
4 |     let i = 0;
  |         - help: consider changing this to be mutable: `mut i`
5 |     let _ = || {
6 |         i = 1;
  |         ^^^^^ cannot assign

Desired output

error[E0658]: mutable references are not allowed in constant functions
 --> src/lib.rs:5:13
  |
5 |       let _ = || {
  |  _____________^
6 | |         i = 1;
7 | |     };
  | |_____^

error[E0594]: cannot assign to `i`, as it is not declared as mutable
 --> src/lib.rs:6:9
  |
4 |     let i = 0;
  |         - help: consider changing this to be mutable: `mut i`
5 |     let _ = || {
6 |         i = 1;
  |         ^^^^^ cannot assign

Rationale and extra context

Apparently there's no interior mutability involved. So we should just output we cannot mutable borrow i in const context.

Incidentally(?) fixed by #112119. Still needs test though.

@rustbot label E-needs-test

Other cases

No response

Anything else?

rustc version

rustc +stable --version
rustc 1.70.0 (90c541806 2023-05-31)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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