Skip to content

Commit

Permalink
Fix span for IndexMut method call on HashMap/BTreeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Dec 31, 2024
1 parent 6a3474e commit f28e13b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
self.suggested = true;
} else if let hir::ExprKind::MethodCall(_path, receiver, _, sp) = expr.kind
&& let hir::ExprKind::Index(val, index, _) = receiver.kind
&& expr.span == self.assign_span
&& receiver.span == self.assign_span
{
// val[index].path(args..);
self.err.multipart_suggestion(
Expand All @@ -639,7 +639,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
index.span.shrink_to_hi().with_hi(receiver.span.hi()),
") { val".to_string(),
),
(sp.shrink_to_hi(), "); }".to_string()),
(sp.shrink_to_hi(), "; }".to_string()),
],
Applicability::MachineApplicable,
);
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/borrowck/index-mut-help.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ 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
help: to modify a `HashMap<&str, String>` use `.get_mut()`
|
LL | if let Some(val) = map.get_mut("peter") { val.clear(); };
| ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~ +++

error[E0594]: cannot assign to data in an index of `HashMap<&str, String>`
--> $DIR/index-mut-help.rs:11:5
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/issues/issue-41726.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ 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
help: to modify a `HashMap<String, Vec<String>>` use `.get_mut()`
|
LL | if let Some(val) = things.get_mut(src.as_str()) { val.sort(); };
| ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~ +++

error: aborting due to 1 previous error

Expand Down

0 comments on commit f28e13b

Please sign in to comment.