]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_macros/src/type_visitable.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / compiler / rustc_macros / src / type_visitable.rs
1 use quote::quote;
2 use syn::parse_quote;
3
4 pub fn type_visitable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
5 if let syn::Data::Union(_) = s.ast().data {
6 panic!("cannot derive on union")
7 }
8
9 if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
10 s.add_impl_generic(parse_quote! { 'tcx });
11 }
12
13 s.add_bounds(synstructure::AddBounds::Generics);
14 let body_visit = s.each(|bind| {
15 quote! {
16 ::rustc_middle::ty::visit::TypeVisitable::visit_with(#bind, __visitor)?;
17 }
18 });
19 s.bind_with(|_| synstructure::BindStyle::Move);
20
21 s.bound_impl(
22 quote!(::rustc_middle::ty::visit::TypeVisitable<'tcx>),
23 quote! {
24 fn visit_with<__V: ::rustc_middle::ty::visit::TypeVisitor<'tcx>>(
25 &self,
26 __visitor: &mut __V
27 ) -> ::std::ops::ControlFlow<__V::BreakTy> {
28 match *self { #body_visit }
29 ::std::ops::ControlFlow::Continue(())
30 }
31 },
32 )
33 }