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