Skip to content

Commit 77dfd71

Browse files
committed
fix 82032
1 parent d416093 commit 77dfd71

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_hir as hir;
22
use rustc_hir::Node;
33
use rustc_index::vec::Idx;
4+
use rustc_middle::hir::map::Map;
45
use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
56
use rustc_middle::ty::{self, Ty, TyCtxt};
67
use rustc_middle::{
@@ -543,13 +544,24 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
543544
// Attempt to search similar mutable associated items for suggestion.
544545
// In the future, attempt in all path but initially for RHS of for_loop
545546
fn suggest_similar_mut_method_for_for_loop(&self, err: &mut DiagnosticBuilder<'_>) {
546-
let hir = self.infcx.tcx.hir();
547-
let node = hir.item(self.mir_hir_id());
548547
use hir::{
549-
Expr,
548+
BodyId, Expr,
550549
ExprKind::{Block, Call, DropTemps, Match, MethodCall},
550+
HirId, ImplItem, ImplItemKind, Item, ItemKind,
551551
};
552-
if let hir::ItemKind::Fn(_, _, body_id) = node.kind {
552+
553+
fn maybe_body_id_of_fn(hir_map: &Map<'tcx>, id: HirId) -> Option<BodyId> {
554+
match hir_map.find(id) {
555+
Some(Node::Item(Item { kind: ItemKind::Fn(_, _, body_id), .. }))
556+
| Some(Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(_, body_id), .. })) => {
557+
Some(*body_id)
558+
}
559+
_ => None,
560+
}
561+
}
562+
let hir_map = self.infcx.tcx.hir();
563+
let mir_body_hir_id = self.mir_hir_id();
564+
if let Some(fn_body_id) = maybe_body_id_of_fn(&hir_map, mir_body_hir_id) {
553565
if let Block(
554566
hir::Block {
555567
expr:
@@ -579,7 +591,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
579591
..
580592
},
581593
_,
582-
) = hir.body(body_id).value.kind
594+
) = hir_map.body(fn_body_id).value.kind
583595
{
584596
let opt_suggestions = path_segment
585597
.hir_id

0 commit comments

Comments
 (0)