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