Skip to content

Make our pattern matching logic undestand &[T] constants #70699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 62 additions & 32 deletions src/librustc_mir_build/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,30 +291,40 @@ impl<'tcx> LiteralExpander<'tcx> {
}
Scalar::Raw { .. } => {
let layout = self.tcx.layout_of(self.param_env.and(rty)).unwrap();
if layout.is_zst() {
// Deref of a reference to a ZST is a nop.
ConstValue::Scalar(Scalar::zst())
} else {
// FIXME(oli-obk): this is reachable for `const FOO: &&&u32 = &&&42;`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this comment incorrect before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apparently, we have a test for this, but the ICE is a different one somewhere in the middle of pattern matching. I've so far been unable to figure it out.

bug!("cannot deref {:#?}, {} -> {}", val, crty, rty);
}
assert!(layout.is_zst());
// Deref of a reference to a ZST is a nop.
ConstValue::Scalar(Scalar::zst())
}
}
}
// unsize array to slice if pattern is array but match value or other patterns are slice
(ConstValue::Scalar(Scalar::Ptr(p)), ty::Array(t, n), ty::Slice(u)) => {
// Unsize the array to a slice if we're matching on a slice,
// or other patterns are a slice (despite this pattern being
// an array).
(ConstValue::Scalar(s), ty::Array(t, n), ty::Slice(u)) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to avoid these one-letter variable names.

assert_eq!(t, u);
ConstValue::Slice {
data: self.tcx.alloc_map.lock().unwrap_memory(p.alloc_id),
start: p.offset.bytes().try_into().unwrap(),
end: n.eval_usize(self.tcx, ty::ParamEnv::empty()).try_into().unwrap(),
let n = n.eval_usize(self.tcx, ty::ParamEnv::empty()).try_into().unwrap();
match s {
Scalar::Ptr(p) => {
let start = p.offset.bytes().try_into().unwrap();
ConstValue::Slice {
data: self.tcx.alloc_map.lock().unwrap_memory(p.alloc_id),
start,
end: n,
}
}
Scalar::Raw { .. } => {
assert_eq!(n, 0);
// FIXME(oli-obk): this is reachable for
// `const FOO: &[u8] = transmute::<usize, &[u8; 0]>(1);`
bug!("cannot deref {:#?}, {} -> {}", val, crty, rty);
}
}
}
// fat pointers stay the same
// Wide pointers stay the same.
(ConstValue::Slice { .. }, _, _)
| (_, ty::Slice(_), ty::Slice(_))
| (_, ty::Str, ty::Str) => val,
// FIXME(oli-obk): this is reachable for `const FOO: &&&u32 = &&&42;` being used
// FIXME(oli-obk): this is reachable for `const FOO: &&&u32 = &&&42;`
_ => bug!("cannot deref {:#?}, {} -> {}", val, crty, rty),
}
}
Expand Down Expand Up @@ -2398,24 +2408,44 @@ fn specialize_one_pattern<'p, 'tcx>(
_ => span_bug!(pat.span, "array pattern is {:?}", value,),
}
}
ty::Slice(t) => {
match value.val {
ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => {
let offset = Size::from_bytes(start);
let n = (end - start) as u64;
(Cow::Borrowed(data), offset, n, t)
}
ty::ConstKind::Value(ConstValue::ByRef { .. }) => {
// FIXME(oli-obk): implement `deref` for `ConstValue`
return None;
}
_ => span_bug!(
pat.span,
"slice pattern constant must be scalar pair but is {:?}",
value,
),
ty::Slice(t) => match value.val {
ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => {
let offset = Size::from_bytes(start as u64);
let n = (end - start) as u64;
(Cow::Borrowed(data), offset, n, t)
}
}
ty::ConstKind::Value(ConstValue::ByRef { alloc, offset }) => {
// The following code is essentially a `deref` operation for slice wide
// pointers.
// First, read a pointer.
let offset = Pointer::new(AllocId(0), offset);
let ptr = alloc
.read_ptr_sized(&cx.tcx, offset)
.unwrap()
.not_undef()
.unwrap()
.assert_ptr();
// Then read a length.
let offset =
offset.offset(cx.tcx.data_layout.pointer_size, &cx.tcx).unwrap();
let len = alloc
.read_ptr_sized(&cx.tcx, offset)
.unwrap()
.not_undef()
.unwrap()
.assert_bits(cx.tcx.data_layout.pointer_size)
.try_into()
.unwrap();
// Now load the memory where the pointer points to.
let data = cx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
(Cow::Borrowed(data), ptr.offset, len, t)
}
_ => span_bug!(
pat.span,
"slice pattern constant must be scalar pair but is {:?}",
value,
),
},
_ => span_bug!(
pat.span,
"unexpected const-val {:?} with ctor {:?}",
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/pattern/const-pat-unreachable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(const_transmute)]
#![deny(unreachable_patterns)]

const FOO: &[u8] = unsafe { std::mem::transmute::<usize, &[u8; 0]>(1) };
const BAR: &[u8] = unsafe { std::mem::transmute::<usize, &[u8; 0]>(3) };

fn main() {
let x: &[u8] = unimplemented!();
match x {
b"" => {}
FOO => {} //~ ERROR unreachable pattern
BAR => {} //~ ERROR unreachable pattern
_ => {}
}
}
20 changes: 20 additions & 0 deletions src/test/ui/pattern/const-pat-unreachable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: unreachable pattern
--> $DIR/const-pat-unreachable.rs:11:9
|
LL | FOO => {}
| ^^^
|
note: the lint level is defined here
--> $DIR/const-pat-unreachable.rs:2:9
|
LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^

error: unreachable pattern
--> $DIR/const-pat-unreachable.rs:12:9
|
LL | BAR => {}
| ^^^

error: aborting due to 2 previous errors

4 changes: 2 additions & 2 deletions src/test/ui/pattern/usefulness/slice-pattern-const-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ fn main() {
match s {
MAGIC_TEST => (),
[0x00, 0x00, 0x00, 0x00] => (),
[4, 5, 6, 7] => (), // FIXME(oli-obk): this should warn, but currently does not
[4, 5, 6, 7] => (), //~ ERROR unreachable pattern
_ => (),
}
match s {
[0x00, 0x00, 0x00, 0x00] => (),
MAGIC_TEST => (),
[4, 5, 6, 7] => (), // FIXME(oli-obk): this should warn, but currently does not
[4, 5, 6, 7] => (), //~ ERROR unreachable pattern
_ => (),
}
match s {
Expand Down
20 changes: 16 additions & 4 deletions src/test/ui/pattern/usefulness/slice-pattern-const-2.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
error: unreachable pattern
--> $DIR/slice-pattern-const-2.rs:28:9
--> $DIR/slice-pattern-const-2.rs:9:9
|
LL | FOO => (),
| ^^^
LL | [4, 5, 6, 7] => (),
| ^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/slice-pattern-const-2.rs:1:9
|
LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
error: unreachable pattern
--> $DIR/slice-pattern-const-2.rs:15:9
|
LL | [4, 5, 6, 7] => (),
| ^^^^^^^^^^^^

error: unreachable pattern
--> $DIR/slice-pattern-const-2.rs:28:9
|
LL | FOO => (),
| ^^^

error: aborting due to 3 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ fn main() {
CONST => {}
}
match s {
//~^ ERROR `&[true]` not covered
[] => {},
[false] => {},
CONST => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,15 @@ LL | match s {
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `&[bool]`

error[E0004]: non-exhaustive patterns: `&[true]` not covered
--> $DIR/slice-patterns-exhaustiveness.rs:89:11
|
LL | match s {
| ^ pattern `&[true]` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `&[bool]`

error[E0004]: non-exhaustive patterns: `&[false]` not covered
--> $DIR/slice-patterns-exhaustiveness.rs:97:11
--> $DIR/slice-patterns-exhaustiveness.rs:96:11
|
LL | match s1 {
| ^^ pattern `&[false]` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `&[bool; 1]`

error: aborting due to 16 previous errors
error: aborting due to 15 previous errors

For more information about this error, try `rustc --explain E0004`.