|
1 | 1 | use rustc_hir as hir; |
2 | 2 | use rustc_hir::Node; |
3 | 3 | use rustc_index::vec::Idx; |
| 4 | +use rustc_middle::hir::map::Map; |
4 | 5 | use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem}; |
5 | 6 | use rustc_middle::ty::{self, Ty, TyCtxt}; |
6 | 7 | use rustc_middle::{ |
@@ -543,13 +544,24 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { |
543 | 544 | // Attempt to search similar mutable associated items for suggestion. |
544 | 545 | // In the future, attempt in all path but initially for RHS of for_loop |
545 | 546 | 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()); |
548 | 547 | use hir::{ |
549 | | - Expr, |
| 548 | + BodyId, Expr, |
550 | 549 | ExprKind::{Block, Call, DropTemps, Match, MethodCall}, |
| 550 | + HirId, ImplItem, ImplItemKind, Item, ItemKind, |
551 | 551 | }; |
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) { |
553 | 565 | if let Block( |
554 | 566 | hir::Block { |
555 | 567 | expr: |
@@ -579,7 +591,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { |
579 | 591 | .. |
580 | 592 | }, |
581 | 593 | _, |
582 | | - ) = hir.body(body_id).value.kind |
| 594 | + ) = hir_map.body(fn_body_id).value.kind |
583 | 595 | { |
584 | 596 | let opt_suggestions = path_segment |
585 | 597 | .hir_id |
|
0 commit comments