@@ -33,7 +33,7 @@ use rustc_middle::ty::{GenericArgKind, GenericArgsRef, UserArgs, UserSelfTy};
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 _;
@@ -910,76 +910,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
910
910
)
911
911
}
912
912
913
- /// Given a function `Node`, return its `HirId` and `FnDecl` if it exists. Given a closure
914
- /// that is the child of a function, return that function's `HirId` and `FnDecl` instead.
915
- /// This may seem confusing at first, but this is used in diagnostics for `async fn`,
916
- /// for example, where most of the type checking actually happens within a nested closure,
917
- /// but we often want access to the parent function's signature.
918
- ///
919
- /// Otherwise, return false.
920
- pub ( in super :: super ) fn get_node_fn_decl (
921
- & self ,
922
- node : Node < ' tcx > ,
923
- ) -> Option < ( LocalDefId , & ' tcx hir:: FnDecl < ' tcx > , Ident , bool ) > {
924
- match node {
925
- Node :: Item ( & hir:: Item {
926
- ident,
927
- kind : hir:: ItemKind :: Fn ( ref sig, ..) ,
928
- owner_id,
929
- ..
930
- } ) => {
931
- // This is less than ideal, it will not suggest a return type span on any
932
- // method called `main`, regardless of whether it is actually the entry point,
933
- // but it will still present it as the reason for the expected type.
934
- Some ( ( owner_id. def_id , & sig. decl , ident, ident. name != sym:: main) )
935
- }
936
- Node :: TraitItem ( & hir:: TraitItem {
937
- ident,
938
- kind : hir:: TraitItemKind :: Fn ( ref sig, ..) ,
939
- owner_id,
940
- ..
941
- } ) => Some ( ( owner_id. def_id , & sig. decl , ident, true ) ) ,
942
- Node :: ImplItem ( & hir:: ImplItem {
943
- ident,
944
- kind : hir:: ImplItemKind :: Fn ( ref sig, ..) ,
945
- owner_id,
946
- ..
947
- } ) => Some ( ( owner_id. def_id , & sig. decl , ident, false ) ) ,
948
- Node :: Expr ( & hir:: Expr {
949
- hir_id,
950
- kind :
951
- hir:: ExprKind :: Closure ( hir:: Closure {
952
- kind : hir:: ClosureKind :: Coroutine ( ..) , ..
953
- } ) ,
954
- ..
955
- } ) => {
956
- let ( ident, sig, owner_id) = match self . tcx . parent_hir_node ( hir_id) {
957
- Node :: Item ( & hir:: Item {
958
- ident,
959
- kind : hir:: ItemKind :: Fn ( ref sig, ..) ,
960
- owner_id,
961
- ..
962
- } ) => ( ident, sig, owner_id) ,
963
- Node :: TraitItem ( & hir:: TraitItem {
964
- ident,
965
- kind : hir:: TraitItemKind :: Fn ( ref sig, ..) ,
966
- owner_id,
967
- ..
968
- } ) => ( ident, sig, owner_id) ,
969
- Node :: ImplItem ( & hir:: ImplItem {
970
- ident,
971
- kind : hir:: ImplItemKind :: Fn ( ref sig, ..) ,
972
- owner_id,
973
- ..
974
- } ) => ( ident, sig, owner_id) ,
975
- _ => return None ,
976
- } ;
977
- Some ( ( owner_id. def_id , & sig. decl , ident, ident. name != sym:: main) )
978
- }
979
- _ => None ,
980
- }
981
- }
982
-
983
913
/// Given a `HirId`, return the `HirId` of the enclosing function, its `FnDecl`, and whether a
984
914
/// suggestion can be made, `None` otherwise.
985
915
pub fn get_fn_decl (
@@ -988,10 +918,72 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
988
918
) -> Option < ( LocalDefId , & ' tcx hir:: FnDecl < ' tcx > , bool ) > {
989
919
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
990
920
// `while` before reaching it, as block tail returns are not available in them.
991
- self . tcx . hir ( ) . get_fn_id_for_return_block ( blk_id) . and_then ( |blk_id| {
992
- let parent = self . tcx . hir_node ( blk_id) ;
993
- self . get_node_fn_decl ( parent)
994
- . map ( |( fn_id, fn_decl, _, is_main) | ( fn_id, fn_decl, is_main) )
921
+ self . tcx . hir ( ) . get_fn_id_for_return_block ( blk_id) . and_then ( |item_id| {
922
+ match self . tcx . hir_node ( item_id) {
923
+ Node :: Item ( & hir:: Item {
924
+ ident,
925
+ kind : hir:: ItemKind :: Fn ( ref sig, ..) ,
926
+ owner_id,
927
+ ..
928
+ } ) => {
929
+ // This is less than ideal, it will not suggest a return type span on any
930
+ // method called `main`, regardless of whether it is actually the entry point,
931
+ // but it will still present it as the reason for the expected type.
932
+ Some ( ( owner_id. def_id , sig. decl , ident. name != sym:: main) )
933
+ }
934
+ Node :: TraitItem ( & hir:: TraitItem {
935
+ kind : hir:: TraitItemKind :: Fn ( ref sig, ..) ,
936
+ owner_id,
937
+ ..
938
+ } ) => Some ( ( owner_id. def_id , sig. decl , true ) ) ,
939
+ // FIXME: Suggestable if this is not a trait implementation
940
+ Node :: ImplItem ( & hir:: ImplItem {
941
+ kind : hir:: ImplItemKind :: Fn ( ref sig, ..) ,
942
+ owner_id,
943
+ ..
944
+ } ) => Some ( ( owner_id. def_id , sig. decl , false ) ) ,
945
+ Node :: Expr ( & hir:: Expr {
946
+ hir_id,
947
+ kind : hir:: ExprKind :: Closure ( & hir:: Closure { def_id, kind, fn_decl, .. } ) ,
948
+ ..
949
+ } ) => {
950
+ match kind {
951
+ hir:: ClosureKind :: CoroutineClosure ( _) => {
952
+ // FIXME(async_closures): Implement this.
953
+ return None ;
954
+ }
955
+ hir:: ClosureKind :: Coroutine ( hir:: CoroutineKind :: Desugared (
956
+ _,
957
+ hir:: CoroutineSource :: Fn ,
958
+ ) ) => {
959
+ let ( ident, sig, owner_id) = match self . tcx . parent_hir_node ( hir_id) {
960
+ Node :: Item ( & hir:: Item {
961
+ ident,
962
+ kind : hir:: ItemKind :: Fn ( ref sig, ..) ,
963
+ owner_id,
964
+ ..
965
+ } ) => ( ident, sig, owner_id) ,
966
+ Node :: TraitItem ( & hir:: TraitItem {
967
+ ident,
968
+ kind : hir:: TraitItemKind :: Fn ( ref sig, ..) ,
969
+ owner_id,
970
+ ..
971
+ } ) => ( ident, sig, owner_id) ,
972
+ Node :: ImplItem ( & hir:: ImplItem {
973
+ ident,
974
+ kind : hir:: ImplItemKind :: Fn ( ref sig, ..) ,
975
+ owner_id,
976
+ ..
977
+ } ) => ( ident, sig, owner_id) ,
978
+ _ => return None ,
979
+ } ;
980
+ Some ( ( owner_id. def_id , sig. decl , ident. name != sym:: main) )
981
+ }
982
+ _ => Some ( ( def_id, fn_decl, true ) ) ,
983
+ }
984
+ }
985
+ _ => None ,
986
+ }
995
987
} )
996
988
}
997
989
0 commit comments