@@ -1630,7 +1630,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1630
1630
fcx,
1631
1631
blk_id,
1632
1632
expression,
1633
- Some ( blk_id) ,
1634
1633
) ;
1635
1634
if !fcx. tcx . features ( ) . unsized_locals {
1636
1635
unsized_return = self . is_return_ty_definitely_unsized ( fcx) ;
@@ -1641,16 +1640,15 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1641
1640
intravisit:: walk_block ( & mut visitor, loop_blk) ;
1642
1641
}
1643
1642
}
1644
- ObligationCauseCode :: ReturnValue ( id ) => {
1643
+ ObligationCauseCode :: ReturnValue ( return_expr_id ) => {
1645
1644
err = self . report_return_mismatched_types (
1646
1645
cause,
1647
1646
expected,
1648
1647
found,
1649
1648
coercion_error,
1650
1649
fcx,
1651
- id ,
1650
+ return_expr_id ,
1652
1651
expression,
1653
- None ,
1654
1652
) ;
1655
1653
if !fcx. tcx . features ( ) . unsized_locals {
1656
1654
unsized_return = self . is_return_ty_definitely_unsized ( fcx) ;
@@ -1941,13 +1939,14 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1941
1939
found : Ty < ' tcx > ,
1942
1940
ty_err : TypeError < ' tcx > ,
1943
1941
fcx : & FnCtxt < ' a , ' tcx > ,
1944
- id : hir:: HirId ,
1942
+ block_or_return_id : hir:: HirId ,
1945
1943
expression : Option < & ' tcx hir:: Expr < ' tcx > > ,
1946
- blk_id : Option < hir:: HirId > ,
1947
1944
) -> Diag < ' a > {
1948
1945
let mut err = fcx. err_ctxt ( ) . report_mismatched_types ( cause, expected, found, ty_err) ;
1949
1946
1950
- let parent_id = fcx. tcx . parent_hir_id ( id) ;
1947
+ let due_to_block = matches ! ( fcx. tcx. hir_node( block_or_return_id) , hir:: Node :: Block ( ..) ) ;
1948
+
1949
+ let parent_id = fcx. tcx . parent_hir_id ( block_or_return_id) ;
1951
1950
let parent = fcx. tcx . hir_node ( parent_id) ;
1952
1951
if let Some ( expr) = expression
1953
1952
&& let hir:: Node :: Expr ( hir:: Expr {
@@ -1962,11 +1961,16 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1962
1961
// label pointing out the cause for the type coercion will be wrong
1963
1962
// as prior return coercions would not be relevant (#57664).
1964
1963
if let Some ( expr) = expression
1965
- && let Some ( blk_id ) = blk_id
1964
+ && due_to_block
1966
1965
{
1967
1966
fcx. suggest_missing_semicolon ( & mut err, expr, expected, false ) ;
1968
- let pointing_at_return_type =
1969
- fcx. suggest_mismatched_types_on_tail ( & mut err, expr, expected, found, blk_id) ;
1967
+ let pointing_at_return_type = fcx. suggest_mismatched_types_on_tail (
1968
+ & mut err,
1969
+ expr,
1970
+ expected,
1971
+ found,
1972
+ block_or_return_id,
1973
+ ) ;
1970
1974
if let Some ( cond_expr) = fcx. tcx . hir ( ) . get_if_cause ( expr. hir_id )
1971
1975
&& expected. is_unit ( )
1972
1976
&& !pointing_at_return_type
@@ -1990,23 +1994,17 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1990
1994
}
1991
1995
} ;
1992
1996
1993
- if let Some ( ( fn_id, fn_decl, can_suggest) ) = fcx. get_fn_decl ( parent_id) {
1994
- if blk_id. is_none ( ) {
1995
- fcx. suggest_missing_return_type (
1996
- & mut err,
1997
- fn_decl,
1998
- expected,
1999
- found,
2000
- can_suggest,
2001
- fn_id,
2002
- ) ;
2003
- }
1997
+ if let Some ( ( fn_id, fn_decl, can_suggest) ) = fcx. get_fn_decl ( parent_id)
1998
+ && !due_to_block
1999
+ {
2000
+ fcx. suggest_missing_return_type ( & mut err, fn_decl, expected, found, can_suggest, fn_id) ;
2004
2001
}
2005
2002
2006
- let mut parent_id = fcx. tcx . hir ( ) . get_parent_item ( id ) . def_id ;
2003
+ let mut parent_id = fcx. tcx . hir ( ) . get_parent_item ( block_or_return_id ) . def_id ;
2007
2004
let mut parent_item = fcx. tcx . hir_node_by_def_id ( parent_id) ;
2008
2005
// When suggesting return, we need to account for closures and async blocks, not just items.
2009
- for ( _, node) in fcx. tcx . hir ( ) . parent_iter ( id) {
2006
+ // FIXME: fix get_fn_decl to be async block aware, use get_fn_decl results above
2007
+ for ( _, node) in fcx. tcx . hir ( ) . parent_iter ( block_or_return_id) {
2010
2008
match node {
2011
2009
hir:: Node :: Expr ( & hir:: Expr {
2012
2010
kind : hir:: ExprKind :: Closure ( hir:: Closure { def_id, .. } ) ,
@@ -2021,9 +2019,18 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
2021
2019
}
2022
2020
}
2023
2021
2024
- if let ( Some ( expr) , Some ( _) , Some ( fn_decl) ) = ( expression, blk_id, parent_item. fn_decl ( ) ) {
2022
+ if let Some ( expr) = expression
2023
+ && let Some ( fn_decl) = parent_item. fn_decl ( )
2024
+ && due_to_block
2025
+ {
2025
2026
fcx. suggest_missing_break_or_return_expr (
2026
- & mut err, expr, fn_decl, expected, found, id, parent_id,
2027
+ & mut err,
2028
+ expr,
2029
+ fn_decl,
2030
+ expected,
2031
+ found,
2032
+ block_or_return_id,
2033
+ parent_id,
2027
2034
) ;
2028
2035
}
2029
2036
0 commit comments