Skip to content

Commit

Permalink
Rollup merge of rust-lang#100906 - ChayimFriedman2:map-index-mut, r=d…
Browse files Browse the repository at this point in the history
…avidtwco

Suggest alternatives when trying to mutate a `HashMap`/`BTreeMap` via indexing

The error can be quite confusing to newcomers.

Fixes rust-lang#100873.

I'm not so sure about the message, open to wording suggestions.
  • Loading branch information
matthiaskrgr authored Aug 24, 2022
2 parents b0b4c4f + b0255a1 commit 6623333
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
19 changes: 17 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::Node;
use rustc_middle::hir::map::Map;
Expand All @@ -12,12 +13,11 @@ use rustc_middle::{
};
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::{BytePos, Span};
use rustc_span::{sym, BytePos, Span};

use crate::diagnostics::BorrowedContentSource;
use crate::MirBorrowckCtxt;
use rustc_const_eval::util::collect_writes::FindAssignments;
use rustc_errors::{Applicability, Diagnostic};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum AccessKind {
Expand Down Expand Up @@ -614,6 +614,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
"trait `IndexMut` is required to modify indexed content, \
but it is not implemented for `{ty}`",
));
self.suggest_map_index_mut_alternatives(ty, &mut err);
}
_ => (),
}
Expand All @@ -627,6 +628,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
self.buffer_error(err);
}

fn suggest_map_index_mut_alternatives(
&self,
ty: Ty<'_>,
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
) {
let Some(adt) = ty.ty_adt_def() else { return };
let did = adt.did();
if self.infcx.tcx.is_diagnostic_item(sym::HashMap, did)
|| self.infcx.tcx.is_diagnostic_item(sym::BTreeMap, did)
{
err.help(format!("to modify a `{ty}`, use `.get_mut()`, `.insert()` or the entry API"));
}
}

/// User cannot make signature of a trait mutable without changing the
/// trait. So we find if this error belongs to a trait and if so we move
/// suggestion to the trait or disable it if it is out of scope of this crate
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/borrowck/index-mut-help.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | map["peter"].clear();
| ^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<&str, String>`
= help: to modify a `HashMap<&str, String>`, use `.get_mut()`, `.insert()` or the entry API

error[E0594]: cannot assign to data in an index of `HashMap<&str, String>`
--> $DIR/index-mut-help.rs:12:5
Expand All @@ -13,6 +14,7 @@ LL | map["peter"] = "0".to_string();
| ^^^^^^^^^^^^ cannot assign
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<&str, String>`
= help: to modify a `HashMap<&str, String>`, use `.get_mut()`, `.insert()` or the entry API

error[E0596]: cannot borrow data in an index of `HashMap<&str, String>` as mutable
--> $DIR/index-mut-help.rs:13:13
Expand All @@ -21,6 +23,7 @@ LL | let _ = &mut map["peter"];
| ^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<&str, String>`
= help: to modify a `HashMap<&str, String>`, use `.get_mut()`, `.insert()` or the entry API

error: aborting due to 3 previous errors

Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/btreemap/btreemap-index-mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::collections::BTreeMap;

fn main() {
let mut map = BTreeMap::<u32, u32>::new();
map[&0] = 1; //~ ERROR cannot assign
}
12 changes: 12 additions & 0 deletions src/test/ui/btreemap/btreemap-index-mut.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0594]: cannot assign to data in an index of `BTreeMap<u32, u32>`
--> $DIR/btreemap-index-mut.rs:5:5
|
LL | map[&0] = 1;
| ^^^^^^^^^^^ cannot assign
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `BTreeMap<u32, u32>`
= help: to modify a `BTreeMap<u32, u32>`, use `.get_mut()`, `.insert()` or the entry API

error: aborting due to previous error

For more information about this error, try `rustc --explain E0594`.
6 changes: 6 additions & 0 deletions src/test/ui/hashmap/hashmap-index-mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::collections::HashMap;

fn main() {
let mut map = HashMap::<u32, u32>::new();
map[&0] = 1; //~ ERROR cannot assign
}
12 changes: 12 additions & 0 deletions src/test/ui/hashmap/hashmap-index-mut.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0594]: cannot assign to data in an index of `HashMap<u32, u32>`
--> $DIR/hashmap-index-mut.rs:5:5
|
LL | map[&0] = 1;
| ^^^^^^^^^^^ cannot assign
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<u32, u32>`
= help: to modify a `HashMap<u32, u32>`, use `.get_mut()`, `.insert()` or the entry API

error: aborting due to previous error

For more information about this error, try `rustc --explain E0594`.
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-41726.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | things[src.as_str()].sort();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<String, Vec<String>>`
= help: to modify a `HashMap<String, Vec<String>>`, use `.get_mut()`, `.insert()` or the entry API

error: aborting due to previous error

Expand Down

0 comments on commit 6623333

Please sign in to comment.