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

consider changing this to be mutable suggested for a binding in a ref pattern #114896

Closed
Alexendoo opened this issue Aug 16, 2023 · 2 comments · Fixed by #115595
Closed

consider changing this to be mutable suggested for a binding in a ref pattern #114896

Alexendoo opened this issue Aug 16, 2023 · 2 comments · Fixed by #115595
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

@Alexendoo
Copy link
Member

Alexendoo commented Aug 16, 2023

Code

fn x(a: &char) {
    let &b = a;
    b.make_ascii_uppercase();
}

Current output

error[E0596]: cannot borrow `b` as mutable, as it is not declared as mutable
 --> src/lib.rs:3:5
  |
3 |     b.make_ascii_uppercase();
  |     ^ cannot borrow as mutable
  |
help: consider changing this to be mutable
  |
2 |     let &mut b = a;
  |          +++

Desired output

No response

Rationale and extra context

Applying the suggestion gives

error[[E0308]](https://doc.rust-lang.org/nightly/error_codes/E0308.html): mismatched types
 --> src/lib.rs:2:9
  |
2 |     let &mut b = a;
  |         ^^^^^^   - this expression has type `&char`
  |         |
  |         types differ in mutability
  |         help: to declare a mutable variable use: `mut b`
  |
  = note:      expected reference `&char`
          found mutable reference `&mut _`

Other cases

No response

Anything else?

No response

@Alexendoo Alexendoo 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 Aug 16, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 16, 2023
@fmease
Copy link
Member

fmease commented Aug 16, 2023

It should suggest let &(mut b) = a; instead.
Looks odd but it's correct.

@rustbot label -needs-triage A-suggestion-diagnostics D-invalid-suggestion

@rustbot rustbot added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 16, 2023
@surechen
Copy link
Contributor

surechen commented Sep 4, 2023

Hi, I'm interested in this issue and want to try to fix it.
@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Sep 11, 2023
Fix incorrect mutable suggestion information for binding in ref pattern like:  `let &b = a;`

fixes rust-lang#114896

I find we have to get pat_span but not local_decl.source_info.span for suggestion. In `let &b = a;`  pat_span is &b. I think check `let &b = a` in hir to make sure it is hir::Node::Local(hir::Local {pat: hir::Pat{kind: hir::PatKind::Ref(.......   can distinguish it from other situation, but I'm not sure.

If my processing method is not accurate, please guide me to modify it, thank you.

r? `@davidtwco`
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 11, 2023
Fix incorrect mutable suggestion information for binding in ref pattern like:  `let &b = a;`

fixes rust-lang#114896

I find we have to get pat_span but not local_decl.source_info.span for suggestion. In `let &b = a;`  pat_span is &b. I think check `let &b = a` in hir to make sure it is hir::Node::Local(hir::Local {pat: hir::Pat{kind: hir::PatKind::Ref(.......   can distinguish it from other situation, but I'm not sure.

If my processing method is not accurate, please guide me to modify it, thank you.

r? `@davidtwco`
@bors bors closed this as completed in a8f3c76 Sep 11, 2023
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
4 participants