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 suggests "&mut mut" #69789

Closed
matthiaskrgr opened this issue Mar 6, 2020 · 1 comment · Fixed by #70264
Closed

compiler suggests "&mut mut" #69789

matthiaskrgr opened this issue Mar 6, 2020 · 1 comment · Fixed by #70264
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Mar 6, 2020

I was messing around with this function

    fn build_sidebar_items(&self, m: &clean::Module) -> BTreeMap<String, Vec<NameDoc>> {
        // BTreeMap instead of HashMap to get a sorted output
        let mut map: BTreeMap<_, Vec<_>> = BTreeMap::new();
        for item in &m.items {
            if item.is_stripped() {
                continue;
            }

            let short = item.type_();
            let myname = match item.name {
                None => continue,
                Some(ref s) => s.to_string(),
            };
            let short = short.to_string();
            map.entry(short)
                .or_default()
                .push((myname, Some(plain_summary_line(item.doc_value()))));
        }

        if self.shared.sort_modules_alphabetically {
            for (_, items) in &mut map {
                items.sort();
            }
        }
        map
    }

After I changed the last block to

        if self.shared.sort_modules_alphabetically {
            for items in &mut map.values() {
                items.sort();
            }
        }
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -1542,7 +1542,7 @@ impl Context {
         }
 
         if self.shared.sort_modules_alphabetically {
-            for (_, items) in &mut map {
+            for items in &mut map.values() {
                 items.sort();
             }
         }

The compiler gave this interesting but wrong suggestion: &mut mut map.values()

error[E0596]: cannot borrow `*items` as mutable, as it is behind a `&` reference
    --> src/librustdoc/html/render.rs:1546:17
     |
1545 |             for items in &mut map.values() {
     |                          ----------------- help: consider changing this to be a mutable reference: `&mut mut map.values()`
1546 |                 items.sort();
     |                 ^^^^^ `items` 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`.

which does not compiler obviously 😄

The correct solution would be to use &mut map.values_mut():

    if self.shared.sort_modules_alphabetically {
            for items in &mut map.values_mut() {
                items.sort();
            }
        }
```
@matthiaskrgr matthiaskrgr added the C-bug Category: This is a bug. label Mar 6, 2020
@jonas-schievink jonas-schievink added 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. labels Mar 6, 2020
@CAD97
Copy link
Contributor

CAD97 commented Mar 8, 2020

Just a minor note: typically these iterators are iterated by-value (e.g. for items in map.values()), so this explains why the diagnostics issue was missed. This gives a less bad error message:

error[E0596]: cannot borrow `*items` as mutable, as it is behind a `&` reference
  --> src/main.rs:10:9
   |
9  |     for items in map.values() {
   |         ----- help: consider changing this to be a mutable reference: `&mut std::vec::Vec<u8>`
10 |         items.sort();
   |         ^^^^^ `items` is a `&` reference, so the data it refers to cannot be borrowed as mutable

Centril added a commit to Centril/rust that referenced this issue Mar 23, 2020
…r=estebank

Fix invalid suggestion on `&mut` iterators yielding `&` references

Fixes rust-lang#69789.

rustc suggested an invalid code when `&` reference from `&mut` iterator is mutated. The compiler knew we're mutating a value behind `&` reference, but as the assignment RHS is from desugaring, it could only see the iterator expression from source and inserted `mut` there.

r? @estebank
Centril added a commit to Centril/rust that referenced this issue Mar 23, 2020
…r=estebank

Fix invalid suggestion on `&mut` iterators yielding `&` references

Fixes rust-lang#69789.

rustc suggested an invalid code when `&` reference from `&mut` iterator is mutated. The compiler knew we're mutating a value behind `&` reference, but as the assignment RHS is from desugaring, it could only see the iterator expression from source and inserted `mut` there.

r? @estebank
@bors bors closed this as completed in ab2817b Mar 24, 2020
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` 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