Add pattern types to ast

This commit is contained in:
Oli Scherer 2023-01-30 09:50:16 +00:00
parent 0e5f520788
commit fc27a91880
7 changed files with 26 additions and 1 deletions

View File

@ -2152,6 +2152,9 @@ pub enum TyKind {
MacCall(P<MacCall>), MacCall(P<MacCall>),
/// Placeholder for a `va_list`. /// Placeholder for a `va_list`.
CVarArgs, CVarArgs,
/// Pattern types like `u32 as 1..=`, which is the same as `NonZeroU32`,
/// just as part of the type system.
Pat(P<Ty>, P<Pat>),
/// Sometimes we need a dummy value when no error has occurred. /// Sometimes we need a dummy value when no error has occurred.
Dummy, Dummy,
/// Placeholder for a kind that has failed to be defined. /// Placeholder for a kind that has failed to be defined.

View File

@ -502,6 +502,10 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
} }
TyKind::Tup(tys) => visit_thin_vec(tys, |ty| vis.visit_ty(ty)), TyKind::Tup(tys) => visit_thin_vec(tys, |ty| vis.visit_ty(ty)),
TyKind::Paren(ty) => vis.visit_ty(ty), TyKind::Paren(ty) => vis.visit_ty(ty),
TyKind::Pat(ty, pat) => {
vis.visit_ty(ty);
vis.visit_pat(pat);
}
TyKind::Path(qself, path) => { TyKind::Path(qself, path) => {
vis.visit_qself(qself); vis.visit_qself(qself);
vis.visit_path(path); vis.visit_path(path);

View File

@ -446,6 +446,10 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) -> V::Result {
} }
try_visit!(visitor.visit_path(path, typ.id)); try_visit!(visitor.visit_path(path, typ.id));
} }
TyKind::Pat(ty, pat) => {
try_visit!(visitor.visit_ty(ty));
try_visit!(visitor.visit_pat(pat));
}
TyKind::Array(ty, length) => { TyKind::Array(ty, length) => {
try_visit!(visitor.visit_ty(ty)); try_visit!(visitor.visit_ty(ty));
try_visit!(visitor.visit_anon_const(length)); try_visit!(visitor.visit_anon_const(length));

View File

@ -1463,7 +1463,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} }
} }
} }
TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"), TyKind::MacCall(_) => {
span_bug!(t.span, "`TyKind::MacCall` should have been expanded by now")
}
TyKind::CVarArgs => { TyKind::CVarArgs => {
let guar = self.dcx().span_delayed_bug( let guar = self.dcx().span_delayed_bug(
t.span, t.span,
@ -1471,6 +1473,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
); );
hir::TyKind::Err(guar) hir::TyKind::Err(guar)
} }
TyKind::Pat(..) => span_bug!(t.span, "pattern types are unimplemented"),
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"), TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
}; };

View File

@ -1188,6 +1188,11 @@ impl<'a> State<'a> {
ast::TyKind::CVarArgs => { ast::TyKind::CVarArgs => {
self.word("..."); self.word("...");
} }
ast::TyKind::Pat(ty, pat) => {
self.print_type(ty);
self.word(" is ");
self.print_pat(pat);
}
} }
self.end(); self.end();
} }

View File

@ -611,6 +611,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
AnonStruct, AnonStruct,
AnonUnion, AnonUnion,
Path, Path,
Pat,
TraitObject, TraitObject,
ImplTrait, ImplTrait,
Paren, Paren,

View File

@ -867,6 +867,11 @@ impl Rewrite for ast::Ty {
self.span, self.span,
shape, shape,
), ),
ast::TyKind::Pat(ref ty, ref pat) => {
let ty = ty.rewrite(context, shape)?;
let pat = pat.rewrite(context, shape)?;
Some(format!("{ty} is {pat}"))
}
} }
} }
} }