Skip to content

Commit ef4394d

Browse files
committed
add guard patterns to THIR
1 parent 64bc33f commit ef4394d

File tree

8 files changed

+34
-3
lines changed

8 files changed

+34
-3
lines changed

compiler/rustc_middle/src/thir.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ impl<'tcx> Pat<'tcx> {
644644
| Binding { subpattern: Some(subpattern), .. }
645645
| Deref { subpattern }
646646
| DerefPattern { subpattern, .. }
647-
| InlineConstant { subpattern, .. } => subpattern.walk_(it),
647+
| InlineConstant { subpattern, .. }
648+
| Guard { subpattern, .. } => subpattern.walk_(it),
648649
Leaf { subpatterns } | Variant { subpatterns, .. } => {
649650
subpatterns.iter().for_each(|field| field.pattern.walk_(it))
650651
}
@@ -827,6 +828,13 @@ pub enum PatKind<'tcx> {
827828
pats: Box<[Box<Pat<'tcx>>]>,
828829
},
829830

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

compiler/rustc_middle/src/thir/visit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,9 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
259259
visitor.visit_pat(pat);
260260
}
261261
}
262+
Guard { subpattern, condition } => {
263+
visitor.visit_pat(subpattern);
264+
visitor.visit_expr(&visitor.thir()[*condition]);
265+
}
262266
};
263267
}

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

+5
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

+4
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

+1
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

+1-1
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

+8
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

+2-1
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)