Skip to content

Commit 3f4b4cb

Browse files
committed
Add pattern types to ast
1 parent 366d112 commit 3f4b4cb

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
@@ -2135,6 +2135,9 @@ pub enum TyKind {
21352135
Err,
21362136
/// Placeholder for a `va_list`.
21372137
CVarArgs,
2138+
/// Pattern types like `u32 as 1..=`, which is the same as `NonZeroU32`,
2139+
/// just as part of the type system.
2140+
Pat(P<Ty>, P<Pat>),
21382141
}
21392142

21402143
impl TyKind {

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
497497
}
498498
TyKind::Tup(tys) => visit_thin_vec(tys, |ty| vis.visit_ty(ty)),
499499
TyKind::Paren(ty) => vis.visit_ty(ty),
500+
TyKind::Pat(ty, pat) => {
501+
vis.visit_ty(ty);
502+
vis.visit_pat(pat);
503+
}
500504
TyKind::Path(qself, path) => {
501505
vis.visit_qself(qself);
502506
vis.visit_path(path);

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
436436
}
437437
visitor.visit_path(path, typ.id);
438438
}
439+
TyKind::Pat(ty, pat) => {
440+
visitor.visit_ty(ty);
441+
visitor.visit_pat(pat);
442+
}
439443
TyKind::Array(ty, length) => {
440444
visitor.visit_ty(ty);
441445
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
@@ -1521,14 +1521,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15211521
}
15221522
}
15231523
}
1524-
TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"),
1524+
TyKind::MacCall(_) => {
1525+
span_bug!(t.span, "`TyKind::MacCall` should have been expanded by now")
1526+
}
15251527
TyKind::CVarArgs => {
15261528
let guar = self.dcx().span_delayed_bug(
15271529
t.span,
15281530
"`TyKind::CVarArgs` should have been handled elsewhere",
15291531
);
15301532
hir::TyKind::Err(guar)
15311533
}
1534+
TyKind::Pat(..) => span_bug!(t.span, "pattern types are unimplemented"),
15321535
};
15331536

15341537
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,11 @@ impl<'a> State<'a> {
10461046
ast::TyKind::CVarArgs => {
10471047
self.word("...");
10481048
}
1049+
ast::TyKind::Pat(ty, pat) => {
1050+
self.print_type(ty);
1051+
self.word(" is ");
1052+
self.print_pat(pat);
1053+
}
10491054
}
10501055
self.end();
10511056
}

compiler/rustc_passes/src/hir_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
613613
AnonStruct,
614614
AnonUnion,
615615
Path,
616+
Pat,
616617
TraitObject,
617618
ImplTrait,
618619
Paren,

src/tools/rustfmt/src/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,11 @@ impl Rewrite for ast::Ty {
856856
self.span,
857857
shape,
858858
),
859+
ast::TyKind::Pat(ref ty, ref pat) => {
860+
let ty = ty.rewrite(context, shape)?;
861+
let pat = pat.rewrite(context, shape)?;
862+
Some(format!("{ty} is {pat}"))
863+
}
859864
}
860865
}
861866
}

0 commit comments

Comments
 (0)