Skip to content

Commit a72c632

Browse files
committed
add guard patterns to THIR
1 parent 2ecce0b commit a72c632

File tree

8 files changed

+34
-3
lines changed

8 files changed

+34
-3
lines changed

Diff for: compiler/rustc_middle/src/thir.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,8 @@ impl<'tcx> Pat<'tcx> {
640640
| Binding { subpattern: Some(subpattern), .. }
641641
| Deref { subpattern }
642642
| DerefPattern { subpattern, .. }
643-
| InlineConstant { subpattern, .. } => subpattern.walk_(it),
643+
| InlineConstant { subpattern, .. }
644+
| Guard { subpattern, .. } => subpattern.walk_(it),
644645
Leaf { subpatterns } | Variant { subpatterns, .. } => {
645646
subpatterns.iter().for_each(|field| field.pattern.walk_(it))
646647
}
@@ -823,6 +824,13 @@ pub enum PatKind<'tcx> {
823824
pats: Box<[Box<Pat<'tcx>>]>,
824825
},
825826

827+
/// A guard pattern, e.g. `x if guard(x)`.
828+
Guard {
829+
subpattern: Box<Pat<'tcx>>,
830+
#[type_visitable(ignore)]
831+
condition: ExprId,
832+
},
833+
826834
/// A never pattern `!`.
827835
Never,
828836

Diff for: compiler/rustc_middle/src/thir/visit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,9 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
256256
visitor.visit_pat(pat);
257257
}
258258
}
259+
Guard { subpattern, condition } => {
260+
visitor.visit_pat(subpattern);
261+
visitor.visit_expr(&visitor.thir()[*condition]);
262+
}
259263
};
260264
}

Diff for: 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

@@ -248,6 +249,10 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
248249
}
249250

250251
PatKind::Never => TestCase::Never,
252+
253+
PatKind::Guard { .. } => {
254+
span_bug!(pattern.span, "MIR lowering is not yet implemented for guard patterns")
255+
}
251256
};
252257

253258
MatchPairTree { place, test_case, subpairs, pattern }

Diff for: compiler/rustc_mir_build/src/build/matches/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
936936
self.visit_primary_bindings(subpattern, pattern_user_ty, f)
937937
}
938938

939+
PatKind::Guard { ref subpattern, .. } => {
940+
self.visit_primary_bindings(subpattern, pattern_user_ty, f);
941+
}
942+
939943
PatKind::Leaf { ref subpatterns } => {
940944
for subpattern in subpatterns {
941945
let subpattern_user_ty = pattern_user_ty.clone().leaf(subpattern.field);

Diff for: 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
};

Diff for: 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),

Diff for: compiler/rustc_mir_build/src/thir/print.rs

+8
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,14 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
763763
print_indented!(self, "]", depth_lvl + 2);
764764
print_indented!(self, "}", depth_lvl + 1);
765765
}
766+
PatKind::Guard { subpattern, condition } => {
767+
print_indented!(self, "Guard {", depth_lvl + 1);
768+
print_indented!(self, "subpattern:", depth_lvl + 2);
769+
self.print_pat(subpattern, depth_lvl + 3);
770+
print_indented!(self, "condition:", depth_lvl + 2);
771+
self.print_expr(*condition, depth_lvl + 3);
772+
print_indented!(self, "}", depth_lvl + 1);
773+
}
766774
PatKind::Error(_) => {
767775
print_indented!(self, "Error", depth_lvl + 1);
768776
}

Diff for: 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)