Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4f2c97

Browse files
committedApr 8, 2024
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 0e5f520 + 90e88aa commit a4f2c97

File tree

126 files changed

+1498
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+1498
-66
lines changed
 

‎compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,9 @@ pub enum TyKind {
21522152
MacCall(P<MacCall>),
21532153
/// Placeholder for a `va_list`.
21542154
CVarArgs,
2155+
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZeroU32`,
2156+
/// just as part of the type system.
2157+
Pat(P<Ty>, P<Pat>),
21552158
/// Sometimes we need a dummy value when no error has occurred.
21562159
Dummy,
21572160
/// Placeholder for a kind that has failed to be defined.

‎compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
502502
}
503503
TyKind::Tup(tys) => visit_thin_vec(tys, |ty| vis.visit_ty(ty)),
504504
TyKind::Paren(ty) => vis.visit_ty(ty),
505+
TyKind::Pat(ty, pat) => {
506+
vis.visit_ty(ty);
507+
vis.visit_pat(pat);
508+
}
505509
TyKind::Path(qself, path) => {
506510
vis.visit_qself(qself);
507511
vis.visit_path(path);

0 commit comments

Comments
 (0)