Skip to content

Commit b669730

Browse files
committed
Don't use max_slice_length when subtracting from VarLenSlice
This alters error messages slightly, but that'll be improved later
1 parent 909ec37 commit b669730

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ impl<'tcx> Constructor<'tcx> {
658658
// anything in `other_ctors`.
659659
fn subtract_ctors(
660660
&self,
661-
pcx: PatCtxt<'tcx>,
662661
tcx: TyCtxt<'tcx>,
663662
param_env: ty::ParamEnv<'tcx>,
664663
other_ctors: &Vec<Constructor<'tcx>>,
@@ -681,11 +680,7 @@ impl<'tcx> Constructor<'tcx> {
681680
if other_ctors.iter().any(overlaps) { vec![] } else { vec![self.clone()] }
682681
}
683682
VarLenSlice(_) => {
684-
let mut remaining_ctors = if let VarLenSlice(len) = self {
685-
(*len..pcx.max_slice_length + 1).map(FixedLenSlice).collect()
686-
} else {
687-
vec![self.clone()]
688-
};
683+
let mut remaining_ctors = vec![self.clone()];
689684

690685
// For each used ctor, subtract from the current set of constructors.
691686
// Naming: we remove the "neg" constructors from the "pos" ones.
@@ -704,6 +699,23 @@ impl<'tcx> Constructor<'tcx> {
704699
smallvec![pos_ctor]
705700
}
706701
}
702+
(VarLenSlice(pos_len), VarLenSlice(neg_len)) => {
703+
if neg_len <= pos_len {
704+
smallvec![]
705+
} else {
706+
(*pos_len..*neg_len).map(FixedLenSlice).collect()
707+
}
708+
}
709+
(VarLenSlice(pos_len), FixedLenSlice(neg_len)) => {
710+
if neg_len < pos_len {
711+
smallvec![pos_ctor]
712+
} else {
713+
(*pos_len..*neg_len)
714+
.map(FixedLenSlice)
715+
.chain(Some(VarLenSlice(neg_len + 1)))
716+
.collect()
717+
}
718+
}
707719
_ if pos_ctor == *neg_ctor => smallvec![],
708720
_ => smallvec![pos_ctor],
709721
}
@@ -1456,7 +1468,6 @@ impl<'tcx> IntRange<'tcx> {
14561468

14571469
// A struct to compute a set of constructors equivalent to `all_ctors \ used_ctors`.
14581470
struct MissingConstructors<'tcx> {
1459-
pcx: PatCtxt<'tcx>,
14601471
tcx: TyCtxt<'tcx>,
14611472
param_env: ty::ParamEnv<'tcx>,
14621473
all_ctors: Vec<Constructor<'tcx>>,
@@ -1465,13 +1476,12 @@ struct MissingConstructors<'tcx> {
14651476

14661477
impl<'tcx> MissingConstructors<'tcx> {
14671478
fn new(
1468-
pcx: PatCtxt<'tcx>,
14691479
tcx: TyCtxt<'tcx>,
14701480
param_env: ty::ParamEnv<'tcx>,
14711481
all_ctors: Vec<Constructor<'tcx>>,
14721482
used_ctors: Vec<Constructor<'tcx>>,
14731483
) -> Self {
1474-
MissingConstructors { pcx, tcx, param_env, all_ctors, used_ctors }
1484+
MissingConstructors { tcx, param_env, all_ctors, used_ctors }
14751485
}
14761486

14771487
fn into_inner(self) -> (Vec<Constructor<'tcx>>, Vec<Constructor<'tcx>>) {
@@ -1490,7 +1500,7 @@ impl<'tcx> MissingConstructors<'tcx> {
14901500
/// Iterate over all_ctors \ used_ctors
14911501
fn iter<'a>(&'a self) -> impl Iterator<Item = Constructor<'tcx>> + Captures<'a> {
14921502
self.all_ctors.iter().flat_map(move |req_ctor| {
1493-
req_ctor.subtract_ctors(self.pcx, self.tcx, self.param_env, &self.used_ctors)
1503+
req_ctor.subtract_ctors(self.tcx, self.param_env, &self.used_ctors)
14941504
})
14951505
}
14961506
}
@@ -1633,8 +1643,7 @@ pub fn is_useful<'p, 'a, 'tcx>(
16331643
// non-wildcard patterns in the current column. To determine if
16341644
// the set is empty, we can check that `.peek().is_none()`, so
16351645
// we only fully construct them on-demand, because they're rarely used and can be big.
1636-
let missing_ctors =
1637-
MissingConstructors::new(pcx, cx.tcx, cx.param_env, all_ctors, used_ctors);
1646+
let missing_ctors = MissingConstructors::new(cx.tcx, cx.param_env, all_ctors, used_ctors);
16381647

16391648
debug!(
16401649
"missing_ctors.empty()={:#?} is_privately_empty={:#?} is_declared_nonexhaustive={:#?}",

src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | match buf {
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

9-
error[E0004]: non-exhaustive patterns: `&[]`, `&[_]`, `&[_, _]` and 3 more not covered
9+
error[E0004]: non-exhaustive patterns: `&[]` not covered
1010
--> $DIR/match-byte-array-patterns-2.rs:10:11
1111
|
1212
LL | match buf {
13-
| ^^^ patterns `&[]`, `&[_]`, `&[_, _]` and 3 more not covered
13+
| ^^^ pattern `&[]` not covered
1414
|
1515
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1616

0 commit comments

Comments
 (0)