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

fix ice (#82032) #82056

Merged
merged 2 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_hir as hir;
use rustc_hir::Node;
use rustc_index::vec::Idx;
use rustc_middle::hir::map::Map;
use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::{
Expand Down Expand Up @@ -543,13 +544,24 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
// Attempt to search similar mutable associated items for suggestion.
// In the future, attempt in all path but initially for RHS of for_loop
fn suggest_similar_mut_method_for_for_loop(&self, err: &mut DiagnosticBuilder<'_>) {
let hir = self.infcx.tcx.hir();
let node = hir.item(self.mir_hir_id());
use hir::{
Expr,
BodyId, Expr,
ExprKind::{Block, Call, DropTemps, Match, MethodCall},
HirId, ImplItem, ImplItemKind, Item, ItemKind,
};
if let hir::ItemKind::Fn(_, _, body_id) = node.kind {

fn maybe_body_id_of_fn(hir_map: &Map<'tcx>, id: HirId) -> Option<BodyId> {
match hir_map.find(id) {
Some(Node::Item(Item { kind: ItemKind::Fn(_, _, body_id), .. }))
| Some(Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(_, body_id), .. })) => {
Some(*body_id)
}
_ => None,
}
}
let hir_map = self.infcx.tcx.hir();
let mir_body_hir_id = self.mir_hir_id();
if let Some(fn_body_id) = maybe_body_id_of_fn(&hir_map, mir_body_hir_id) {
if let Block(
hir::Block {
expr:
Expand Down Expand Up @@ -579,7 +591,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
..
},
_,
) = hir.body(body_id).value.kind
) = hir_map.body(fn_body_id).value.kind
{
let opt_suggestions = path_segment
.hir_id
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/borrowck/issue-82032.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::{fs, io::*};
use std::collections::HashMap;

type Handle = BufWriter<fs::File>;
struct Thing(HashMap<String, Handle>);

impl Thing {
pub fn die_horribly(&mut self) {
for v in self.0.values() {
v.flush();
//~^ ERROR cannot borrow
}
}
}

fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/borrowck/issue-82032.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0596]: cannot borrow `*v` as mutable, as it is behind a `&` reference
--> $DIR/issue-82032.rs:10:13
|
LL | for v in self.0.values() {
| ---------------
| | |
| | help: use mutable method: `values_mut()`
| this iterator yields `&` references
LL | v.flush();
| ^ `v` 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`.