Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 77d80b2

Browse files
Introduce if-let guards in the HIR
1 parent a68864b commit 77d80b2

File tree

6 files changed

+24
-15
lines changed

6 files changed

+24
-15
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
505505
}
506506

507507
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
508+
let pat = self.lower_pat(&arm.pat);
509+
let guard = arm.guard.as_ref().map(|cond| {
510+
if let ExprKind::Let(ref pat, ref scrutinee) = cond.kind {
511+
hir::Guard::IfLet(self.lower_pat(pat), self.lower_expr(scrutinee))
512+
} else {
513+
hir::Guard::If(self.lower_expr(cond))
514+
}
515+
});
508516
hir::Arm {
509517
hir_id: self.next_id(),
510518
attrs: self.lower_attrs(&arm.attrs),
511-
pat: self.lower_pat(&arm.pat),
512-
guard: match arm.guard {
513-
Some(ref x) => Some(hir::Guard::If(self.lower_expr(x))),
514-
_ => None,
515-
},
519+
pat,
520+
guard,
516521
body: self.lower_expr(&arm.body),
517522
span: arm.span,
518523
}

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,7 @@ pub struct Arm<'hir> {
11601160
#[derive(Debug, HashStable_Generic)]
11611161
pub enum Guard<'hir> {
11621162
If(&'hir Expr<'hir>),
1163+
IfLet(&'hir Pat<'hir>, &'hir Expr<'hir>),
11631164
}
11641165

11651166
#[derive(Debug, HashStable_Generic)]

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,10 @@ pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm<'v>) {
12281228
if let Some(ref g) = arm.guard {
12291229
match g {
12301230
Guard::If(ref e) => visitor.visit_expr(e),
1231+
Guard::IfLet(ref pat, ref e) => {
1232+
visitor.visit_pat(pat);
1233+
visitor.visit_expr(e);
1234+
}
12311235
}
12321236
}
12331237
visitor.visit_expr(&arm.body);

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,15 @@ impl<'a> State<'a> {
20022002
self.print_expr(&e);
20032003
self.s.space();
20042004
}
2005+
hir::Guard::IfLet(pat, e) => {
2006+
self.word_nbsp("if");
2007+
self.word_nbsp("let");
2008+
self.print_pat(&pat);
2009+
self.s.space();
2010+
self.word_space("=");
2011+
self.print_expr(&e);
2012+
self.s.space();
2013+
}
20052014
}
20062015
}
20072016
self.word_space("=>");

src/test/ui/rfc-2294-if-let-guard/feature-gate.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ fn _if_let_guard() {
66
match () {
77
() if let 0 = 1 => {}
88
//~^ ERROR `if let` guard is not implemented
9-
//~| ERROR `let` expressions are not supported here
109

1110
() if (let 0 = 1) => {}
1211
//~^ ERROR `let` expressions in this position are experimental

src/test/ui/rfc-2294-if-let-guard/feature-gate.stderr

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,6 @@ LL | use_expr!((let 0 = 1));
169169
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
170170
= help: add `#![feature(let_chains)]` to the crate attributes to enable
171171

172-
error: `let` expressions are not supported here
173-
--> $DIR/feature-gate.rs:7:15
174-
|
175-
LL | () if let 0 = 1 => {}
176-
| ^^^^^^^^^
177-
|
178-
= note: only supported directly in conditions of `if`- and `while`-expressions
179-
= note: as well as when nested within `&&` and parenthesis in those conditions
180-
181172
error: `let` expressions are not supported here
182173
--> $DIR/feature-gate.rs:11:16
183174
|

0 commit comments

Comments
 (0)