-
Notifications
You must be signed in to change notification settings - Fork 13.3k
deref_patterns
: support string and byte string literals in explicit deref!("...")
patterns
#140028
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
Open
dianne
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
dianne:lit-deref-pats-p1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+349
−38
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
08baec4
add a test for byte string literal pattern mutability mismatches
dianne 6184025
make `str` literal patterns usable in deref patterns
dianne 3250344
make `[u8]` and `[u8;N]` literal patterns usable in deref patterns
dianne 4c7e866
update unstable book to mention string/bytestring typing
dianne 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
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
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 |
---|---|---|
|
@@ -280,6 +280,16 @@ impl<'tcx> ConstToPat<'tcx> { | |
slice: None, | ||
suffix: Box::new([]), | ||
}, | ||
ty::Str => { | ||
// String literal patterns may have type `str` if `deref_patterns` is enabled, in | ||
// order to allow `deref!("..."): String`. Since we need a `&str` for the comparison | ||
// when lowering to MIR in `Builder::perform_test`, treat the constant as a `&str`. | ||
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. I think it's important to note that the valtrees of 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. done: (diff) |
||
// This works because `str` and `&str` have the same valtree representation. | ||
let ref_str_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, ty); | ||
PatKind::Constant { | ||
value: mir::Const::Ty(ref_str_ty, ty::Const::new_value(tcx, cv, ref_str_ty)), | ||
} | ||
} | ||
ty::Ref(_, pointee_ty, ..) => match *pointee_ty.kind() { | ||
// `&str` is represented as a valtree, let's keep using this | ||
// optimization for now. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//! Byte string literal patterns use the mutability of the literal, rather than the mutability of | ||
//! the pattern's scrutinee. Since byte string literals are always shared references, it's a | ||
//! mismatch to use a byte string literal pattern to match on a mutable array or slice reference. | ||
fn main() { | ||
let mut val = [97u8, 10u8]; | ||
match &mut val { | ||
b"a\n" => {}, | ||
//~^ ERROR mismatched types | ||
//~| types differ in mutability | ||
_ => {}, | ||
} | ||
match &mut val[..] { | ||
b"a\n" => {}, | ||
//~^ ERROR mismatched types | ||
//~| types differ in mutability | ||
_ => {}, | ||
} | ||
} |
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,25 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/byte-string-mutability-mismatch.rs:8:9 | ||
| | ||
LL | match &mut val { | ||
| -------- this expression has type `&mut [u8; 2]` | ||
LL | b"a\n" => {}, | ||
| ^^^^^^ types differ in mutability | ||
| | ||
= note: expected mutable reference `&mut _` | ||
found reference `&'static _` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/byte-string-mutability-mismatch.rs:14:10 | ||
| | ||
LL | match &mut val[..] { | ||
| ------------ this expression has type `&mut [u8]` | ||
LL | b"a\n" => {}, | ||
| ^^^^^^ types differ in mutability | ||
| | ||
= note: expected mutable reference `&mut _` | ||
found reference `&'static _` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
34 changes: 34 additions & 0 deletions
34
tests/ui/pattern/deref-patterns/byte-string-type-errors.rs
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,34 @@ | ||
//! Test type errors for byte string literal patterns. `deref_patterns` allows byte string literal | ||
//! patterns to have type `[u8]` or `[u8; N]` when matching on a slice or array; this can affect the | ||
//! "found" type reported in error messages when matching on a slice or array of the wrong type. | ||
|
||
#![feature(deref_patterns)] | ||
#![expect(incomplete_features)] | ||
|
||
fn main() { | ||
// Baseline 1: under normal circumstances, byte string literal patterns have type `&[u8; N]`, | ||
// the same as byte string literals. | ||
if let b"test" = () {} | ||
//~^ ERROR mismatched types | ||
//~| expected `()`, found `&[u8; 4]` | ||
|
||
// Baseline 2: there's a special case for byte string patterns in stable rust, allowing them to | ||
// match on slice references. This affects the error when matching on a non-`&[u8]` slice ref, | ||
// reporting the "found" type as `&[u8]`. | ||
if let b"test" = &[] as &[i8] {} | ||
//~^ ERROR mismatched types | ||
//~| expected `&[i8]`, found `&[u8]` | ||
|
||
// Test matching on a non-`[u8]` slice: the pattern has type `[u8]` if a slice is expected. | ||
if let b"test" = *(&[] as &[i8]) {} | ||
//~^ ERROR mismatched types | ||
//~| expected `[i8]`, found `[u8]` | ||
|
||
// Test matching on a non-`[u8;4]` array: the pattern has type `[u8;4]` if an array is expected. | ||
if let b"test" = [()] {} | ||
//~^ ERROR mismatched types | ||
//~| expected `[(); 1]`, found `[u8; 4]` | ||
if let b"test" = *b"this array is too long" {} | ||
//~^ ERROR mismatched types | ||
//~| expected an array with a size of 22, found one with a size of 4 | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/ui/pattern/deref-patterns/byte-string-type-errors.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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/byte-string-type-errors.rs:11:12 | ||
| | ||
LL | if let b"test" = () {} | ||
| ^^^^^^^ -- this expression has type `()` | ||
| | | ||
| expected `()`, found `&[u8; 4]` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/byte-string-type-errors.rs:18:12 | ||
| | ||
LL | if let b"test" = &[] as &[i8] {} | ||
| ^^^^^^^ ------------ this expression has type `&[i8]` | ||
| | | ||
| expected `&[i8]`, found `&[u8]` | ||
| | ||
= note: expected reference `&[i8]` | ||
found reference `&'static [u8]` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/byte-string-type-errors.rs:23:12 | ||
| | ||
LL | if let b"test" = *(&[] as &[i8]) {} | ||
| ^^^^^^^ --------------- this expression has type `[i8]` | ||
| | | ||
| expected `[i8]`, found `[u8]` | ||
| | ||
= note: expected slice `[i8]` | ||
found slice `[u8]` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/byte-string-type-errors.rs:28:12 | ||
| | ||
LL | if let b"test" = [()] {} | ||
| ^^^^^^^ ---- this expression has type `[(); 1]` | ||
| | | ||
| expected `[(); 1]`, found `[u8; 4]` | ||
| | ||
= note: expected array `[(); 1]` | ||
found array `[u8; 4]` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/byte-string-type-errors.rs:31:12 | ||
| | ||
LL | if let b"test" = *b"this array is too long" {} | ||
| ^^^^^^^ -------------------------- this expression has type `[u8; 22]` | ||
| | | ||
| expected an array with a size of 22, found one with a size of 4 | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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
Oops, something went wrong.
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.
similarly, shouldn't we forward the mutability? Please add tests for this
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.
I've added a test: (diff) (playground). Forwarding the mutability of the scrutinee would make sense, but since the current stable behavior is to use the literal's mutability, that would also need to be gated. And if we do forward ref mutability, we'd probably want to do it for array references too. Interestingly, it doesn't look like there were any other tests preventing this from changing; nice catch! I've made it the first commit since it was existing behavior and is unchanged by this PR.