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

Bogus "consider specifying this binding's type" when reference differs in mutability #113767

Open
lukas-code opened this issue Jul 16, 2023 · 5 comments
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-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lukas-code
Copy link
Member

lukas-code commented Jul 16, 2023

Code

fn get() -> &'static Vec<i32> {
    todo!()
}

fn main() {
    let x = get();
    x.push(1);
}

Current output

error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
 --> src/main.rs:7:5
  |
7 |     x.push(1);
  |     ^^^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
  |
help: consider specifying this binding's type
  |
6 |     let x: &mut Vec<i32> = get();
  |          +++++++++++++++

Desired output

error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
 --> src/main.rs:7:5
  |
7 |     x.push(1);
  |     ^^^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
  |

Rationale and extra context

The type is defined as immutable reference by the return type of the function and specifying it as a mutable reference in the let binding won't help.

Other cases

This seems to happen only if

  • the immutable reference doesn't come from a local borrow (eg. let x = &vec![];)
  • the variable x goes through auto(de)ref with a method call or field access (eg. x.field = 1)

Anything else?

@rustbot label A-suggestion-diagnostics D-incorrect

@lukas-code lukas-code 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 Jul 16, 2023
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Jul 16, 2023
@compiler-errors compiler-errors removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 16, 2023
@jmintb
Copy link
Contributor

jmintb commented Jul 23, 2023

Can I try to fix this? :)

awaitlink referenced this issue in awaitlink/signalupdates-bot Jul 24, 2023
@awaitlink
Copy link
Contributor

It seems this can happen in function arguments too, where the suggestion also results in invalid syntax.

If the &mut in this function...

pub async fn json_from_response<T: DeserializeOwned>(response: &mut Response) -> anyhow::Result<T> {
    response
        .json()
        .await
        .map_err(|e| anyhow!(e.to_string()))
        .context("could not get JSON")
}

...is replaced by & (e.g. per the invalid clippy suggestion rust-lang/rust-clippy#11199, which is a separate issue)...

Invalid clippy suggestion for completeness
warning: this argument is a mutable reference, but not used mutably
  --> src/network.rs:46:64
   |
46 | pub async fn json_from_response<T: DeserializeOwned>(response: &mut Response) -> anyhow::Result<T> {
   |                                                                ^^^^^^^^^^^^^ help: consider changing to: `&Response`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
   = note: `#[warn(clippy::needless_pass_by_ref_mut)]` on by default

warning: `signalupdates-bot` (lib) generated 1 warning

...that results in:

error[E0596]: cannot borrow `*response` as mutable, as it is behind a `&` reference
  --> src/network.rs:47:5
   |
47 |     response
   |     ^^^^^^^^ `response` is a `&` reference, so the data it refers to cannot be borrowed as mutable
   |
help: consider specifying this binding's type
   |
46 | pub async fn json_from_response<T: DeserializeOwned>(response: &mut worker::Response: &Response) -> anyhow::Result<T> {
   |                                                              +++++++++++++++++++++++

which suggests adding a second type to the function argument.

Sorry for non-minimal example / if it's a different issue.

@lukas-code
Copy link
Member Author

It seems this can happen in function arguments too, where the suggestion also results in invalid syntax.

It looks like this happens only for async fns:

async fn foo(x: &Vec<i32>) {
    x.push(42);
}
error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
 --> src/lib.rs:2:5
  |
2 |     x.push(42);
  |     ^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
  |
help: consider specifying this binding's type
  |
1 | async fn foo(x: &mut Vec<i32>: &Vec<i32>) {
  |               +++++++++++++++

This makes sense, because the async fn gets desugared to this:

fn foo(x: &Vec<i32>) -> impl core::future::Future<Output = ()> + '_ {
    async move {
        let x = x; // <-- notice let binding here
        x.push(42);
    }
}

... and the compiler then tries to help by suggesting let x: &mut Vec<i32> = x;, similar to the original issue.


Can I try to fix this? :)

Yes, absolutely, feel free to work on this. Be sure to check out the dev guide and don't hesitate to ask in the help channel on zulip if you have any questions.

@jmintb
Copy link
Contributor

jmintb commented Jul 26, 2023

Awesome! thanks 🦀

@jmintb
Copy link
Contributor

jmintb commented Aug 2, 2023

@rustbot claim

jmintb added a commit to jmintb/rust that referenced this issue Sep 7, 2023
issue: rust-lang#113767

This commit disables the "consider changing this bindings type"
suggestion in cases where the initial value of a binding is not mutable
and the suggestion is therefore invalid.
jmintb added a commit to jmintb/rust that referenced this issue Sep 12, 2023
issue: rust-lang#113767

This commit disables the "consider changing this bindings type"
suggestion in cases where the initial value of a binding is not mutable
and the suggestion is therefore invalid.
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-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

5 participants