@@ -87,40 +87,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
87
87
}
88
88
}
89
89
ExprKind :: Ret ( ref expr_opt) => {
90
- if self . ret_coercion . is_none ( ) {
91
- struct_span_err ! ( self . tcx. sess, expr. span, E0572 ,
92
- "return statement outside of function body" ) . emit ( ) ;
93
- } else if let Some ( ref e) = * expr_opt {
94
- if self . ret_coercion_span . borrow ( ) . is_none ( ) {
95
- * self . ret_coercion_span . borrow_mut ( ) = Some ( e. span ) ;
96
- }
97
- self . check_return_expr ( e) ;
98
- } else {
99
- let mut coercion = self . ret_coercion . as_ref ( ) . unwrap ( ) . borrow_mut ( ) ;
100
- if self . ret_coercion_span . borrow ( ) . is_none ( ) {
101
- * self . ret_coercion_span . borrow_mut ( ) = Some ( expr. span ) ;
102
- }
103
- let cause = self . cause ( expr. span , ObligationCauseCode :: ReturnNoExpression ) ;
104
- if let Some ( ( fn_decl, _) ) = self . get_fn_decl ( expr. hir_id ) {
105
- coercion. coerce_forced_unit (
106
- self ,
107
- & cause,
108
- & mut |db| {
109
- db. span_label (
110
- fn_decl. output . span ( ) ,
111
- format ! (
112
- "expected `{}` because of this return type" ,
113
- fn_decl. output,
114
- ) ,
115
- ) ;
116
- } ,
117
- true ,
118
- ) ;
119
- } else {
120
- coercion. coerce_forced_unit ( self , & cause, & mut |_| ( ) , true ) ;
121
- }
122
- }
123
- tcx. types . never
90
+ self . check_expr_return ( expr_opt. deref ( ) , expr)
124
91
}
125
92
ExprKind :: Assign ( ref lhs, ref rhs) => {
126
93
self . check_assign ( expr, expected, lhs, rhs)
@@ -734,4 +701,45 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
734
701
tcx. types . err
735
702
}
736
703
}
704
+
705
+ fn check_expr_return (
706
+ & self ,
707
+ expr_opt : Option < & ' tcx hir:: Expr > ,
708
+ expr : & ' tcx hir:: Expr
709
+ ) -> Ty < ' tcx > {
710
+ if self . ret_coercion . is_none ( ) {
711
+ struct_span_err ! ( self . tcx. sess, expr. span, E0572 ,
712
+ "return statement outside of function body" ) . emit ( ) ;
713
+ } else if let Some ( ref e) = expr_opt {
714
+ if self . ret_coercion_span . borrow ( ) . is_none ( ) {
715
+ * self . ret_coercion_span . borrow_mut ( ) = Some ( e. span ) ;
716
+ }
717
+ self . check_return_expr ( e) ;
718
+ } else {
719
+ let mut coercion = self . ret_coercion . as_ref ( ) . unwrap ( ) . borrow_mut ( ) ;
720
+ if self . ret_coercion_span . borrow ( ) . is_none ( ) {
721
+ * self . ret_coercion_span . borrow_mut ( ) = Some ( expr. span ) ;
722
+ }
723
+ let cause = self . cause ( expr. span , ObligationCauseCode :: ReturnNoExpression ) ;
724
+ if let Some ( ( fn_decl, _) ) = self . get_fn_decl ( expr. hir_id ) {
725
+ coercion. coerce_forced_unit (
726
+ self ,
727
+ & cause,
728
+ & mut |db| {
729
+ db. span_label (
730
+ fn_decl. output . span ( ) ,
731
+ format ! (
732
+ "expected `{}` because of this return type" ,
733
+ fn_decl. output,
734
+ ) ,
735
+ ) ;
736
+ } ,
737
+ true ,
738
+ ) ;
739
+ } else {
740
+ coercion. coerce_forced_unit ( self , & cause, & mut |_| ( ) , true ) ;
741
+ }
742
+ }
743
+ self . tcx . types . never
744
+ }
737
745
}
0 commit comments