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