-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Allow const patterns of matches to contain pattern types #138393
Open
oli-obk
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
oli-obk:pattern-type-in-pattern
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.
Open
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 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 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 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 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 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,26 @@ | ||
//! Check that pattern types don't implement traits of their base automatically. | ||
//! Exceptions are `Clone` and `Copy`. | ||
|
||
#![feature(pattern_types)] | ||
#![feature(pattern_type_macro)] | ||
|
||
use std::pat::pattern_type; | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Default)] | ||
#[repr(transparent)] | ||
struct Nanoseconds(NanoI32); | ||
//~^ ERROR: the trait bound `(i32) is 0..=999999999: Eq` is not satisfied | ||
//~| ERROR: `(i32) is 0..=999999999` doesn't implement `Debug` | ||
//~| ERROR: the trait bound `(i32) is 0..=999999999: Ord` is not satisfied | ||
//~| ERROR: the trait bound `(i32) is 0..=999999999: Hash` is not satisfied | ||
//~| ERROR: the trait bound `(i32) is 0..=999999999: Default` is not satisfied | ||
//~| ERROR: can't compare `(i32) is 0..=999999999` with `_` | ||
//~| ERROR: `==` cannot be applied | ||
|
||
type NanoI32 = crate::pattern_type!(i32 is 0..=999_999_999); | ||
|
||
fn main() { | ||
let x = Nanoseconds(unsafe { std::mem::transmute(42) }); | ||
let y = x.clone(); | ||
if y == x {} | ||
} |
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,74 @@ | ||
error[E0369]: binary operation `==` cannot be applied to type `(i32) is 0..=999999999` | ||
--> $DIR/derives_fail.rs:11:20 | ||
| | ||
LL | #[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Default)] | ||
| --------- in this derive macro expansion | ||
LL | #[repr(transparent)] | ||
LL | struct Nanoseconds(NanoI32); | ||
| ^^^^^^^ | ||
|
||
error[E0277]: the trait bound `(i32) is 0..=999999999: Eq` is not satisfied | ||
--> $DIR/derives_fail.rs:11:20 | ||
| | ||
LL | #[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Default)] | ||
| -- in this derive macro expansion | ||
LL | #[repr(transparent)] | ||
LL | struct Nanoseconds(NanoI32); | ||
| ^^^^^^^ the trait `Eq` is not implemented for `(i32) is 0..=999999999` | ||
| | ||
note: required by a bound in `AssertParamIsEq` | ||
--> $SRC_DIR/core/src/cmp.rs:LL:COL | ||
|
||
error[E0277]: `(i32) is 0..=999999999` doesn't implement `Debug` | ||
--> $DIR/derives_fail.rs:11:20 | ||
| | ||
LL | #[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Default)] | ||
| ----- in this derive macro expansion | ||
LL | #[repr(transparent)] | ||
LL | struct Nanoseconds(NanoI32); | ||
| ^^^^^^^ `(i32) is 0..=999999999` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ||
| | ||
= help: the trait `Debug` is not implemented for `(i32) is 0..=999999999` | ||
|
||
error[E0277]: the trait bound `(i32) is 0..=999999999: Ord` is not satisfied | ||
--> $DIR/derives_fail.rs:11:20 | ||
| | ||
LL | #[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Default)] | ||
| --- in this derive macro expansion | ||
LL | #[repr(transparent)] | ||
LL | struct Nanoseconds(NanoI32); | ||
| ^^^^^^^ the trait `Ord` is not implemented for `(i32) is 0..=999999999` | ||
|
||
error[E0277]: can't compare `(i32) is 0..=999999999` with `_` | ||
--> $DIR/derives_fail.rs:11:20 | ||
| | ||
LL | #[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Default)] | ||
| ---------- in this derive macro expansion | ||
LL | #[repr(transparent)] | ||
LL | struct Nanoseconds(NanoI32); | ||
| ^^^^^^^ no implementation for `(i32) is 0..=999999999 < _` and `(i32) is 0..=999999999 > _` | ||
| | ||
= help: the trait `PartialOrd<_>` is not implemented for `(i32) is 0..=999999999` | ||
|
||
error[E0277]: the trait bound `(i32) is 0..=999999999: Hash` is not satisfied | ||
--> $DIR/derives_fail.rs:11:20 | ||
| | ||
LL | #[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Default)] | ||
| ---- in this derive macro expansion | ||
LL | #[repr(transparent)] | ||
LL | struct Nanoseconds(NanoI32); | ||
| ^^^^^^^ the trait `Hash` is not implemented for `(i32) is 0..=999999999` | ||
|
||
error[E0277]: the trait bound `(i32) is 0..=999999999: Default` is not satisfied | ||
--> $DIR/derives_fail.rs:11:20 | ||
| | ||
LL | #[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash, Default)] | ||
| ------- in this derive macro expansion | ||
LL | #[repr(transparent)] | ||
LL | struct Nanoseconds(NanoI32); | ||
| ^^^^^^^ the trait `Default` is not implemented for `(i32) is 0..=999999999` | ||
|
||
error: aborting due to 7 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0369. | ||
For more information about an error, try `rustc --explain E0277`. |
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,26 @@ | ||
#![feature(pattern_types, pattern_type_macro, structural_match)] | ||
|
||
//@ check-pass | ||
|
||
use std::marker::StructuralPartialEq; | ||
use std::pat::pattern_type; | ||
|
||
struct Thing(pattern_type!(u32 is 1..)); | ||
|
||
impl StructuralPartialEq for Thing {} | ||
impl PartialEq for Thing { | ||
fn eq(&self, other: &Thing) -> bool { | ||
unsafe { std::mem::transmute::<_, u32>(self.0) == std::mem::transmute::<_, u32>(other.0) } | ||
} | ||
} | ||
|
||
impl Eq for Thing {} | ||
|
||
const TWO: Thing = Thing(2); | ||
|
||
const _: () = match TWO { | ||
TWO => {} | ||
_ => unreachable!(), | ||
}; | ||
|
||
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,25 @@ | ||
#![feature(pattern_types, pattern_type_macro, structural_match)] | ||
|
||
use std::pat::pattern_type; | ||
|
||
const THREE: pattern_type!(u32 is 1..) = 3; | ||
|
||
const _: () = match THREE { | ||
THREE => {} | ||
//~^ ERROR non-structural type | ||
_ => unreachable!(), | ||
}; | ||
|
||
const _: () = match THREE { | ||
3 => {} | ||
//~^ ERROR mismatched types | ||
_ => unreachable!(), | ||
}; | ||
|
||
const _: () = match 3 { | ||
THREE => {} | ||
//~^ ERROR mismatched types | ||
_ => unreachable!(), | ||
}; | ||
|
||
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,43 @@ | ||
error: constant of non-structural type `(u32) is 1..` in a pattern | ||
--> $DIR/matching_fail.rs:8:5 | ||
| | ||
LL | const THREE: pattern_type!(u32 is 1..) = 3; | ||
| -------------------------------------- constant defined here | ||
... | ||
LL | THREE => {} | ||
| ^^^^^ constant of non-structural type | ||
| | ||
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/matching_fail.rs:14:5 | ||
| | ||
LL | const _: () = match THREE { | ||
| ----- this expression has type `(u32) is 1..` | ||
LL | 3 => {} | ||
| ^ expected `(u32) is 1..`, found integer | ||
| | ||
= note: expected pattern type `(u32) is 1..` | ||
found type `{integer}` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/matching_fail.rs:20:5 | ||
| | ||
LL | const THREE: pattern_type!(u32 is 1..) = 3; | ||
| -------------------------------------- constant defined here | ||
... | ||
LL | const _: () = match 3 { | ||
| - this expression has type `{integer}` | ||
LL | THREE => {} | ||
| ^^^^^ | ||
| | | ||
| expected integer, found `(u32) is 1..` | ||
| `THREE` is interpreted as a constant, not a new binding | ||
| help: introduce a new binding instead: `other_three` | ||
| | ||
= note: expected type `{integer}` | ||
found pattern type `(u32) is 1..` | ||
|
||
error: aborting due to 3 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.
Can you add a comment explaining that we only support pattern types on the basic scalar types rn which all implement
Copy
so its fine to just unconditionally do aOperand::Copy
here instead of the usual "is it copy mod regions" thing.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 added an assert for
is_trivially_pure_clone_copy()