@@ -2041,7 +2041,6 @@ impl<'a> LoweringContext<'a> {
2041
2041
//
2042
2042
// match <sub_expr> {
2043
2043
// <pat> => <body>,
2044
- // [_ if <else_opt_if_cond> => <else_opt_if_body>,]
2045
2044
// _ => [<else_opt> | ()]
2046
2045
// }
2047
2046
@@ -2055,93 +2054,16 @@ impl<'a> LoweringContext<'a> {
2055
2054
arms. push ( self . arm ( hir_vec ! [ pat] , body_expr) ) ;
2056
2055
}
2057
2056
2058
- // `[_ if <else_opt_if_cond> => <else_opt_if_body>,]`
2059
- // `_ => [<else_opt> | ()]`
2057
+ // _ => [<else_opt>|()]
2060
2058
{
2061
- let mut current: Option < & Expr > = else_opt. as_ref ( ) . map ( |p| & * * p) ;
2062
- let mut else_exprs: Vec < Option < & Expr > > = vec ! [ current] ;
2063
-
2064
- // First, we traverse the AST and recursively collect all
2065
- // `else` branches into else_exprs, e.g.:
2066
- //
2067
- // if let Some(_) = x {
2068
- // ...
2069
- // } else if ... { // Expr1
2070
- // ...
2071
- // } else if ... { // Expr2
2072
- // ...
2073
- // } else { // Expr3
2074
- // ...
2075
- // }
2076
- //
2077
- // ... results in else_exprs = [Some(&Expr1),
2078
- // Some(&Expr2),
2079
- // Some(&Expr3)]
2080
- //
2081
- // Because there also the case there is no `else`, these
2082
- // entries can also be `None`, as in:
2083
- //
2084
- // if let Some(_) = x {
2085
- // ...
2086
- // } else if ... { // Expr1
2087
- // ...
2088
- // } else if ... { // Expr2
2089
- // ...
2090
- // }
2091
- //
2092
- // ... results in else_exprs = [Some(&Expr1),
2093
- // Some(&Expr2),
2094
- // None]
2095
- //
2096
- // The last entry in this list is always translated into
2097
- // the final "unguard" wildcard arm of the `match`. In the
2098
- // case of a `None`, it becomes `_ => ()`.
2099
- loop {
2100
- if let Some ( e) = current {
2101
- // There is an else branch at this level
2102
- if let ExprKind :: If ( _, _, ref else_opt) = e. node {
2103
- // The else branch is again an if-expr
2104
- current = else_opt. as_ref ( ) . map ( |p| & * * p) ;
2105
- else_exprs. push ( current) ;
2106
- } else {
2107
- // The last item in the list is not an if-expr,
2108
- // stop here
2109
- break
2110
- }
2111
- } else {
2112
- // We have no more else branch
2113
- break
2114
- }
2115
- }
2116
-
2117
- // Now translate the list of nested else-branches into the
2118
- // arms of the match statement.
2119
- for else_expr in else_exprs {
2120
- if let Some ( else_expr) = else_expr {
2121
- let ( guard, body) = if let ExprKind :: If ( ref cond,
2122
- ref then,
2123
- _) = else_expr. node {
2124
- let then = self . lower_block ( then, false ) ;
2125
- ( Some ( cond) ,
2126
- self . expr_block ( then, ThinVec :: new ( ) ) )
2127
- } else {
2128
- ( None ,
2129
- self . lower_expr ( else_expr) )
2130
- } ;
2131
-
2132
- arms. push ( hir:: Arm {
2133
- attrs : hir_vec ! [ ] ,
2134
- pats : hir_vec ! [ self . pat_wild( e. span) ] ,
2135
- guard : guard. map ( |e| P ( self . lower_expr ( e) ) ) ,
2136
- body : P ( body) ,
2137
- } ) ;
2138
- } else {
2139
- // There was no else-branch, push a noop
2140
- let pat_under = self . pat_wild ( e. span ) ;
2141
- let unit = self . expr_tuple ( e. span , hir_vec ! [ ] ) ;
2142
- arms. push ( self . arm ( hir_vec ! [ pat_under] , unit) ) ;
2143
- }
2144
- }
2059
+ let wildcard_arm: Option < & Expr > = else_opt. as_ref ( ) . map ( |p| & * * p) ;
2060
+ let wildcard_pattern = self . pat_wild ( e. span ) ;
2061
+ let body = if let Some ( else_expr) = wildcard_arm {
2062
+ P ( self . lower_expr ( else_expr) )
2063
+ } else {
2064
+ self . expr_tuple ( e. span , hir_vec ! [ ] )
2065
+ } ;
2066
+ arms. push ( self . arm ( hir_vec ! [ wildcard_pattern] , body) ) ;
2145
2067
}
2146
2068
2147
2069
let contains_else_clause = else_opt. is_some ( ) ;
0 commit comments