Skip to content

Commit

Permalink
Use appropriate constructor for const slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Nov 21, 2019
1 parent fa4a4d3 commit 5510f55
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
16 changes: 14 additions & 2 deletions src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ impl<'tcx> Constructor<'tcx> {
.iter()
.filter_map(|c: &Constructor<'_>| match c {
Slice(slice) => Some(*slice),
// FIXME(#65413): We ignore `ConstantValue`s here.
// FIXME(oli-obk): implement `deref` for `ConstValue`
ConstantValue(..) => None,
_ => bug!("bad slice pattern constructor {:?}", c),
})
Expand Down Expand Up @@ -1771,7 +1771,19 @@ fn pat_constructor<'tcx>(
if let Some(int_range) = IntRange::from_const(tcx, param_env, value, pat.span) {
Some(IntRange(int_range))
} else {
Some(ConstantValue(value))
match (value.val, &value.ty.kind) {
(_, ty::Array(_, n)) => {
let len = n.eval_usize(tcx, param_env);
Some(Slice(Slice { array_len: Some(len), kind: FixedLen(len) }))
}
(ty::ConstKind::Value(ConstValue::Slice { start, end, .. }), ty::Slice(_)) => {
let len = (end - start) as u64;
Some(Slice(Slice { array_len: None, kind: FixedLen(len) }))
}
// FIXME(oli-obk): implement `deref` for `ConstValue`
// (ty::ConstKind::Value(ConstValue::ByRef { .. }), ty::Slice(_)) => { ... }
_ => Some(ConstantValue(value)),
}
}
}
PatKind::Range(PatRange { lo, hi, end }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// check-pass
#![feature(slice_patterns)]
#![deny(unreachable_patterns)]

Expand All @@ -8,8 +9,7 @@ fn main() {
match x {
&[] => {}
&[1..=255] => {}
// this shouldn't be unreachable
C0 => {} //~ unreachable pattern
C0 => {}
&[_, _, ..] => {}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error[E0004]: non-exhaustive patterns: `&[..]` not covered
error[E0004]: non-exhaustive patterns: `&[0u8..=64u8, _, _, _]` and `&[66u8..=std::u8::MAX, _, _, _]` not covered
--> $DIR/match-byte-array-patterns-2.rs:4:11
|
LL | match buf {
| ^^^ pattern `&[..]` not covered
| ^^^ patterns `&[0u8..=64u8, _, _, _]` and `&[66u8..=std::u8::MAX, _, _, _]` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn main() {
}
const CONST1: &[bool; 1] = &[true];
match s1 {
//~^ ERROR `&[..]` not covered
//~^ ERROR `&[false]` not covered
CONST1 => {}
}
match s1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ LL | match s {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

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

Expand Down

0 comments on commit 5510f55

Please sign in to comment.