Skip to content

Commit 9ad2044

Browse files
committed
Start enabling empty types in pattern matching.
Remove the assumption at the start of is_useful that any suitably-long array of wildcard patterns is useful relative the any empty vector. Instead we just continue to recurse column-wise over the matrix. This assumption is false in the presence of empty types. eg. in the simplest case: let x: ! = ...; match x { // This pattern should not be considered useful by the algorithm _ => ... }
1 parent 1659d65 commit 9ad2044

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/librustc_const_eval/_match.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,24 @@ pub fn is_useful<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
510510
-> Usefulness<'tcx> {
511511
let &Matrix(ref rows) = matrix;
512512
debug!("is_useful({:?}, {:?})", matrix, v);
513-
if rows.is_empty() {
514-
return match witness {
515-
ConstructWitness => UsefulWithWitness(vec![Witness(
516-
repeat(cx.wild_pattern).take(v.len()).cloned().collect()
517-
)]),
518-
LeaveOutWitness => Useful
519-
};
520-
}
521-
if rows[0].is_empty() {
522-
return NotUseful;
523-
}
524513

525-
let &Matrix(ref rows) = matrix;
514+
// The base case. We are pattern-matching on () and the return value is
515+
// based on whether our matrix has a row or not.
516+
// NOTE: This could potentially be optimized by checking rows.is_empty()
517+
// first and then, if v is non-empty, the return value is based on whether
518+
// the type of the tuple we're checking is inhabited or not.
519+
if v.is_empty() {
520+
return if rows.is_empty() {
521+
match witness {
522+
ConstructWitness => UsefulWithWitness(vec![Witness(vec![])]),
523+
LeaveOutWitness => Useful,
524+
}
525+
}
526+
else {
527+
NotUseful
528+
}
529+
};
530+
526531
assert!(rows.iter().all(|r| r.len() == v.len()));
527532

528533

0 commit comments

Comments
 (0)