Skip to content

rustc suggests a syntax error (for x in &mut mut y) #67683

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

Closed
davidben opened this issue Dec 28, 2019 · 1 comment
Closed

rustc suggests a syntax error (for x in &mut mut y) #67683

davidben opened this issue Dec 28, 2019 · 1 comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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

@davidben
Copy link

This may be a duplicate of issue #57431; I don't understand Rust well enough yet to be sure.

When using chunks_exact instead of chunks_exact_mut and then mutating through the returned slices, rustc complains and suggests iterating over &mut mut chunks in the loop, which appears to be a syntax error.

I tried this code:

fn main() {
    let mut v = vec![1, 2, 3, 4, 5, 6, 7];
    let mut chunks = v.chunks_exact(2);
    for chunk in &mut chunks {
        chunk[0] += 1;
    }
    println!("v = {:?}; remainder = {:?}", v, chunks.remainder());
}

I expected to see this happen:

The actual bug is chunks_exact should be chunks_exact_mut (and then switch remainder to into_remainder to match). That's probably a tall order for the compiler to suggest, but the error message should definitely not suggest a syntax error.

Instead, this happened:

warning: variable does not need to be mutable
 --> src/main.rs:2:9
  |
2 |     let mut v = vec![1, 2, 3, 4, 5, 6, 7];
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` on by default

error[E0594]: cannot assign to `chunk[_]` which is behind a `&` reference
 --> src/main.rs:5:9
  |
4 |     for chunk in &mut chunks {
  |                  ----------- help: consider changing this to be a mutable reference: `&mut mut chunks`
5 |         chunk[0] += 1;
  |         ^^^^^^^^^^^^^ `chunk` is a `&` reference, so the data it refers to cannot be written

error: aborting due to previous error

Applying the suggestion results in a syntax error:

error: expected expression, found keyword `mut`
 --> src/main.rs:4:23
  |
4 |     for chunk in &mut mut chunks {
  |                       ^^^ expected expression

error: aborting due to previous error

Meta

rustc --version --verbose:
rustc 1.40.0 (73528e3 2019-12-16)
binary: rustc
commit-hash: 73528e3
commit-date: 2019-12-16
host: x86_64-unknown-linux-gnu
release: 1.40.0
LLVM version: 9.0

@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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. labels Dec 28, 2019
@rylev
Copy link
Member

rylev commented Dec 18, 2020

This no longer reproduces. Running on 1.48.0 I get the following diagnostics:

warning: variable does not need to be mutable
 --> src/main.rs:2:9
  |
2 |     let mut v = vec![1, 2, 3, 4, 5, 6, 7];
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: `#[warn(unused_mut)]` on by default

error[E0594]: cannot assign to `chunk[_]` which is behind a `&` reference
 --> src/main.rs:5:9
  |
4 |     for chunk in &mut chunks {
  |                  ----------- this iterator yields `&` references
5 |         chunk[0] += 1;
  |         ^^^^^^^^^^^^^ `chunk` is a `&` reference, so the data it refers to cannot be written

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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

No branches or pull requests

4 participants