-
Notifications
You must be signed in to change notification settings - Fork 13.3k
literal pattern lowering: use the pattern's type instead of the literal's in const_to_pat
#138992
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,53 @@ | ||
//! Tests that arrays and slices in constants aren't interchangeable when used as patterns. | ||
|
||
#[derive(PartialEq, Eq)] | ||
struct SomeStruct<T: ?Sized>(T); | ||
|
||
const BSTR_SIZED: &'static [u8; 3] = b"012"; | ||
const BSTR_UNSIZED: &'static [u8] = BSTR_SIZED; | ||
const STRUCT_SIZED: &'static SomeStruct<[u8; 3]> = &SomeStruct(*BSTR_SIZED); | ||
const STRUCT_UNSIZED: &'static SomeStruct<[u8]> = STRUCT_SIZED; | ||
|
||
fn type_mismatches() { | ||
// Test that array consts can't be used where a slice pattern is expected. This helps ensure | ||
// that `const_to_pat` won't produce irrefutable `thir::PatKind::Array` patterns when matching | ||
// on slices, which would result in missing length checks. | ||
// See also `tests/ui/match/pattern-deref-miscompile.rs`, which tests that byte string literal | ||
// patterns check slices' length appropriately when matching on slices. | ||
match BSTR_UNSIZED { | ||
BSTR_SIZED => {} | ||
//~^ ERROR: mismatched types | ||
_ => {} | ||
} | ||
match STRUCT_UNSIZED { | ||
STRUCT_SIZED => {} | ||
//~^ ERROR: mismatched types | ||
_ => {} | ||
} | ||
|
||
// Test that slice consts can't be used where an array pattern is expected. | ||
match BSTR_UNSIZED { | ||
BSTR_SIZED => {} | ||
//~^ ERROR: mismatched types | ||
_ => {} | ||
} | ||
// If the types matched here, this would still error, since unsized structs aren't permitted in | ||
// constant patterns. See the `invalid_patterns` test below. | ||
match STRUCT_UNSIZED { | ||
STRUCT_SIZED => {} | ||
//~^ ERROR: mismatched types | ||
_ => {} | ||
} | ||
} | ||
|
||
fn invalid_patterns() { | ||
// Test that unsized structs containing slices can't be used as patterns. | ||
// See `tests/ui/consts/issue-87046.rs` for an example with `str`. | ||
match STRUCT_UNSIZED { | ||
STRUCT_UNSIZED => {} | ||
//~^ ERROR: cannot use unsized non-slice type `SomeStruct<[u8]>` in constant patterns | ||
_ => {} | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains 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,84 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/arrays-and-slices.rs:18:9 | ||
| | ||
LL | const BSTR_SIZED: &'static [u8; 3] = b"012"; | ||
| ---------------------------------- constant defined here | ||
... | ||
LL | match BSTR_UNSIZED { | ||
| ------------ this expression has type `&[u8]` | ||
LL | BSTR_SIZED => {} | ||
| ^^^^^^^^^^ | ||
| | | ||
| expected `&[u8]`, found `&[u8; 3]` | ||
| `BSTR_SIZED` is interpreted as a constant, not a new binding | ||
| help: introduce a new binding instead: `other_bstr_sized` | ||
| | ||
= note: expected reference `&[u8]` | ||
found reference `&'static [u8; 3]` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/arrays-and-slices.rs:23:9 | ||
| | ||
LL | const STRUCT_SIZED: &'static SomeStruct<[u8; 3]> = &SomeStruct(*BSTR_SIZED); | ||
| ------------------------------------------------ constant defined here | ||
... | ||
LL | match STRUCT_UNSIZED { | ||
| -------------- this expression has type `&SomeStruct<[u8]>` | ||
LL | STRUCT_SIZED => {} | ||
| ^^^^^^^^^^^^ | ||
| | | ||
| expected `&SomeStruct<[u8]>`, found `&SomeStruct<[u8; 3]>` | ||
| `STRUCT_SIZED` is interpreted as a constant, not a new binding | ||
| help: introduce a new binding instead: `other_struct_sized` | ||
| | ||
= note: expected reference `&SomeStruct<[u8]>` | ||
found reference `&'static SomeStruct<[u8; 3]>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/arrays-and-slices.rs:30:9 | ||
| | ||
LL | const BSTR_SIZED: &'static [u8; 3] = b"012"; | ||
| ---------------------------------- constant defined here | ||
... | ||
LL | match BSTR_UNSIZED { | ||
| ------------ this expression has type `&[u8]` | ||
LL | BSTR_SIZED => {} | ||
| ^^^^^^^^^^ | ||
| | | ||
| expected `&[u8]`, found `&[u8; 3]` | ||
| `BSTR_SIZED` is interpreted as a constant, not a new binding | ||
| help: introduce a new binding instead: `other_bstr_sized` | ||
| | ||
= note: expected reference `&[u8]` | ||
found reference `&'static [u8; 3]` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/arrays-and-slices.rs:37:9 | ||
| | ||
LL | const STRUCT_SIZED: &'static SomeStruct<[u8; 3]> = &SomeStruct(*BSTR_SIZED); | ||
| ------------------------------------------------ constant defined here | ||
... | ||
LL | match STRUCT_UNSIZED { | ||
| -------------- this expression has type `&SomeStruct<[u8]>` | ||
LL | STRUCT_SIZED => {} | ||
| ^^^^^^^^^^^^ | ||
| | | ||
| expected `&SomeStruct<[u8]>`, found `&SomeStruct<[u8; 3]>` | ||
| `STRUCT_SIZED` is interpreted as a constant, not a new binding | ||
| help: introduce a new binding instead: `other_struct_sized` | ||
| | ||
= note: expected reference `&SomeStruct<[u8]>` | ||
found reference `&'static SomeStruct<[u8; 3]>` | ||
|
||
error: cannot use unsized non-slice type `SomeStruct<[u8]>` in constant patterns | ||
--> $DIR/arrays-and-slices.rs:47:9 | ||
| | ||
LL | const STRUCT_UNSIZED: &'static SomeStruct<[u8]> = STRUCT_SIZED; | ||
| ----------------------------------------------- constant defined here | ||
... | ||
LL | STRUCT_UNSIZED => {} | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
Oops, something went wrong.
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.
what's the difference in the repr between this and mentioning the pattern inline that causes this error?
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.
That would be these lines in the literal pattern checking rules. Byte string literal patterns are assigned a slice reference type if the current scrutinee is known to be a slice reference, and otherwise default to the type of the literal (an array reference). Since that link's from the
master
branch, that's also wheretreat_byte_string_as_slice
was assigned. There's no array unsizing logic for named constant patterns (type-checking code here), so it fails to unify&[u8]
with&[u8;3]
. If, hypothetically, that was special-cased, and the pattern was assigned the type&[u8]
rather than the constant's type, then that example would work too (though without the change this PR makes, it'd also have to settreat_byte_string_as_slice
or it'd be missing length checks).