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

Rust gives wrong help. (&mut mut Vec<T>) #85765

Closed
TornaxO7 opened this issue May 27, 2021 · 1 comment · Fixed by #85823
Closed

Rust gives wrong help. (&mut mut Vec<T>) #85765

TornaxO7 opened this issue May 27, 2021 · 1 comment · Fixed by #85823
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. 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

@TornaxO7
Copy link

TornaxO7 commented May 27, 2021

So this is the minimal code:

fn main() {
    let mut test = Vec::new();

    let rofl: &Vec<Vec<i32>> = &mut test;

    rofl.push(Vec::new());
}

If I'm running cargo check on this, than I'm getting the following:

    Checking rust_tmp v0.1.0 (/tmp/rust_tmp)
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
 --> src/main.rs:6:5
  |
4 |     let rofl: &Vec<Vec<i32>> = &mut test;
  |                                --------- help: consider changing this to be a mutable reference: `&mut mut test`
5 |
6 |     rofl.push(Vec::new());
  |     ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
error: could not compile `rust_tmp`

To learn more, run the command again with --verbose.

[Process exited 101]

So the interesting part is this one:

  |   --------- help: consider changing this to be a mutable reference: `&mut mut test`

If I try this:

fn main() {
    let mut test = Vec::new();

    // Notice the second "mut" after the "="
    let rofl: &Vec<Vec<i32>> = &mut mut test;

    rofl.push(Vec::new());
}

Than I'm getting this when running cargo check:

    Checking rust_tmp v0.1.0 (/tmp/rust_tmp)
error: expected expression, found keyword `mut`
 --> src/main.rs:5:37
  |
5 |     let rofl: &Vec<Vec<i32>> = &mut mut test;
  |                                     ^^^ expected expression

error: aborting due to previous error

error: could not compile `rust_tmp`

To learn more, run the command again with --verbose.

[Process exited 101]

Well I expected that rust would say, that the given type of rofl is wrong or doesn't fit with the value which is gonna be assigned. Something like this:

    Checking rust_tmp v0.1.0 (/tmp/rust_tmp)
error[E0596]: cannot borrow `*rofl` as mutable, as it is behind a `&` reference
 --> src/main.rs:7:5
  |
5 |     let rofl: &Vec<Vec<i32>> = &mut test;
  |               --- help: consider changing this to be a mutable reference:
  |                         `&mut Vec<Vec<i32>>`
6 |
7 |     rofl.push(Vec::new());
  |     ^^^^ `rofl` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
error: could not compile `rust_tmp`

To learn more, run the command again with --verbose.

[Process exited 101]

Meta

rustc --version --verbose:

rustc 1.54.0-nightly (5c0292654 2021-05-11)
binary: rustc
commit-hash: 5c029265465301fe9cb3960ce2a5da6c99b8dcf2
commit-date: 2021-05-11
host: x86_64-unknown-linux-gnu
release: 1.54.0-nightly
LLVM version: 12.0.1
Backtrace

It didn't even build, so I think it's fine if I don't fill this part.

@TornaxO7 TornaxO7 added the C-bug Category: This is a bug. label May 27, 2021
@TornaxO7 TornaxO7 changed the title Rust anayses code wrong. (&mut mut Vec<T>) Rust gives wrong help. (&mut mut Vec<T>) May 27, 2021
@LeSeulArtichaut LeSeulArtichaut added A-diagnostics Area: Messages for errors, warnings, and lints 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. labels May 28, 2021
@fee1-dead
Copy link
Member

@rustbot claim

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 C-bug Category: This is a bug. 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