Skip to content

Commit fc27a91

Browse files
committed
Add pattern types to ast
1 parent 0e5f520 commit fc27a91

File tree

7 files changed

+26
-1
lines changed

7 files changed

+26
-1
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 `u32 as 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/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,14 +1463,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14631463
}
14641464
}
14651465
}
1466-
TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"),
1466+
TyKind::MacCall(_) => {
1467+
span_bug!(t.span, "`TyKind::MacCall` should have been expanded by now")
1468+
}
14671469
TyKind::CVarArgs => {
14681470
let guar = self.dcx().span_delayed_bug(
14691471
t.span,
14701472
"`TyKind::CVarArgs` should have been handled elsewhere",
14711473
);
14721474
hir::TyKind::Err(guar)
14731475
}
1476+
TyKind::Pat(..) => span_bug!(t.span, "pattern types are unimplemented"),
14741477
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
14751478
};
14761479

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_passes/src/hir_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
611611
AnonStruct,
612612
AnonUnion,
613613
Path,
614+
Pat,
614615
TraitObject,
615616
ImplTrait,
616617
Paren,

src/tools/rustfmt/src/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,11 @@ impl Rewrite for ast::Ty {
867867
self.span,
868868
shape,
869869
),
870+
ast::TyKind::Pat(ref ty, ref pat) => {
871+
let ty = ty.rewrite(context, shape)?;
872+
let pat = pat.rewrite(context, shape)?;
873+
Some(format!("{ty} is {pat}"))
874+
}
870875
}
871876
}
872877
}

0 commit comments

Comments
 (0)