Skip to content

Commit 65fafd9

Browse files
author
Suchith J N
committed
[41272] - code for desugaring iflet changed
1 parent 4f32e0d commit 65fafd9

File tree

2 files changed

+23
-87
lines changed

2 files changed

+23
-87
lines changed

src/librustc/hir/lowering.rs

Lines changed: 10 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,6 @@ impl<'a> LoweringContext<'a> {
20332033
//
20342034
// match <sub_expr> {
20352035
// <pat> => <body>,
2036-
// [_ if <else_opt_if_cond> => <else_opt_if_body>,]
20372036
// _ => [<else_opt> | ()]
20382037
// }
20392038

@@ -2047,93 +2046,17 @@ impl<'a> LoweringContext<'a> {
20472046
arms.push(self.arm(hir_vec![pat], body_expr));
20482047
}
20492048

2050-
// `[_ if <else_opt_if_cond> => <else_opt_if_body>,]`
2051-
// `_ => [<else_opt> | ()]`
2049+
// _ => [<else_opt>|()]
20522050
{
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));
21372060
}
21382061

21392062
let contains_else_clause = else_opt.is_some();

src/test/run-pass/issue-41272.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct Foo;
2+
3+
impl Foo {
4+
fn bar(&mut self) -> bool { true }
5+
}
6+
7+
/* This causes E0301. By fixing issue #41272 this problem should vanish */
8+
fn iflet_issue(foo: &mut Foo) {
9+
if let Some(_) = Some(true) {
10+
} else if foo.bar() {}
11+
}
12+
13+
fn main() {}

0 commit comments

Comments
 (0)