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

Compiler incorrectly suggests &mut x instead of &(mut x) in a pattern for Copy types. #122415

Closed
theemathas opened this issue Mar 13, 2024 · 2 comments · Fixed by #122677
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@theemathas
Copy link
Contributor

theemathas commented Mar 13, 2024

Code

fn mutate(_y: &mut i32) {}

fn foo(&x: &i32) {
    mutate(&mut x);
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
 --> src/lib.rs:4:12
  |
4 |     mutate(&mut x);
  |            ^^^^^^ cannot borrow as mutable
  |
help: consider changing this to be mutable
  |
3 | fn foo(&mut x: &i32) {
  |         +++

For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

Instead of suggesting &mut x: &i32, which doesn't compile, the compiler should suggest &(mut x): &i32, which does compile.

Rationale and extra context

This incorrect suggestion occurs when attempting to use a &x pattern to copy a Copy type (i32 in the reproduction code) from a reference, and then mutate the copy. The correct thing to do is to dereference with a & pattern, and assign it to a mut binding, which is written as &(mut x). The compiler incorrectly suggests &mut x, which means something else, and doesn't work.

The original use case that led to this issue had a one-line closure instead of the foo function, which makes doing something like let mut x = x undesirable.

Other cases

No response

Rust Version

1.78.0-nightly (2024-03-11 4a0cc881dcc4d800f106) on the playground

Anything else?

No response

@theemathas theemathas 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 Mar 13, 2024
@theemathas theemathas changed the title Compiler incorrectly suggests &mut x instead of &(mut x) for Copy types. Compiler incorrectly suggests &mut x instead of &(mut x) in a pattern for Copy types. Mar 13, 2024
@fmease
Copy link
Member

fmease commented Mar 13, 2024

Ah, we had a similar but not identical bug report a while ago, #114896, which was fixed by #115595. cc @surechen you might be interested in this :)

@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Mar 13, 2024
@surechen
Copy link
Contributor

surechen commented Mar 13, 2024

Ah, we had a similar but not identical bug report a while ago, #114896, which was fixed by #115595. cc @surechen you might be interested in this :)

Yes. Thank you.

@rustbot claim

surechen added a commit to surechen/rust that referenced this issue Mar 18, 2024
For ref pattern in func param, the mutability suggestion has to apply to the binding.

For example: `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)`

fixes rust-lang#122415
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 19, 2024
Fix incorrect mutable suggestion information for binding in ref pattern.

For ref pattern in func param, the mutability suggestion has to apply to the binding.

For example: `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)`

fixes rust-lang#122415
@bors bors closed this as completed in 19f72df Mar 19, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 19, 2024
Rollup merge of rust-lang#122677 - surechen:fix_122415, r=Nadrieril

Fix incorrect mutable suggestion information for binding in ref pattern.

For ref pattern in func param, the mutability suggestion has to apply to the binding.

For example: `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)`

fixes rust-lang#122415
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants