Skip to content

Commit ca69d3b

Browse files
committed
Auto merge of rust-lang#120131 - oli-obk:pattern_types_syntax, r=compiler-errors
Implement minimal, internal-only pattern types in the type system rebase of rust-lang#107606 You can create pattern types with `std::pat::pattern_type!(ty is pat)`. The feature is incomplete and will panic on you if you use any pattern other than integral range patterns. The only way to create or deconstruct a pattern type is via `transmute`. This PR's implementation differs from the MCP's text. Specifically > This means you could implement different traits for different pattern types with the same base type. Thus, we just forbid implementing any traits for pattern types. is violated in this PR. The reason is that we do need impls after all in order to make them usable as fields. constants of type `std::time::Nanoseconds` struct are used in patterns, so the type must be structural-eq, which it only can be if you derive several traits on it. It doesn't need to be structural-eq recursively, so we can just manually implement the relevant traits on the pattern type and use the pattern type as a private field. Waiting on: * [x] move all unrelated commits into their own PRs. * [x] fix niche computation (see 2db07f9) * [x] add lots more tests * [x] T-types MCP rust-lang/types-team#126 to finish * [x] some commit cleanup * [x] full self-review * [x] remove 61bd325, it's not necessary anymore I think. * [ ] ~~make sure we never accidentally leak pattern types to user code (add stability checks or feature gate checks and appopriate tests)~~ we don't even do this for the new float primitives * [x] get approval that [the scope expansion to trait impls](https://rust-lang.zulipchat.com/#narrow/stream/326866-t-types.2Fnominated/topic/Pattern.20types.20types-team.23126/near/427670099) is ok r? `@BoxyUwU`
2 parents 322d51c + 77ffffa commit ca69d3b

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ pub mod net;
396396
pub mod option;
397397
pub mod panic;
398398
pub mod panicking;
399+
#[cfg(not(bootstrap))]
400+
#[unstable(feature = "core_pattern_types", issue = "none")]
401+
pub mod pat;
399402
pub mod pin;
400403
pub mod result;
401404
pub mod sync;

core/src/pat.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Helper module for exporting the `pattern_type` macro
2+
3+
/// Creates a pattern type.
4+
/// ```ignore (cannot test this from within core yet)
5+
/// type Positive = std::pat::pattern_type!(i32 is 1..);
6+
/// ```
7+
#[macro_export]
8+
#[rustc_builtin_macro(pattern_type)]
9+
#[unstable(feature = "core_pattern_type", issue = "none")]
10+
macro_rules! pattern_type {
11+
($($arg:tt)*) => {
12+
/* compiler built-in */
13+
};
14+
}

std/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ pub mod net;
576576
pub mod num;
577577
pub mod os;
578578
pub mod panic;
579+
#[cfg(not(bootstrap))]
580+
#[unstable(feature = "core_pattern_types", issue = "none")]
581+
pub mod pat;
579582
pub mod path;
580583
pub mod process;
581584
pub mod sync;

std/src/pat.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Helper module for exporting the `pattern_type` macro
2+
3+
pub use core::pattern_type;

0 commit comments

Comments
 (0)