@@ -33,7 +33,7 @@ use rustc_middle::{bug, span_bug};
33
33
use rustc_session:: lint;
34
34
use rustc_span:: def_id:: LocalDefId ;
35
35
use rustc_span:: hygiene:: DesugaringKind ;
36
- use rustc_span:: symbol:: { kw, sym, Ident } ;
36
+ use rustc_span:: symbol:: { kw, sym} ;
37
37
use rustc_span:: Span ;
38
38
use rustc_target:: abi:: FieldIdx ;
39
39
use rustc_trait_selection:: traits:: error_reporting:: TypeErrCtxtExt as _;
@@ -867,76 +867,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
867
867
)
868
868
}
869
869
870
- /// Given a function `Node`, return its `HirId` and `FnDecl` if it exists. Given a closure
871
- /// that is the child of a function, return that function's `HirId` and `FnDecl` instead.
872
- /// This may seem confusing at first, but this is used in diagnostics for `async fn`,
873
- /// for example, where most of the type checking actually happens within a nested closure,
874
- /// but we often want access to the parent function's signature.
875
- ///
876
- /// Otherwise, return false.
877
- pub ( crate ) fn get_node_fn_decl (
878
- & self ,
879
- node : Node < ' tcx > ,
880
- ) -> Option < ( LocalDefId , & ' tcx hir:: FnDecl < ' tcx > , Ident , bool ) > {
881
- match node {
882
- Node :: Item ( & hir:: Item {
883
- ident,
884
- kind : hir:: ItemKind :: Fn ( ref sig, ..) ,
885
- owner_id,
886
- ..
887
- } ) => {
888
- // This is less than ideal, it will not suggest a return type span on any
889
- // method called `main`, regardless of whether it is actually the entry point,
890
- // but it will still present it as the reason for the expected type.
891
- Some ( ( owner_id. def_id , & sig. decl , ident, ident. name != sym:: main) )
892
- }
893
- Node :: TraitItem ( & hir:: TraitItem {
894
- ident,
895
- kind : hir:: TraitItemKind :: Fn ( ref sig, ..) ,
896
- owner_id,
897
- ..
898
- } ) => Some ( ( owner_id. def_id , & sig. decl , ident, true ) ) ,
899
- Node :: ImplItem ( & hir:: ImplItem {
900
- ident,
901
- kind : hir:: ImplItemKind :: Fn ( ref sig, ..) ,
902
- owner_id,
903
- ..
904
- } ) => Some ( ( owner_id. def_id , & sig. decl , ident, false ) ) ,
905
- Node :: Expr ( & hir:: Expr {
906
- hir_id,
907
- kind :
908
- hir:: ExprKind :: Closure ( hir:: Closure {
909
- kind : hir:: ClosureKind :: Coroutine ( ..) , ..
910
- } ) ,
911
- ..
912
- } ) => {
913
- let ( ident, sig, owner_id) = match self . tcx . parent_hir_node ( hir_id) {
914
- Node :: Item ( & hir:: Item {
915
- ident,
916
- kind : hir:: ItemKind :: Fn ( ref sig, ..) ,
917
- owner_id,
918
- ..
919
- } ) => ( ident, sig, owner_id) ,
920
- Node :: TraitItem ( & hir:: TraitItem {
921
- ident,
922
- kind : hir:: TraitItemKind :: Fn ( ref sig, ..) ,
923
- owner_id,
924
- ..
925
- } ) => ( ident, sig, owner_id) ,
926
- Node :: ImplItem ( & hir:: ImplItem {
927
- ident,
928
- kind : hir:: ImplItemKind :: Fn ( ref sig, ..) ,
929
- owner_id,
930
- ..
931
- } ) => ( ident, sig, owner_id) ,
932
- _ => return None ,
933
- } ;
934
- Some ( ( owner_id. def_id , & sig. decl , ident, ident. name != sym:: main) )
935
- }
936
- _ => None ,
937
- }
938
- }
939
-
940
870
/// Given a `HirId`, return the `HirId` of the enclosing function, its `FnDecl`, and whether a
941
871
/// suggestion can be made, `None` otherwise.
942
872
pub fn get_fn_decl (
@@ -945,10 +875,73 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
945
875
) -> Option < ( LocalDefId , & ' tcx hir:: FnDecl < ' tcx > , bool ) > {
946
876
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
947
877
// `while` before reaching it, as block tail returns are not available in them.
948
- self . tcx . hir ( ) . get_return_block ( blk_id) . and_then ( |blk_id| {
949
- let parent = self . tcx . hir_node ( blk_id) ;
950
- self . get_node_fn_decl ( parent)
951
- . map ( |( fn_id, fn_decl, _, is_main) | ( fn_id, fn_decl, is_main) )
878
+ self . tcx . hir ( ) . get_fn_id_for_return_block ( blk_id) . and_then ( |item_id| {
879
+ match self . tcx . hir_node ( item_id) {
880
+ Node :: Item ( & hir:: Item {
881
+ ident,
882
+ kind : hir:: ItemKind :: Fn ( ref sig, ..) ,
883
+ owner_id,
884
+ ..
885
+ } ) => {
886
+ // This is less than ideal, it will not suggest a return type span on any
887
+ // method called `main`, regardless of whether it is actually the entry point,
888
+ // but it will still present it as the reason for the expected type.
889
+ Some ( ( owner_id. def_id , sig. decl , ident. name != sym:: main) )
890
+ }
891
+ Node :: TraitItem ( & hir:: TraitItem {
892
+ kind : hir:: TraitItemKind :: Fn ( ref sig, ..) ,
893
+ owner_id,
894
+ ..
895
+ } ) => Some ( ( owner_id. def_id , sig. decl , true ) ) ,
896
+ // FIXME: Suggestable if this is not a trait implementation
897
+ Node :: ImplItem ( & hir:: ImplItem {
898
+ kind : hir:: ImplItemKind :: Fn ( ref sig, ..) ,
899
+ owner_id,
900
+ ..
901
+ } ) => Some ( ( owner_id. def_id , sig. decl , false ) ) ,
902
+ Node :: Expr ( & hir:: Expr {
903
+ hir_id,
904
+ kind : hir:: ExprKind :: Closure ( & hir:: Closure { def_id, kind, fn_decl, .. } ) ,
905
+ ..
906
+ } ) => {
907
+ match kind {
908
+ hir:: ClosureKind :: CoroutineClosure ( _) => {
909
+ // FIXME(async_closures): Implement this.
910
+ return None ;
911
+ }
912
+ hir:: ClosureKind :: Closure => Some ( ( def_id, fn_decl, true ) ) ,
913
+ hir:: ClosureKind :: Coroutine ( hir:: CoroutineKind :: Desugared (
914
+ _,
915
+ hir:: CoroutineSource :: Fn ,
916
+ ) ) => {
917
+ let ( ident, sig, owner_id) = match self . tcx . parent_hir_node ( hir_id) {
918
+ Node :: Item ( & hir:: Item {
919
+ ident,
920
+ kind : hir:: ItemKind :: Fn ( ref sig, ..) ,
921
+ owner_id,
922
+ ..
923
+ } ) => ( ident, sig, owner_id) ,
924
+ Node :: TraitItem ( & hir:: TraitItem {
925
+ ident,
926
+ kind : hir:: TraitItemKind :: Fn ( ref sig, ..) ,
927
+ owner_id,
928
+ ..
929
+ } ) => ( ident, sig, owner_id) ,
930
+ Node :: ImplItem ( & hir:: ImplItem {
931
+ ident,
932
+ kind : hir:: ImplItemKind :: Fn ( ref sig, ..) ,
933
+ owner_id,
934
+ ..
935
+ } ) => ( ident, sig, owner_id) ,
936
+ _ => return None ,
937
+ } ;
938
+ Some ( ( owner_id. def_id , sig. decl , ident. name != sym:: main) )
939
+ }
940
+ _ => None ,
941
+ }
942
+ }
943
+ _ => None ,
944
+ }
952
945
} )
953
946
}
954
947
0 commit comments