1
1
use super :: FnCtxt ;
2
2
3
- use crate :: coercion:: CollectRetsVisitor ;
4
3
use crate :: errors;
5
4
use crate :: fluent_generated as fluent;
6
5
use crate :: fn_ctxt:: rustc_span:: BytePos ;
@@ -17,7 +16,6 @@ use rustc_errors::{Applicability, Diagnostic, MultiSpan};
17
16
use rustc_hir as hir;
18
17
use rustc_hir:: def:: Res ;
19
18
use rustc_hir:: def:: { CtorKind , CtorOf , DefKind } ;
20
- use rustc_hir:: intravisit:: { Map , Visitor } ;
21
19
use rustc_hir:: lang_items:: LangItem ;
22
20
use rustc_hir:: {
23
21
CoroutineDesugaring , CoroutineKind , CoroutineSource , Expr , ExprKind , GenericBound , HirId , Node ,
@@ -1040,22 +1038,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1040
1038
return ;
1041
1039
}
1042
1040
1043
- let in_closure = matches ! (
1044
- self . tcx
1045
- . hir( )
1046
- . parent_iter( id)
1047
- . filter( |( _, node) | {
1048
- matches!(
1049
- node,
1050
- Node :: Expr ( Expr { kind: ExprKind :: Closure ( ..) , .. } )
1051
- | Node :: Item ( _)
1052
- | Node :: TraitItem ( _)
1053
- | Node :: ImplItem ( _)
1054
- )
1055
- } )
1056
- . next( ) ,
1057
- Some ( ( _ , Node :: Expr ( Expr { kind : ExprKind :: Closure ( .. ) , .. } ) ) )
1058
- ) ;
1041
+ let scope = self
1042
+ . tcx
1043
+ . hir ( )
1044
+ . parent_iter ( id)
1045
+ . filter ( |( _, node) | {
1046
+ matches ! (
1047
+ node,
1048
+ Node :: Expr ( Expr { kind: ExprKind :: Closure ( ..) , .. } )
1049
+ | Node :: Item ( _)
1050
+ | Node :: TraitItem ( _)
1051
+ | Node :: ImplItem ( _)
1052
+ )
1053
+ } )
1054
+ . next ( ) ;
1055
+ let in_closure =
1056
+ matches ! ( scope , Some ( ( _ , Node :: Expr ( Expr { kind : ExprKind :: Closure ( .. ) , .. } ) ) ) ) ;
1059
1057
1060
1058
let can_return = match fn_decl. output {
1061
1059
hir:: FnRetTy :: Return ( ty) => {
@@ -1077,29 +1075,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1077
1075
self . can_coerce ( found, ty)
1078
1076
}
1079
1077
hir:: FnRetTy :: DefaultReturn ( _) if in_closure => {
1080
- let mut rets = vec ! [ ] ;
1081
- if let Some ( ret_coercion) = self . ret_coercion . as_ref ( ) {
1082
- let ret_ty = ret_coercion. borrow ( ) . expected_ty ( ) ;
1083
- rets. push ( ret_ty) ;
1084
- }
1085
- let mut visitor = CollectRetsVisitor { ret_exprs : vec ! [ ] } ;
1086
- if let Some ( item) = self . tcx . hir ( ) . find ( id)
1087
- && let Node :: Expr ( expr) = item
1088
- {
1089
- visitor. visit_expr ( expr) ;
1090
- for expr in visitor. ret_exprs {
1091
- if let Some ( ty) = self . typeck_results . borrow ( ) . node_type_opt ( expr. hir_id ) {
1092
- rets. push ( ty) ;
1093
- }
1094
- }
1095
- if let hir:: ExprKind :: Block ( hir:: Block { expr : Some ( expr) , .. } , _) = expr. kind
1096
- {
1097
- if let Some ( ty) = self . typeck_results . borrow ( ) . node_type_opt ( expr. hir_id ) {
1098
- rets. push ( ty) ;
1099
- }
1100
- }
1101
- }
1102
- rets. into_iter ( ) . all ( |ty| self . can_coerce ( found, ty) )
1078
+ self . ret_coercion . as_ref ( ) . map_or ( false , |ret| {
1079
+ let ret_ty = ret. borrow ( ) . expected_ty ( ) ;
1080
+ self . can_coerce ( found, ret_ty)
1081
+ } )
1103
1082
}
1104
1083
_ => false ,
1105
1084
} ;
0 commit comments