Skip to content

Commit b579c36

Browse files
committed
add guard patterns to HIR and implement lowering
1 parent d117b7f commit b579c36

File tree

11 files changed

+40
-7
lines changed

11 files changed

+40
-7
lines changed

compiler/rustc_ast_lowering/src/pat.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
120120
self.lower_range_end(end, e2.is_some()),
121121
);
122122
}
123-
// FIXME(guard_patterns): lower pattern guards to HIR
124-
PatKind::Guard(inner, _) => pattern = inner,
123+
PatKind::Guard(inner, cond) => {
124+
break hir::PatKind::Guard(self.lower_pat(inner), self.lower_expr(cond));
125+
}
125126
PatKind::Slice(pats) => break self.lower_pat_slice(pats),
126127
PatKind::Rest => {
127128
// If we reach here the `..` pattern is not semantically allowed.

compiler/rustc_hir/src/hir.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ impl<'hir> Pat<'hir> {
13851385
use PatKind::*;
13861386
match self.kind {
13871387
Wild | Never | Lit(_) | Range(..) | Binding(.., None) | Path(_) | Err(_) => true,
1388-
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) => s.walk_short_(it),
1388+
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_short_(it),
13891389
Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk_short_(it)),
13901390
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().all(|p| p.walk_short_(it)),
13911391
Slice(before, slice, after) => {
@@ -1412,7 +1412,7 @@ impl<'hir> Pat<'hir> {
14121412
use PatKind::*;
14131413
match self.kind {
14141414
Wild | Never | Lit(_) | Range(..) | Binding(.., None) | Path(_) | Err(_) => {}
1415-
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) => s.walk_(it),
1415+
Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_(it),
14161416
Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk_(it)),
14171417
TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().for_each(|p| p.walk_(it)),
14181418
Slice(before, slice, after) => {
@@ -1564,6 +1564,9 @@ pub enum PatKind<'hir> {
15641564
/// A literal.
15651565
Lit(&'hir Expr<'hir>),
15661566

1567+
/// A guard pattern (e.g., `x if guard(x)`).
1568+
Guard(&'hir Pat<'hir>, &'hir Expr<'hir>),
1569+
15671570
/// A range pattern (e.g., `1..=2` or `1..2`).
15681571
Range(Option<&'hir Expr<'hir>>, Option<&'hir Expr<'hir>>, RangeEnd),
15691572

compiler/rustc_hir/src/intravisit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,10 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) -> V:
696696
visit_opt!(visitor, visit_pat, slice_pattern);
697697
walk_list!(visitor, visit_pat, postpatterns);
698698
}
699+
PatKind::Guard(subpat, condition) => {
700+
try_visit!(visitor.visit_pat(subpat));
701+
try_visit!(visitor.visit_expr(condition));
702+
}
699703
}
700704
V::Result::output()
701705
}

compiler/rustc_hir_analysis/src/check/region.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ fn resolve_local<'tcx>(
654654
/// | ( ..., P&, ... )
655655
/// | ... "|" P& "|" ...
656656
/// | box P&
657+
/// | P& if ...
657658
/// ```
658659
fn is_binding_pat(pat: &hir::Pat<'_>) -> bool {
659660
// Note that the code below looks for *explicit* refs only, that is, it won't
@@ -694,7 +695,9 @@ fn resolve_local<'tcx>(
694695
| PatKind::TupleStruct(_, subpats, _)
695696
| PatKind::Tuple(subpats, _) => subpats.iter().any(|p| is_binding_pat(p)),
696697

697-
PatKind::Box(subpat) | PatKind::Deref(subpat) => is_binding_pat(subpat),
698+
PatKind::Box(subpat) | PatKind::Deref(subpat) | PatKind::Guard(subpat, _) => {
699+
is_binding_pat(subpat)
700+
}
698701

699702
PatKind::Ref(_, _)
700703
| PatKind::Binding(hir::BindingMode(hir::ByRef::No, _), ..)

compiler/rustc_hir_pretty/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,12 @@ impl<'a> State<'a> {
19991999
self.commasep(Inconsistent, after, |s, p| s.print_pat(p));
20002000
self.word("]");
20012001
}
2002+
PatKind::Guard(inner, cond) => {
2003+
self.print_pat(inner);
2004+
self.space();
2005+
self.word_space("if");
2006+
self.print_expr(cond);
2007+
}
20022008
PatKind::Err(_) => {
20032009
self.popen();
20042010
self.word("/*ERROR*/");

compiler/rustc_hir_typeck/src/expr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
457457
// Does not constitute a read.
458458
hir::PatKind::Wild => false,
459459

460+
// Might not constitute a read, since the condition might be false.
461+
hir::PatKind::Guard(_, _) => true,
462+
460463
// This is unnecessarily restrictive when the pattern that doesn't
461464
// constitute a read is unreachable.
462465
//

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
615615
| PatKind::Box(_)
616616
| PatKind::Deref(_)
617617
| PatKind::Ref(..)
618+
| PatKind::Guard(..)
618619
| PatKind::Wild
619620
| PatKind::Err(_) => {
620621
// If the PatKind is Or, Box, or Ref, the decision is made later
@@ -1737,7 +1738,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
17371738
}
17381739
}
17391740

1740-
PatKind::Binding(.., Some(subpat)) => {
1741+
PatKind::Binding(.., Some(subpat)) | PatKind::Guard(subpat, _) => {
17411742
self.cat_pattern(place_with_id, subpat, op)?;
17421743
}
17431744

compiler/rustc_hir_typeck/src/pat.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
284284
PatKind::Struct(ref qpath, fields, has_rest_pat) => {
285285
self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, pat_info)
286286
}
287+
PatKind::Guard(pat, _) => {
288+
self.check_pat(pat, expected, pat_info);
289+
expected
290+
}
287291
PatKind::Or(pats) => {
288292
for pat in pats {
289293
self.check_pat(pat, expected, pat_info);
@@ -422,7 +426,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
422426
// An OR-pattern just propagates to each individual alternative.
423427
// This is maximally flexible, allowing e.g., `Some(mut x) | &Some(mut x)`.
424428
// In that example, `Some(mut x)` results in `Peel` whereas `&Some(mut x)` in `Reset`.
425-
| PatKind::Or(_) => AdjustMode::Pass,
429+
| PatKind::Or(_)
430+
// Like or-patterns, guard patterns just propogate to their subpatterns.
431+
| PatKind::Guard(..) => AdjustMode::Pass,
426432
}
427433
}
428434

@@ -901,6 +907,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
901907
PatKind::Struct(..)
902908
| PatKind::TupleStruct(..)
903909
| PatKind::Or(..)
910+
| PatKind::Guard(..)
904911
| PatKind::Tuple(..)
905912
| PatKind::Slice(..) => "binding",
906913

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

+3
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
435435

436436
hir::PatKind::Or(pats) => PatKind::Or { pats: self.lower_patterns(pats) },
437437

438+
// FIXME(guard_patterns): implement guard pattern lowering
439+
hir::PatKind::Guard(pat, _) => self.lower_pattern(pat).kind,
440+
438441
hir::PatKind::Err(guar) => PatKind::Error(guar),
439442
};
440443

compiler/rustc_passes/src/input_stats.rs

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
305305
Deref,
306306
Ref,
307307
Lit,
308+
Guard,
308309
Range,
309310
Slice,
310311
Err

src/librustdoc/clean/utils.rs

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ pub(crate) fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
320320
);
321321
return Symbol::intern("()");
322322
}
323+
PatKind::Guard(p, _) => return name_from_pat(&*p),
323324
PatKind::Range(..) => return kw::Underscore,
324325
PatKind::Slice(begin, ref mid, end) => {
325326
let begin = begin.iter().map(|p| name_from_pat(p).to_string());

0 commit comments

Comments
 (0)