Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spurious E0492 with closure capture #112378

Closed
zirconium-n opened this issue Jun 7, 2023 · 2 comments
Closed

Spurious E0492 with closure capture #112378

zirconium-n opened this issue Jun 7, 2023 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zirconium-n
Copy link
Contributor

zirconium-n commented Jun 7, 2023

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)
@zirconium-n zirconium-n added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 7, 2023
@rustbot rustbot added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jun 7, 2023
@zirconium-n
Copy link
Contributor Author

@rustbot claim

@zirconium-n
Copy link
Contributor Author

Actually I feel mentioning mutable reference is still pretty weird, since it's hidden under closure capture. At least it's better than non-existent interior mutability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants