@@ -1599,22 +1599,20 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1599
1599
fcx,
1600
1600
blk_id,
1601
1601
expression,
1602
- Some ( blk_id) ,
1603
1602
) ;
1604
1603
if !fcx. tcx . features ( ) . unsized_locals {
1605
1604
unsized_return = self . is_return_ty_definitely_unsized ( fcx) ;
1606
1605
}
1607
1606
}
1608
- ObligationCauseCode :: ReturnValue ( id ) => {
1607
+ ObligationCauseCode :: ReturnValue ( return_expr_id ) => {
1609
1608
err = self . report_return_mismatched_types (
1610
1609
cause,
1611
1610
expected,
1612
1611
found,
1613
1612
coercion_error,
1614
1613
fcx,
1615
- id ,
1614
+ return_expr_id ,
1616
1615
expression,
1617
- None ,
1618
1616
) ;
1619
1617
if !fcx. tcx . features ( ) . unsized_locals {
1620
1618
unsized_return = self . is_return_ty_definitely_unsized ( fcx) ;
@@ -1808,13 +1806,14 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1808
1806
found : Ty < ' tcx > ,
1809
1807
ty_err : TypeError < ' tcx > ,
1810
1808
fcx : & FnCtxt < ' a , ' tcx > ,
1811
- id : hir:: HirId ,
1809
+ block_or_return_id : hir:: HirId ,
1812
1810
expression : Option < & ' tcx hir:: Expr < ' tcx > > ,
1813
- blk_id : Option < hir:: HirId > ,
1814
1811
) -> Diag < ' a > {
1815
1812
let mut err = fcx. err_ctxt ( ) . report_mismatched_types ( cause, expected, found, ty_err) ;
1816
1813
1817
- let parent_id = fcx. tcx . parent_hir_id ( id) ;
1814
+ let due_to_block = matches ! ( fcx. tcx. hir_node( block_or_return_id) , hir:: Node :: Block ( ..) ) ;
1815
+
1816
+ let parent_id = fcx. tcx . parent_hir_id ( block_or_return_id) ;
1818
1817
let parent = fcx. tcx . hir_node ( parent_id) ;
1819
1818
if let Some ( expr) = expression
1820
1819
&& let hir:: Node :: Expr ( hir:: Expr {
@@ -1829,11 +1828,16 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1829
1828
// label pointing out the cause for the type coercion will be wrong
1830
1829
// as prior return coercions would not be relevant (#57664).
1831
1830
if let Some ( expr) = expression
1832
- && let Some ( blk_id ) = blk_id
1831
+ && due_to_block
1833
1832
{
1834
1833
fcx. suggest_missing_semicolon ( & mut err, expr, expected, false ) ;
1835
- let pointing_at_return_type =
1836
- fcx. suggest_mismatched_types_on_tail ( & mut err, expr, expected, found, blk_id) ;
1834
+ let pointing_at_return_type = fcx. suggest_mismatched_types_on_tail (
1835
+ & mut err,
1836
+ expr,
1837
+ expected,
1838
+ found,
1839
+ block_or_return_id,
1840
+ ) ;
1837
1841
if let Some ( cond_expr) = fcx. tcx . hir ( ) . get_if_cause ( expr. hir_id )
1838
1842
&& expected. is_unit ( )
1839
1843
&& !pointing_at_return_type
@@ -1857,23 +1861,17 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1857
1861
}
1858
1862
} ;
1859
1863
1860
- if let Some ( ( fn_id, fn_decl, can_suggest) ) = fcx. get_fn_decl ( parent_id) {
1861
- if blk_id. is_none ( ) {
1862
- fcx. suggest_missing_return_type (
1863
- & mut err,
1864
- fn_decl,
1865
- expected,
1866
- found,
1867
- can_suggest,
1868
- fn_id,
1869
- ) ;
1870
- }
1864
+ if let Some ( ( fn_id, fn_decl, can_suggest) ) = fcx. get_fn_decl ( parent_id)
1865
+ && !due_to_block
1866
+ {
1867
+ fcx. suggest_missing_return_type ( & mut err, fn_decl, expected, found, can_suggest, fn_id) ;
1871
1868
}
1872
1869
1873
- let mut parent_id = fcx. tcx . hir ( ) . get_parent_item ( id ) . def_id ;
1870
+ let mut parent_id = fcx. tcx . hir ( ) . get_parent_item ( block_or_return_id ) . def_id ;
1874
1871
let mut parent_item = fcx. tcx . hir_node_by_def_id ( parent_id) ;
1875
1872
// When suggesting return, we need to account for closures and async blocks, not just items.
1876
- for ( _, node) in fcx. tcx . hir ( ) . parent_iter ( id) {
1873
+ // FIXME: fix get_fn_decl to be async block aware, use get_fn_decl results above
1874
+ for ( _, node) in fcx. tcx . hir ( ) . parent_iter ( block_or_return_id) {
1877
1875
match node {
1878
1876
hir:: Node :: Expr ( & hir:: Expr {
1879
1877
kind : hir:: ExprKind :: Closure ( hir:: Closure { def_id, .. } ) ,
@@ -1888,9 +1886,18 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1888
1886
}
1889
1887
}
1890
1888
1891
- if let ( Some ( expr) , Some ( _) , Some ( fn_decl) ) = ( expression, blk_id, parent_item. fn_decl ( ) ) {
1889
+ if let Some ( expr) = expression
1890
+ && let Some ( fn_decl) = parent_item. fn_decl ( )
1891
+ && due_to_block
1892
+ {
1892
1893
fcx. suggest_missing_break_or_return_expr (
1893
- & mut err, expr, fn_decl, expected, found, id, parent_id,
1894
+ & mut err,
1895
+ expr,
1896
+ fn_decl,
1897
+ expected,
1898
+ found,
1899
+ block_or_return_id,
1900
+ parent_id,
1894
1901
) ;
1895
1902
}
1896
1903
0 commit comments