Skip to content

Commit 354fbe4

Browse files
committed
add guard patterns to THIR
1 parent 3ddd186 commit 354fbe4

File tree

8 files changed

+34
-3
lines changed

8 files changed

+34
-3
lines changed

compiler/rustc_middle/src/thir.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ impl<'tcx> Pat<'tcx> {
645645
| Binding { subpattern: Some(subpattern), .. }
646646
| Deref { subpattern }
647647
| DerefPattern { subpattern, .. }
648-
| InlineConstant { subpattern, .. } => subpattern.walk_(it),
648+
| InlineConstant { subpattern, .. }
649+
| Guard { subpattern, .. } => subpattern.walk_(it),
649650
Leaf { subpatterns } | Variant { subpatterns, .. } => {
650651
subpatterns.iter().for_each(|field| field.pattern.walk_(it))
651652
}
@@ -828,6 +829,13 @@ pub enum PatKind<'tcx> {
828829
pats: Box<[Box<Pat<'tcx>>]>,
829830
},
830831

832+
/// A guard pattern, e.g. `x if guard(x)`.
833+
Guard {
834+
subpattern: Box<Pat<'tcx>>,
835+
#[type_visitable(ignore)]
836+
condition: ExprId,
837+
},
838+
831839
/// A never pattern `!`.
832840
Never,
833841

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,9 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
265265
visitor.visit_pat(pat);
266266
}
267267
}
268+
Guard { subpattern, condition } => {
269+
visitor.visit_pat(subpattern);
270+
visitor.visit_expr(&visitor.thir()[*condition]);
271+
}
268272
};
269273
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_middle::mir::*;
2+
use rustc_middle::span_bug;
23
use rustc_middle::thir::{self, *};
34
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
45

@@ -250,6 +251,10 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
250251
}
251252

252253
PatKind::Never => TestCase::Never,
254+
255+
PatKind::Guard { .. } => {
256+
span_bug!(pattern.span, "MIR lowering is not yet implemented for guard patterns")
257+
}
253258
};
254259

255260
MatchPairTree { place, test_case, subpairs, pattern }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
921921
self.visit_primary_bindings(subpattern, pattern_user_ty, f)
922922
}
923923

924+
PatKind::Guard { ref subpattern, .. } => {
925+
self.visit_primary_bindings(subpattern, pattern_user_ty, f);
926+
}
927+
924928
PatKind::Leaf { ref subpatterns } => {
925929
for subpattern in subpatterns {
926930
let subpattern_user_ty = pattern_user_ty.clone().leaf(subpattern.field);

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
334334
PatKind::Or { .. } |
335335
PatKind::InlineConstant { .. } |
336336
PatKind::AscribeUserType { .. } |
337+
PatKind::Guard { .. } |
337338
PatKind::Error(_) => {}
338339
}
339340
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
397397
hir::PatKind::Or(pats) => PatKind::Or { pats: self.lower_patterns(pats) },
398398

399399
hir::PatKind::Guard(..) => {
400-
span_bug!(pat.span, "lowering guard patterns to MIR is not yet implemented");
400+
span_bug!(pat.span, "lowering guard patterns to THIR is not yet implemented");
401401
}
402402

403403
hir::PatKind::Err(guar) => PatKind::Error(guar),

compiler/rustc_mir_build/src/thir/print.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,14 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
770770
print_indented!(self, "]", depth_lvl + 2);
771771
print_indented!(self, "}", depth_lvl + 1);
772772
}
773+
PatKind::Guard { subpattern, condition } => {
774+
print_indented!(self, "Guard {", depth_lvl + 1);
775+
print_indented!(self, "subpattern:", depth_lvl + 2);
776+
self.print_pat(subpattern, depth_lvl + 3);
777+
print_indented!(self, "condition:", depth_lvl + 2);
778+
self.print_expr(*condition, depth_lvl + 3);
779+
print_indented!(self, "}", depth_lvl + 1);
780+
}
773781
PatKind::Error(_) => {
774782
print_indented!(self, "Error", depth_lvl + 1);
775783
}

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
453453
let fields: Vec<_>;
454454
match &pat.kind {
455455
PatKind::AscribeUserType { subpattern, .. }
456-
| PatKind::InlineConstant { subpattern, .. } => return self.lower_pat(subpattern),
456+
| PatKind::InlineConstant { subpattern, .. }
457+
| PatKind::Guard { subpattern, .. } => return self.lower_pat(subpattern),
457458
PatKind::Binding { subpattern: Some(subpat), .. } => return self.lower_pat(subpat),
458459
PatKind::Binding { subpattern: None, .. } | PatKind::Wild => {
459460
ctor = Wildcard;

0 commit comments

Comments
 (0)