1
Fork 0

Add #[derive(TypeVisitable)]

This commit is contained in:
Alan Egerton 2022-06-17 10:53:29 +01:00
parent bca894909c
commit e4b9625b87
No known key found for this signature in database
GPG key ID: 07CAC3CCA7E0643F
31 changed files with 183 additions and 221 deletions

View file

@ -18,6 +18,7 @@ mod query;
mod serialize;
mod symbols;
mod type_foldable;
mod type_visitable;
#[proc_macro]
pub fn rustc_queries(input: TokenStream) -> TokenStream {
@ -121,6 +122,7 @@ decl_derive!([TyEncodable] => serialize::type_encodable_derive);
decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
decl_derive!([TypeFoldable, attributes(type_foldable)] => type_foldable::type_foldable_derive);
decl_derive!([TypeVisitable, attributes(type_visitable)] => type_visitable::type_visitable_derive);
decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
decl_derive!(
[SessionDiagnostic, attributes(

View file

@ -11,11 +11,6 @@ pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::
}
s.add_bounds(synstructure::AddBounds::Generics);
let body_visit = s.each(|bind| {
quote! {
::rustc_middle::ty::fold::TypeFoldable::visit_with(#bind, __folder)?;
}
});
s.bind_with(|_| synstructure::BindStyle::Move);
let body_fold = s.each_variant(|vi| {
let bindings = vi.bindings();
@ -36,14 +31,6 @@ pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::
) -> Result<Self, __F::Error> {
Ok(match self { #body_fold })
}
fn visit_with<__F: ::rustc_middle::ty::fold::TypeVisitor<'tcx>>(
&self,
__folder: &mut __F
) -> ::std::ops::ControlFlow<__F::BreakTy> {
match *self { #body_visit }
::std::ops::ControlFlow::CONTINUE
}
},
)
}

View file

@ -0,0 +1,33 @@
use quote::quote;
use syn::parse_quote;
pub fn type_visitable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
if let syn::Data::Union(_) = s.ast().data {
panic!("cannot derive on union")
}
if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
s.add_impl_generic(parse_quote! { 'tcx });
}
s.add_bounds(synstructure::AddBounds::Generics);
let body_visit = s.each(|bind| {
quote! {
::rustc_middle::ty::visit::TypeVisitable::visit_with(#bind, __visitor)?;
}
});
s.bind_with(|_| synstructure::BindStyle::Move);
s.bound_impl(
quote!(::rustc_middle::ty::visit::TypeVisitable<'tcx>),
quote! {
fn visit_with<__V: ::rustc_middle::ty::visit::TypeVisitor<'tcx>>(
&self,
__visitor: &mut __V
) -> ::std::ops::ControlFlow<__V::BreakTy> {
match *self { #body_visit }
::std::ops::ControlFlow::CONTINUE
}
},
)
}