Skip to content

Commit 846b80e

Browse files
committed
Add pattern types to ast
1 parent a9985cf commit 846b80e

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
@@ -2102,6 +2102,9 @@ pub enum TyKind {
21022102
Err,
21032103
/// Placeholder for a `va_list`.
21042104
CVarArgs,
2105+
/// Pattern types like `u32 as 1..=`, which is the same as `NonZeroU32`,
2106+
/// just as part of the type system.
2107+
Pat(P<Ty>, P<Pat>),
21052108
}
21062109

21072110
impl TyKind {

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
476476
}
477477
TyKind::Tup(tys) => visit_vec(tys, |ty| vis.visit_ty(ty)),
478478
TyKind::Paren(ty) => vis.visit_ty(ty),
479+
TyKind::Pat(ty, pat) => {
480+
vis.visit_ty(ty);
481+
vis.visit_pat(pat);
482+
}
479483
TyKind::Path(qself, path) => {
480484
vis.visit_qself(qself);
481485
vis.visit_path(path);

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
416416
}
417417
visitor.visit_path(path, typ.id);
418418
}
419+
TyKind::Pat(ty, pat) => {
420+
visitor.visit_ty(ty);
421+
visitor.visit_pat(pat);
422+
}
419423
TyKind::Array(ty, length) => {
420424
visitor.visit_ty(ty);
421425
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
@@ -1372,14 +1372,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13721372
}
13731373
}
13741374
}
1375-
TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"),
1375+
TyKind::MacCall(_) => {
1376+
span_bug!(t.span, "`TyKind::MacCall` should have been expanded by now")
1377+
}
13761378
TyKind::CVarArgs => {
13771379
self.tcx.sess.delay_span_bug(
13781380
t.span,
13791381
"`TyKind::CVarArgs` should have been handled elsewhere",
13801382
);
13811383
hir::TyKind::Err
13821384
}
1385+
TyKind::Pat(..) => span_bug!(t.span, "pattern types are unimplemented"),
13831386
};
13841387

13851388
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
@@ -1092,6 +1092,11 @@ impl<'a> State<'a> {
10921092
ast::TyKind::CVarArgs => {
10931093
self.word("...");
10941094
}
1095+
ast::TyKind::Pat(ty, pat) => {
1096+
self.print_type(ty);
1097+
self.word(" is ");
1098+
self.print_pat(pat);
1099+
}
10951100
}
10961101
self.end();
10971102
}

compiler/rustc_passes/src/hir_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
586586
Never,
587587
Tup,
588588
Path,
589+
Pat,
589590
TraitObject,
590591
ImplTrait,
591592
Paren,

src/tools/rustfmt/src/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,11 @@ impl Rewrite for ast::Ty {
847847
self.span,
848848
shape,
849849
),
850+
ast::TyKind::Pat(ref ty, ref pat) => {
851+
let ty = ty.rewrite(context, shape)?;
852+
let pat = pat.rewrite(context, shape)?;
853+
Some(format!("{ty} is {pat}"))
854+
},
850855
}
851856
}
852857
}

0 commit comments

Comments
 (0)