-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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;` | ||
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)) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
} | ||
} | ||
|
@@ -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 {:?}", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
_ => {} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 16 additions & 4 deletions
20
src/test/ui/pattern/usefulness/slice-pattern-const-2.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.