Skip to content

Commit 5510f55

Browse files
committed
Use appropriate constructor for const slices
1 parent fa4a4d3 commit 5510f55

6 files changed

+23
-25
lines changed

src/librustc_mir/hair/pattern/_match.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ impl<'tcx> Constructor<'tcx> {
747747
.iter()
748748
.filter_map(|c: &Constructor<'_>| match c {
749749
Slice(slice) => Some(*slice),
750-
// FIXME(#65413): We ignore `ConstantValue`s here.
750+
// FIXME(oli-obk): implement `deref` for `ConstValue`
751751
ConstantValue(..) => None,
752752
_ => bug!("bad slice pattern constructor {:?}", c),
753753
})
@@ -1771,7 +1771,19 @@ fn pat_constructor<'tcx>(
17711771
if let Some(int_range) = IntRange::from_const(tcx, param_env, value, pat.span) {
17721772
Some(IntRange(int_range))
17731773
} else {
1774-
Some(ConstantValue(value))
1774+
match (value.val, &value.ty.kind) {
1775+
(_, ty::Array(_, n)) => {
1776+
let len = n.eval_usize(tcx, param_env);
1777+
Some(Slice(Slice { array_len: Some(len), kind: FixedLen(len) }))
1778+
}
1779+
(ty::ConstKind::Value(ConstValue::Slice { start, end, .. }), ty::Slice(_)) => {
1780+
let len = (end - start) as u64;
1781+
Some(Slice(Slice { array_len: None, kind: FixedLen(len) }))
1782+
}
1783+
// FIXME(oli-obk): implement `deref` for `ConstValue`
1784+
// (ty::ConstKind::Value(ConstValue::ByRef { .. }), ty::Slice(_)) => { ... }
1785+
_ => Some(ConstantValue(value)),
1786+
}
17751787
}
17761788
}
17771789
PatKind::Range(PatRange { lo, hi, end }) => {

src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// check-pass
12
#![feature(slice_patterns)]
23
#![deny(unreachable_patterns)]
34

@@ -8,8 +9,7 @@ fn main() {
89
match x {
910
&[] => {}
1011
&[1..=255] => {}
11-
// this shouldn't be unreachable
12-
C0 => {} //~ unreachable pattern
12+
C0 => {}
1313
&[_, _, ..] => {}
1414
}
1515
}

src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.stderr

-14
This file was deleted.

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error[E0004]: non-exhaustive patterns: `&[..]` not covered
1+
error[E0004]: non-exhaustive patterns: `&[0u8..=64u8, _, _, _]` and `&[66u8..=std::u8::MAX, _, _, _]` not covered
22
--> $DIR/match-byte-array-patterns-2.rs:4:11
33
|
44
LL | match buf {
5-
| ^^^ pattern `&[..]` not covered
5+
| ^^^ patterns `&[0u8..=64u8, _, _, _]` and `&[66u8..=std::u8::MAX, _, _, _]` not covered
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: `&[..]` not covered
9+
error[E0004]: non-exhaustive patterns: `&[]`, `&[_]`, `&[_, _]` and 2 more not covered
1010
--> $DIR/match-byte-array-patterns-2.rs:10:11
1111
|
1212
LL | match buf {
13-
| ^^^ pattern `&[..]` not covered
13+
| ^^^ patterns `&[]`, `&[_]`, `&[_, _]` and 2 more not covered
1414
|
1515
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1616

src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn main() {
9797
}
9898
const CONST1: &[bool; 1] = &[true];
9999
match s1 {
100-
//~^ ERROR `&[..]` not covered
100+
//~^ ERROR `&[false]` not covered
101101
CONST1 => {}
102102
}
103103
match s1 {

src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ LL | match s {
118118
|
119119
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
120120

121-
error[E0004]: non-exhaustive patterns: `&[..]` not covered
121+
error[E0004]: non-exhaustive patterns: `&[false]` not covered
122122
--> $DIR/slice-patterns-exhaustiveness.rs:99:11
123123
|
124124
LL | match s1 {
125-
| ^^ pattern `&[..]` not covered
125+
| ^^ pattern `&[false]` not covered
126126
|
127127
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
128128

0 commit comments

Comments
 (0)