Skip to content

Commit a549711

Browse files
committed
Remove thir::Guard
Use Expr instead. Use `ExprKind::Let` to represent if let guards.
1 parent 407cb24 commit a549711

File tree

7 files changed

+55
-74
lines changed

7 files changed

+55
-74
lines changed

compiler/rustc_middle/src/thir.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -519,20 +519,13 @@ pub struct FruInfo<'tcx> {
519519
#[derive(Clone, Debug, HashStable)]
520520
pub struct Arm<'tcx> {
521521
pub pattern: Box<Pat<'tcx>>,
522-
pub guard: Option<Guard<'tcx>>,
522+
pub guard: Option<ExprId>,
523523
pub body: ExprId,
524524
pub lint_level: LintLevel,
525525
pub scope: region::Scope,
526526
pub span: Span,
527527
}
528528

529-
/// A `match` guard.
530-
#[derive(Clone, Debug, HashStable)]
531-
pub enum Guard<'tcx> {
532-
If(ExprId),
533-
IfLet(Box<Pat<'tcx>>, ExprId),
534-
}
535-
536529
#[derive(Copy, Clone, Debug, HashStable)]
537530
pub enum LogicalOp {
538531
/// The `&&` operator.

compiler/rustc_middle/src/thir/visit.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{
2-
AdtExpr, Arm, Block, ClosureExpr, Expr, ExprKind, Guard, InlineAsmExpr, InlineAsmOperand, Pat,
2+
AdtExpr, Arm, Block, ClosureExpr, Expr, ExprKind, InlineAsmExpr, InlineAsmOperand, Pat,
33
PatKind, Stmt, StmtKind, Thir,
44
};
55

@@ -213,13 +213,8 @@ pub fn walk_arm<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
213213
visitor: &mut V,
214214
arm: &'thir Arm<'tcx>,
215215
) {
216-
match arm.guard {
217-
Some(Guard::If(expr)) => visitor.visit_expr(&visitor.thir()[expr]),
218-
Some(Guard::IfLet(ref pat, expr)) => {
219-
visitor.visit_pat(pat);
220-
visitor.visit_expr(&visitor.thir()[expr]);
221-
}
222-
None => {}
216+
if let Some(expr) = arm.guard {
217+
visitor.visit_expr(&visitor.thir()[expr])
223218
}
224219
visitor.visit_pat(&arm.pattern);
225220
visitor.visit_expr(&visitor.thir()[arm.body]);

compiler/rustc_mir_build/src/build/matches/mod.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
425425
None,
426426
arm.span,
427427
&arm.pattern,
428-
arm.guard.as_ref(),
428+
arm.guard,
429429
opt_scrutinee_place,
430430
);
431431

@@ -717,7 +717,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
717717
mut visibility_scope: Option<SourceScope>,
718718
scope_span: Span,
719719
pattern: &Pat<'tcx>,
720-
guard: Option<&Guard<'tcx>>,
720+
guard: Option<ExprId>,
721721
opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
722722
) -> Option<SourceScope> {
723723
self.visit_primary_bindings(
@@ -745,7 +745,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
745745
);
746746
},
747747
);
748-
if let Some(&Guard::If(guard_expr)) = guard {
748+
if let Some(guard_expr) = guard {
749749
self.declare_guard_bindings(guard_expr, scope_span, visibility_scope);
750750
}
751751
visibility_scope
@@ -2044,7 +2044,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
20442044
// * So we eagerly create the reference for the arm and then take a
20452045
// reference to that.
20462046
if let Some((arm, match_scope)) = arm_match_scope
2047-
&& let Some(guard) = &arm.guard
2047+
&& let Some(guard) = arm.guard
20482048
{
20492049
let tcx = self.tcx;
20502050
let bindings = parent_bindings
@@ -2069,22 +2069,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
20692069
let mut guard_span = rustc_span::DUMMY_SP;
20702070

20712071
let (post_guard_block, otherwise_post_guard_block) =
2072-
self.in_if_then_scope(match_scope, guard_span, |this| match *guard {
2073-
Guard::If(e) => {
2074-
guard_span = this.thir[e].span;
2075-
this.then_else_break(
2076-
block,
2077-
e,
2078-
None,
2079-
match_scope,
2080-
this.source_info(arm.span),
2081-
false,
2082-
)
2083-
}
2084-
Guard::IfLet(ref pat, s) => {
2085-
guard_span = this.thir[s].span;
2086-
this.lower_let_expr(block, s, pat, match_scope, None, arm.span, false)
2087-
}
2072+
self.in_if_then_scope(match_scope, guard_span, |this| {
2073+
guard_span = this.thir[guard].span;
2074+
this.then_else_break(
2075+
block,
2076+
guard,
2077+
None,
2078+
match_scope,
2079+
this.source_info(arm.span),
2080+
false,
2081+
)
20882082
});
20892083

20902084
let source_info = self.source_info(guard_span);

compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ impl<'tcx> Cx<'tcx> {
856856
fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
857857
let arm = Arm {
858858
pattern: self.pattern_from_hir(&arm.pat),
859-
guard: arm.guard.as_ref().map(|g| Guard::If(self.mirror_expr(g))),
859+
guard: arm.guard.as_ref().map(|g| self.mirror_expr(g)),
860860
body: self.mirror_expr(arm.body),
861861
lint_level: LintLevel::Explicit(arm.hir_id),
862862
scope: region::Scope { id: arm.hir_id.local_id, data: region::ScopeData::Node },

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,10 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
100100
#[instrument(level = "trace", skip(self))]
101101
fn visit_arm(&mut self, arm: &'p Arm<'tcx>) {
102102
self.with_lint_level(arm.lint_level, |this| {
103-
match arm.guard {
104-
Some(Guard::If(expr)) => {
105-
this.with_let_source(LetSource::IfLetGuard, |this| {
106-
this.visit_expr(&this.thir[expr])
107-
});
108-
}
109-
Some(Guard::IfLet(ref pat, expr)) => {
110-
this.with_let_source(LetSource::IfLetGuard, |this| {
111-
this.check_let(pat, Some(expr), pat.span);
112-
this.visit_pat(pat);
113-
this.visit_expr(&this.thir[expr]);
114-
});
115-
}
116-
None => {}
103+
if let Some(expr) = arm.guard {
104+
this.with_let_source(LetSource::IfLetGuard, |this| {
105+
this.visit_expr(&this.thir[expr])
106+
});
117107
}
118108
this.visit_pat(&arm.pattern);
119109
this.visit_expr(&self.thir[arm.body]);

compiler/rustc_mir_build/src/thir/print.rs

+2-23
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,9 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
593593
print_indented!(self, "pattern: ", depth_lvl + 1);
594594
self.print_pat(pattern, depth_lvl + 2);
595595

596-
if let Some(guard) = guard {
596+
if let Some(guard) = *guard {
597597
print_indented!(self, "guard: ", depth_lvl + 1);
598-
self.print_guard(guard, depth_lvl + 2);
598+
self.print_expr(guard, depth_lvl + 2);
599599
} else {
600600
print_indented!(self, "guard: None", depth_lvl + 1);
601601
}
@@ -764,27 +764,6 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
764764
print_indented!(self, "}", depth_lvl);
765765
}
766766

767-
fn print_guard(&mut self, guard: &Guard<'tcx>, depth_lvl: usize) {
768-
print_indented!(self, "Guard {", depth_lvl);
769-
770-
match guard {
771-
Guard::If(expr_id) => {
772-
print_indented!(self, "If (", depth_lvl + 1);
773-
self.print_expr(*expr_id, depth_lvl + 2);
774-
print_indented!(self, ")", depth_lvl + 1);
775-
}
776-
Guard::IfLet(pat, expr_id) => {
777-
print_indented!(self, "IfLet (", depth_lvl + 1);
778-
self.print_pat(pat, depth_lvl + 2);
779-
print_indented!(self, ",", depth_lvl + 1);
780-
self.print_expr(*expr_id, depth_lvl + 2);
781-
print_indented!(self, ")", depth_lvl + 1);
782-
}
783-
}
784-
785-
print_indented!(self, "}", depth_lvl);
786-
}
787-
788767
fn print_closure_expr(&mut self, expr: &ClosureExpr<'tcx>, depth_lvl: usize) {
789768
let ClosureExpr { closure_id, args, upvars, movability, fake_reads } = expr;
790769

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Tests for #88015 when using if let chains in match guards
2+
3+
//run-pass
4+
5+
#![feature(if_let_guard)]
6+
#![feature(let_chains)]
7+
#![allow(irrefutable_let_patterns)]
8+
9+
fn lhs_let(opt: Option<bool>) {
10+
match opt {
11+
None | Some(false) | Some(true) if let x = 42 && true => assert_eq!(x, 42),
12+
_ => panic!()
13+
}
14+
}
15+
16+
fn rhs_let(opt: Option<bool>) {
17+
match opt {
18+
None | Some(false) | Some(true) if true && let x = 41 => assert_eq!(x, 41),
19+
_ => panic!()
20+
}
21+
}
22+
23+
fn main() {
24+
lhs_let(None);
25+
lhs_let(Some(false));
26+
lhs_let(Some(true));
27+
rhs_let(None);
28+
rhs_let(Some(false));
29+
rhs_let(Some(true));
30+
}

0 commit comments

Comments
 (0)