@@ -682,23 +682,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
682682 } ;
683683
684684 let encl_item_id = self . tcx . hir ( ) . get_parent_item ( expr. hir_id ) ;
685- let encl_item = self . tcx . hir ( ) . expect_item ( encl_item_id) ;
686685
687- if let hir:: ItemKind :: Fn ( ..) = encl_item. kind {
688- // We are inside a function body, so reporting "return statement
689- // outside of function body" needs an explanation.
686+ // Somewhat confusingly, get_parent_item() does not necessarily return an
687+ // item -- it can also return a Foreign-/Impl-/TraitItem or a Crate (see
688+ // issue #86721). If it does, we still report the same error.
689+ if let Some ( hir:: Node :: Item ( encl_item) ) = self . tcx . hir ( ) . find ( encl_item_id) {
690+ if let hir:: ItemKind :: Fn ( ..) = encl_item. kind {
691+ // We are inside a function body, so reporting "return statement
692+ // outside of function body" needs an explanation.
690693
691- let encl_body_owner_id = self . tcx . hir ( ) . enclosing_body_owner ( expr. hir_id ) ;
694+ let encl_body_owner_id = self . tcx . hir ( ) . enclosing_body_owner ( expr. hir_id ) ;
692695
693- // If this didn't hold, we would not have to report an error in
694- // the first place.
695- assert_ne ! ( encl_item_id, encl_body_owner_id) ;
696+ // If this didn't hold, we would not have to report an error in
697+ // the first place.
698+ assert_ne ! ( encl_item_id, encl_body_owner_id) ;
696699
697- let encl_body_id = self . tcx . hir ( ) . body_owned_by ( encl_body_owner_id) ;
698- let encl_body = self . tcx . hir ( ) . body ( encl_body_id) ;
700+ let encl_body_id = self . tcx . hir ( ) . body_owned_by ( encl_body_owner_id) ;
701+ let encl_body = self . tcx . hir ( ) . body ( encl_body_id) ;
699702
700- err. encl_body_span = Some ( encl_body. value . span ) ;
701- err. encl_fn_span = Some ( encl_item. span ) ;
703+ err. encl_body_span = Some ( encl_body. value . span ) ;
704+ err. encl_fn_span = Some ( encl_item. span ) ;
705+ }
702706 }
703707
704708 self . tcx . sess . emit_err ( err) ;
0 commit comments