11use super :: FnCtxt ;
22
3+ use crate :: coercion:: CollectRetsVisitor ;
34use crate :: errors;
45use crate :: fluent_generated as fluent;
56use crate :: fn_ctxt:: rustc_span:: BytePos ;
@@ -16,6 +17,7 @@ use rustc_errors::{Applicability, Diagnostic, MultiSpan};
1617use rustc_hir as hir;
1718use rustc_hir:: def:: Res ;
1819use rustc_hir:: def:: { CtorKind , CtorOf , DefKind } ;
20+ use rustc_hir:: intravisit:: { Map , Visitor } ;
1921use rustc_hir:: lang_items:: LangItem ;
2022use rustc_hir:: {
2123 CoroutineDesugaring , CoroutineKind , CoroutineSource , Expr , ExprKind , GenericBound , HirId , Node ,
@@ -828,6 +830,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
828830 }
829831 hir:: FnRetTy :: Return ( hir_ty) => {
830832 if let hir:: TyKind :: OpaqueDef ( item_id, ..) = hir_ty. kind
833+ // FIXME: account for RPITIT.
831834 && let hir:: Node :: Item ( hir:: Item {
832835 kind : hir:: ItemKind :: OpaqueTy ( op_ty) , ..
833836 } ) = self . tcx . hir_node ( item_id. hir_id ( ) )
@@ -1039,33 +1042,81 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10391042 return ;
10401043 }
10411044
1042- if let hir:: FnRetTy :: Return ( ty) = fn_decl. output {
1043- let ty = self . astconv ( ) . ast_ty_to_ty ( ty) ;
1044- let bound_vars = self . tcx . late_bound_vars ( fn_id) ;
1045- let ty = self
1046- . tcx
1047- . instantiate_bound_regions_with_erased ( Binder :: bind_with_vars ( ty, bound_vars) ) ;
1048- let ty = match self . tcx . asyncness ( fn_id. owner ) {
1049- ty:: Asyncness :: Yes => self . get_impl_future_output_ty ( ty) . unwrap_or_else ( || {
1050- span_bug ! ( fn_decl. output. span( ) , "failed to get output type of async function" )
1051- } ) ,
1052- ty:: Asyncness :: No => ty,
1053- } ;
1054- let ty = self . normalize ( expr. span , ty) ;
1055- if self . can_coerce ( found, ty) {
1056- if let Some ( owner_node) = self . tcx . hir_node ( fn_id) . as_owner ( )
1057- && let Some ( span) = expr. span . find_ancestor_inside ( * owner_node. span ( ) )
1045+ let in_closure = matches ! (
1046+ self . tcx
1047+ . hir( )
1048+ . parent_iter( id)
1049+ . filter( |( _, node) | {
1050+ matches!(
1051+ node,
1052+ Node :: Expr ( Expr { kind: ExprKind :: Closure ( ..) , .. } )
1053+ | Node :: Item ( _)
1054+ | Node :: TraitItem ( _)
1055+ | Node :: ImplItem ( _)
1056+ )
1057+ } )
1058+ . next( ) ,
1059+ Some ( ( _, Node :: Expr ( Expr { kind: ExprKind :: Closure ( ..) , .. } ) ) )
1060+ ) ;
1061+
1062+ let can_return = match fn_decl. output {
1063+ hir:: FnRetTy :: Return ( ty) => {
1064+ let ty = self . astconv ( ) . ast_ty_to_ty ( ty) ;
1065+ let bound_vars = self . tcx . late_bound_vars ( fn_id) ;
1066+ let ty = self
1067+ . tcx
1068+ . instantiate_bound_regions_with_erased ( Binder :: bind_with_vars ( ty, bound_vars) ) ;
1069+ let ty = match self . tcx . asyncness ( fn_id. owner ) {
1070+ ty:: Asyncness :: Yes => self . get_impl_future_output_ty ( ty) . unwrap_or_else ( || {
1071+ span_bug ! (
1072+ fn_decl. output. span( ) ,
1073+ "failed to get output type of async function"
1074+ )
1075+ } ) ,
1076+ ty:: Asyncness :: No => ty,
1077+ } ;
1078+ let ty = self . normalize ( expr. span , ty) ;
1079+ self . can_coerce ( found, ty)
1080+ }
1081+ hir:: FnRetTy :: DefaultReturn ( _) if in_closure => {
1082+ let mut rets = vec ! [ ] ;
1083+ if let Some ( ret_coercion) = self . ret_coercion . as_ref ( ) {
1084+ let ret_ty = ret_coercion. borrow ( ) . expected_ty ( ) ;
1085+ rets. push ( ret_ty) ;
1086+ }
1087+ let mut visitor = CollectRetsVisitor { ret_exprs : vec ! [ ] } ;
1088+ if let Some ( item) = self . tcx . hir ( ) . find ( id)
1089+ && let Node :: Expr ( expr) = item
10581090 {
1059- err. multipart_suggestion (
1060- "you might have meant to return this value" ,
1061- vec ! [
1062- ( span. shrink_to_lo( ) , "return " . to_string( ) ) ,
1063- ( span. shrink_to_hi( ) , ";" . to_string( ) ) ,
1064- ] ,
1065- Applicability :: MaybeIncorrect ,
1066- ) ;
1091+ visitor. visit_expr ( expr) ;
1092+ for expr in visitor. ret_exprs {
1093+ if let Some ( ty) = self . typeck_results . borrow ( ) . node_type_opt ( expr. hir_id ) {
1094+ rets. push ( ty) ;
1095+ }
1096+ }
1097+ if let hir:: ExprKind :: Block ( hir:: Block { expr : Some ( expr) , .. } , _) = expr. kind
1098+ {
1099+ if let Some ( ty) = self . typeck_results . borrow ( ) . node_type_opt ( expr. hir_id ) {
1100+ rets. push ( ty) ;
1101+ }
1102+ }
10671103 }
1104+ rets. into_iter ( ) . all ( |ty| self . can_coerce ( found, ty) )
10681105 }
1106+ _ => false ,
1107+ } ;
1108+ if can_return
1109+ && let Some ( owner_node) = self . tcx . hir_node ( fn_id) . as_owner ( )
1110+ && let Some ( span) = expr. span . find_ancestor_inside ( owner_node. span ( ) )
1111+ {
1112+ err. multipart_suggestion (
1113+ "you might have meant to return this value" ,
1114+ vec ! [
1115+ ( span. shrink_to_lo( ) , "return " . to_string( ) ) ,
1116+ ( span. shrink_to_hi( ) , ";" . to_string( ) ) ,
1117+ ] ,
1118+ Applicability :: MaybeIncorrect ,
1119+ ) ;
10691120 }
10701121 }
10711122
0 commit comments