37
37
38
38
use crate :: errors:: SuggestBoxingForReturnImplTrait ;
39
39
use crate :: FnCtxt ;
40
- use rustc_errors:: { codes:: * , struct_span_code_err, Applicability , Diag , MultiSpan } ;
40
+ use rustc_errors:: { codes:: * , struct_span_code_err, Diag } ;
41
41
use rustc_hir as hir;
42
42
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
43
- use rustc_hir:: intravisit:: { self , Visitor } ;
44
- use rustc_hir:: Expr ;
45
43
use rustc_hir_analysis:: hir_ty_lowering:: HirTyLowerer ;
46
44
use rustc_infer:: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
47
45
use rustc_infer:: infer:: { Coercion , DefineOpaqueTypes , InferOk , InferResult } ;
@@ -95,22 +93,6 @@ impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {
95
93
96
94
type CoerceResult < ' tcx > = InferResult < ' tcx , ( Vec < Adjustment < ' tcx > > , Ty < ' tcx > ) > ;
97
95
98
- struct CollectRetsVisitor < ' tcx > {
99
- ret_exprs : Vec < & ' tcx hir:: Expr < ' tcx > > ,
100
- }
101
-
102
- impl < ' tcx > Visitor < ' tcx > for CollectRetsVisitor < ' tcx > {
103
- fn visit_expr ( & mut self , expr : & ' tcx Expr < ' tcx > ) {
104
- match expr. kind {
105
- hir:: ExprKind :: Ret ( _) => self . ret_exprs . push ( expr) ,
106
- // `return` in closures does not return from the outer function
107
- hir:: ExprKind :: Closure ( _) => return ,
108
- _ => { }
109
- }
110
- intravisit:: walk_expr ( self , expr) ;
111
- }
112
- }
113
-
114
96
/// Coercing a mutable reference to an immutable works, while
115
97
/// coercing `&T` to `&mut T` should be forbidden.
116
98
fn coerce_mutbls < ' tcx > (
@@ -1607,7 +1589,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1607
1589
1608
1590
let mut err;
1609
1591
let mut unsized_return = false ;
1610
- let mut visitor = CollectRetsVisitor { ret_exprs : vec ! [ ] } ;
1611
1592
match * cause. code ( ) {
1612
1593
ObligationCauseCode :: ReturnNoExpression => {
1613
1594
err = struct_span_code_err ! (
@@ -1633,11 +1614,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1633
1614
if !fcx. tcx . features ( ) . unsized_locals {
1634
1615
unsized_return = self . is_return_ty_definitely_unsized ( fcx) ;
1635
1616
}
1636
- if let Some ( expression) = expression
1637
- && let hir:: ExprKind :: Loop ( loop_blk, ..) = expression. kind
1638
- {
1639
- intravisit:: walk_block ( & mut visitor, loop_blk) ;
1640
- }
1641
1617
}
1642
1618
ObligationCauseCode :: ReturnValue ( id) => {
1643
1619
err = self . report_return_mismatched_types (
@@ -1738,6 +1714,22 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1738
1714
augment_error ( & mut err) ;
1739
1715
1740
1716
if let Some ( expr) = expression {
1717
+ if let hir:: ExprKind :: Loop (
1718
+ _,
1719
+ _,
1720
+ loop_src @ ( hir:: LoopSource :: While | hir:: LoopSource :: ForLoop ) ,
1721
+ _,
1722
+ ) = expr. kind
1723
+ {
1724
+ let loop_type = if loop_src == hir:: LoopSource :: While {
1725
+ "`while` loops"
1726
+ } else {
1727
+ "`for` loops"
1728
+ } ;
1729
+
1730
+ err. note ( format ! ( "{loop_type} evaluate to unit type `()`" ) ) ;
1731
+ }
1732
+
1741
1733
fcx. emit_coerce_suggestions (
1742
1734
& mut err,
1743
1735
expr,
@@ -1746,15 +1738,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1746
1738
None ,
1747
1739
Some ( coercion_error) ,
1748
1740
) ;
1749
- if visitor. ret_exprs . len ( ) > 0 {
1750
- self . note_unreachable_loop_return (
1751
- & mut err,
1752
- fcx. tcx ,
1753
- & expr,
1754
- & visitor. ret_exprs ,
1755
- expected,
1756
- ) ;
1757
- }
1758
1741
}
1759
1742
1760
1743
let reported = err. emit_unless ( unsized_return) ;
@@ -1828,110 +1811,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1828
1811
) ;
1829
1812
}
1830
1813
1831
- fn note_unreachable_loop_return (
1832
- & self ,
1833
- err : & mut Diag < ' _ > ,
1834
- tcx : TyCtxt < ' tcx > ,
1835
- expr : & hir:: Expr < ' tcx > ,
1836
- ret_exprs : & Vec < & ' tcx hir:: Expr < ' tcx > > ,
1837
- ty : Ty < ' tcx > ,
1838
- ) {
1839
- let hir:: ExprKind :: Loop ( _, _, _, loop_span) = expr. kind else {
1840
- return ;
1841
- } ;
1842
- let mut span: MultiSpan = vec ! [ loop_span] . into ( ) ;
1843
- span. push_span_label ( loop_span, "this might have zero elements to iterate on" ) ;
1844
- const MAXITER : usize = 3 ;
1845
- let iter = ret_exprs. iter ( ) . take ( MAXITER ) ;
1846
- for ret_expr in iter {
1847
- span. push_span_label (
1848
- ret_expr. span ,
1849
- "if the loop doesn't execute, this value would never get returned" ,
1850
- ) ;
1851
- }
1852
- err. span_note (
1853
- span,
1854
- "the function expects a value to always be returned, but loops might run zero times" ,
1855
- ) ;
1856
- if MAXITER < ret_exprs. len ( ) {
1857
- err. note ( format ! (
1858
- "if the loop doesn't execute, {} other values would never get returned" ,
1859
- ret_exprs. len( ) - MAXITER
1860
- ) ) ;
1861
- }
1862
- let hir = tcx. hir ( ) ;
1863
- let item = hir. get_parent_item ( expr. hir_id ) ;
1864
- let ret_msg = "return a value for the case when the loop has zero elements to iterate on" ;
1865
- let ret_ty_msg =
1866
- "otherwise consider changing the return type to account for that possibility" ;
1867
- let node = tcx. hir_node ( item. into ( ) ) ;
1868
- if let Some ( body_id) = node. body_id ( )
1869
- && let Some ( sig) = node. fn_sig ( )
1870
- && let hir:: ExprKind :: Block ( block, _) = hir. body ( body_id) . value . kind
1871
- && !ty. is_never ( )
1872
- {
1873
- let indentation = if let None = block. expr
1874
- && let [ .., last] = & block. stmts
1875
- {
1876
- tcx. sess . source_map ( ) . indentation_before ( last. span ) . unwrap_or_else ( String :: new)
1877
- } else if let Some ( expr) = block. expr {
1878
- tcx. sess . source_map ( ) . indentation_before ( expr. span ) . unwrap_or_else ( String :: new)
1879
- } else {
1880
- String :: new ( )
1881
- } ;
1882
- if let None = block. expr
1883
- && let [ .., last] = & block. stmts
1884
- {
1885
- err. span_suggestion_verbose (
1886
- last. span . shrink_to_hi ( ) ,
1887
- ret_msg,
1888
- format ! ( "\n {indentation}/* `{ty}` value */" ) ,
1889
- Applicability :: MaybeIncorrect ,
1890
- ) ;
1891
- } else if let Some ( expr) = block. expr {
1892
- err. span_suggestion_verbose (
1893
- expr. span . shrink_to_hi ( ) ,
1894
- ret_msg,
1895
- format ! ( "\n {indentation}/* `{ty}` value */" ) ,
1896
- Applicability :: MaybeIncorrect ,
1897
- ) ;
1898
- }
1899
- let mut sugg = match sig. decl . output {
1900
- hir:: FnRetTy :: DefaultReturn ( span) => {
1901
- vec ! [ ( span, " -> Option<()>" . to_string( ) ) ]
1902
- }
1903
- hir:: FnRetTy :: Return ( ty) => {
1904
- vec ! [
1905
- ( ty. span. shrink_to_lo( ) , "Option<" . to_string( ) ) ,
1906
- ( ty. span. shrink_to_hi( ) , ">" . to_string( ) ) ,
1907
- ]
1908
- }
1909
- } ;
1910
- for ret_expr in ret_exprs {
1911
- match ret_expr. kind {
1912
- hir:: ExprKind :: Ret ( Some ( expr) ) => {
1913
- sugg. push ( ( expr. span . shrink_to_lo ( ) , "Some(" . to_string ( ) ) ) ;
1914
- sugg. push ( ( expr. span . shrink_to_hi ( ) , ")" . to_string ( ) ) ) ;
1915
- }
1916
- hir:: ExprKind :: Ret ( None ) => {
1917
- sugg. push ( ( ret_expr. span . shrink_to_hi ( ) , " Some(())" . to_string ( ) ) ) ;
1918
- }
1919
- _ => { }
1920
- }
1921
- }
1922
- if let None = block. expr
1923
- && let [ .., last] = & block. stmts
1924
- {
1925
- sugg. push ( ( last. span . shrink_to_hi ( ) , format ! ( "\n {indentation}None" ) ) ) ;
1926
- } else if let Some ( expr) = block. expr {
1927
- sugg. push ( ( expr. span . shrink_to_hi ( ) , format ! ( "\n {indentation}None" ) ) ) ;
1928
- }
1929
- err. multipart_suggestion ( ret_ty_msg, sugg, Applicability :: MaybeIncorrect ) ;
1930
- } else {
1931
- err. help ( format ! ( "{ret_msg}, {ret_ty_msg}" ) ) ;
1932
- }
1933
- }
1934
-
1935
1814
fn report_return_mismatched_types < ' a > (
1936
1815
& self ,
1937
1816
cause : & ObligationCause < ' tcx > ,
0 commit comments