Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 537aab7

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 ab3dba9 + 18ff131 commit 537aab7

File tree

126 files changed

+1499
-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

+1499
-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);

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) -> V::Result {
446446
}
447447
try_visit!(visitor.visit_path(path, typ.id));
448448
}
449+
TyKind::Pat(ty, pat) => {
450+
try_visit!(visitor.visit_ty(ty));
451+
try_visit!(visitor.visit_pat(pat));
452+
}
449453
TyKind::Array(ty, length) => {
450454
try_visit!(visitor.visit_ty(ty));
451455
try_visit!(visitor.visit_anon_const(length));

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
381381
ArrayLen::Body(..) => intravisit::walk_array_len(self, len),
382382
}
383383
}
384+
385+
fn visit_pattern_type_pattern(&mut self, p: &'hir hir::Pat<'hir>) {
386+
self.visit_pat(p)
387+
}
384388
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14631463
}
14641464
}
14651465
}
1466-
TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"),
1466+
TyKind::Pat(ty, pat) => hir::TyKind::Pat(self.lower_ty(ty, itctx), self.lower_pat(pat)),
1467+
TyKind::MacCall(_) => {
1468+
span_bug!(t.span, "`TyKind::MacCall` should have been expanded by now")
1469+
}
14671470
TyKind::CVarArgs => {
14681471
let guar = self.dcx().span_delayed_bug(
14691472
t.span,

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
332332
ast::TyKind::Never => {
333333
gate!(&self, never_type, ty.span, "the `!` type is experimental");
334334
}
335+
ast::TyKind::Pat(..) => {
336+
gate!(&self, pattern_types, ty.span, "pattern types are unstable");
337+
}
335338
_ => {}
336339
}
337340
visit::walk_ty(self, ty)

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,11 @@ impl<'a> State<'a> {
11881188
ast::TyKind::CVarArgs => {
11891189
self.word("...");
11901190
}
1191+
ast::TyKind::Pat(ty, pat) => {
1192+
self.print_type(ty);
1193+
self.word(" is ");
1194+
self.print_pat(pat);
1195+
}
11911196
}
11921197
self.end();
11931198
}

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16061606
| ty::Foreign(_)
16071607
| ty::Str
16081608
| ty::Array(_, _)
1609+
| ty::Pat(_, _)
16091610
| ty::Slice(_)
16101611
| ty::FnDef(_, _)
16111612
| ty::FnPtr(_)
@@ -1648,6 +1649,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16481649
| ty::Foreign(_)
16491650
| ty::Str
16501651
| ty::Array(_, _)
1652+
| ty::Pat(_, _)
16511653
| ty::Slice(_)
16521654
| ty::RawPtr(_, _)
16531655
| ty::Ref(_, _, _)

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ mod format;
4646
mod format_foreign;
4747
mod global_allocator;
4848
mod log_syntax;
49+
mod pattern_type;
4950
mod source_util;
5051
mod test;
5152
mod trace_macros;
@@ -95,6 +96,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9596
log_syntax: log_syntax::expand_log_syntax,
9697
module_path: source_util::expand_mod,
9798
option_env: env::expand_option_env,
99+
pattern_type: pattern_type::expand,
98100
std_panic: edition_panic::expand_panic,
99101
stringify: source_util::expand_stringify,
100102
trace_macros: trace_macros::expand_trace_macros,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustc_ast::{ast, ptr::P, tokenstream::TokenStream, Pat, Ty};
2+
use rustc_errors::PResult;
3+
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
4+
use rustc_span::{sym, Span};
5+
6+
pub fn expand<'cx>(
7+
cx: &'cx mut ExtCtxt<'_>,
8+
sp: Span,
9+
tts: TokenStream,
10+
) -> MacroExpanderResult<'cx> {
11+
let (ty, pat) = match parse_pat_ty(cx, tts) {
12+
Ok(parsed) => parsed,
13+
Err(err) => {
14+
return ExpandResult::Ready(DummyResult::any(sp, err.emit()));
15+
}
16+
};
17+
18+
ExpandResult::Ready(base::MacEager::ty(cx.ty(sp, ast::TyKind::Pat(ty, pat))))
19+
}
20+
21+
fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P<Ty>, P<Pat>)> {
22+
let mut parser = cx.new_parser_from_tts(stream);
23+
24+
let ty = parser.parse_ty()?;
25+
parser.eat_keyword(sym::is);
26+
let pat = parser.parse_pat_no_top_alt(None, None)?;
27+
28+
Ok((ty, pat))
29+
}

0 commit comments

Comments
 (0)