Skip to content

Commit 1713ac4

Browse files
varkordlrobertson
authored andcommitted
Initial implementation of or patterns
1 parent ac60ca0 commit 1713ac4

File tree

17 files changed

+134
-26
lines changed

17 files changed

+134
-26
lines changed

src/librustc/cfg/construct.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
140140
self.add_ast_node(pat.hir_id.local_id, &[pats_exit])
141141
}
142142

143+
PatKind::Or(ref pats) => {
144+
let branches: Vec<_> = pats.iter().map(|p| self.pat(p, pred)).collect();
145+
self.add_ast_node(pat.hir_id.local_id, &branches)
146+
}
147+
143148
PatKind::Slice(ref pre, ref vec, ref post) => {
144149
let pre_exit = self.pats_all(pre.iter(), pred);
145150
let vec_exit = self.pats_all(vec.iter(), pre_exit);

src/librustc/hir/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
709709
visitor.visit_pat(&field.pat)
710710
}
711711
}
712+
PatKind::Or(ref pats) => walk_list!(visitor, visit_pat, pats),
712713
PatKind::Tuple(ref tuple_elements, _) => {
713714
walk_list!(visitor, visit_pat, tuple_elements);
714715
}

src/librustc/hir/lowering.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,9 @@ impl<'a> LoweringContext<'a> {
26692669
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
26702670
hir::PatKind::TupleStruct(qpath, pats, ddpos)
26712671
}
2672+
PatKind::Or(ref pats) => {
2673+
hir::PatKind::Or(pats.iter().map(|x| self.lower_pat(x)).collect())
2674+
}
26722675
PatKind::Path(ref qself, ref path) => {
26732676
let qpath = self.lower_qpath(
26742677
p.id,

src/librustc/hir/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ impl Pat {
882882
PatKind::TupleStruct(_, ref s, _) | PatKind::Tuple(ref s, _) => {
883883
s.iter().all(|p| p.walk_(it))
884884
}
885+
PatKind::Or(ref pats) => pats.iter().all(|p| p.walk_(it)),
885886
PatKind::Box(ref s) | PatKind::Ref(ref s, _) => {
886887
s.walk_(it)
887888
}
@@ -976,6 +977,9 @@ pub enum PatKind {
976977
/// `0 <= position <= subpats.len()`
977978
TupleStruct(QPath, HirVec<P<Pat>>, Option<usize>),
978979

980+
/// An or-pattern `A | B | C`.
981+
Or(Vec<P<Pat>>),
982+
979983
/// A path pattern for an unit struct/variant or a (maybe-associated) constant.
980984
Path(QPath),
981985

src/librustc/hir/print.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use syntax::source_map::{SourceMap, Spanned};
44
use syntax::parse::ParseSess;
55
use syntax::print::pp::{self, Breaks};
66
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
7-
use syntax::print::pprust::{self, Comments, PrintState};
7+
use syntax::print::pprust::{self, Comments, PrintState, SeparatorSpacing};
88
use syntax::symbol::kw;
99
use syntax::util::parser::{self, AssocOp, Fixity};
1010
use syntax_pos::{self, BytePos, FileName};
@@ -1687,6 +1687,10 @@ impl<'a> State<'a> {
16871687
self.s.space();
16881688
self.s.word("}");
16891689
}
1690+
PatKind::Or(ref pats) => {
1691+
let spacing = SeparatorSpacing::Both;
1692+
self.strsep("|", spacing, Inconsistent, &pats[..], |s, p| s.print_pat(&p))?;
1693+
}
16901694
PatKind::Tuple(ref elts, ddpos) => {
16911695
self.popen();
16921696
if let Some(ddpos) = ddpos {

src/librustc/middle/mem_categorization.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,12 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
12901290
}
12911291
}
12921292

1293+
PatKind::Or(ref pats) => {
1294+
for pat in pats {
1295+
self.cat_pattern_(cmt.clone(), &pat, op)?;
1296+
}
1297+
}
1298+
12931299
PatKind::Binding(.., Some(ref subpat)) => {
12941300
self.cat_pattern_(cmt, &subpat, op)?;
12951301
}

src/librustc_mir/build/matches/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
657657
self.visit_bindings(&subpattern.pattern, subpattern_user_ty, f);
658658
}
659659
}
660+
PatternKind::Or { ref pats } => {
661+
// FIXME(#47184): extract or handle `pattern_user_ty` somehow
662+
for pat in pats {
663+
self.visit_bindings(&pat, &pattern_user_ty.clone(), f);
664+
}
665+
}
660666
}
661667
}
662668
}

src/librustc_mir/build/matches/simplify.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
195195
candidate.match_pairs.push(MatchPair::new(place, subpattern));
196196
Ok(())
197197
}
198+
199+
PatternKind::Or { .. } => {
200+
Err(match_pair)
201+
}
198202
}
199203
}
200204
}

src/librustc_mir/build/matches/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8787
PatternKind::AscribeUserType { .. } |
8888
PatternKind::Array { .. } |
8989
PatternKind::Wild |
90+
PatternKind::Or { .. } |
9091
PatternKind::Binding { .. } |
9192
PatternKind::Leaf { .. } |
9293
PatternKind::Deref { .. } => {
@@ -130,6 +131,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
130131
PatternKind::Slice { .. } |
131132
PatternKind::Array { .. } |
132133
PatternKind::Wild |
134+
PatternKind::Or { .. } |
133135
PatternKind::Binding { .. } |
134136
PatternKind::AscribeUserType { .. } |
135137
PatternKind::Leaf { .. } |

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@
7575
/// D((r_1, p_(i,2), .., p_(i,n)))
7676
/// D((r_2, p_(i,2), .., p_(i,n)))
7777
///
78-
/// Note that the OR-patterns are not always used directly in Rust, but are used to derive
79-
/// the exhaustive integer matching rules, so they're written here for posterity.
80-
///
8178
/// The algorithm for computing `U`
8279
/// -------------------------------
8380
/// The algorithm is inductive (on the number of columns: i.e., components of tuple patterns).
@@ -1359,6 +1356,9 @@ fn pat_constructors<'tcx>(cx: &mut MatchCheckCtxt<'_, 'tcx>,
13591356
Some(vec![Slice(pat_len)])
13601357
}
13611358
}
1359+
PatternKind::Or { .. } => {
1360+
bug!("support for or-patterns has not been fully implemented yet.");
1361+
}
13621362
}
13631363
}
13641364

@@ -1884,6 +1884,10 @@ fn specialize<'p, 'a: 'p, 'tcx>(
18841884
"unexpected ctor {:?} for slice pat", constructor)
18851885
}
18861886
}
1887+
1888+
PatternKind::Or { .. } => {
1889+
bug!("support for or-patterns has not been fully implemented yet.");
1890+
}
18871891
};
18881892
debug!("specialize({:#?}, {:#?}) = {:#?}", r[0], wild_patterns, head);
18891893

0 commit comments

Comments
 (0)