diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /compiler/rustc_macros/src/lift.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_macros/src/lift.rs')
-rw-r--r-- | compiler/rustc_macros/src/lift.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/compiler/rustc_macros/src/lift.rs b/compiler/rustc_macros/src/lift.rs new file mode 100644 index 000000000..ad7ac7404 --- /dev/null +++ b/compiler/rustc_macros/src/lift.rs @@ -0,0 +1,52 @@ +use quote::quote; +use syn::{self, parse_quote}; + +pub fn lift_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream { + s.add_bounds(synstructure::AddBounds::Generics); + s.bind_with(|_| synstructure::BindStyle::Move); + + let tcx: syn::Lifetime = parse_quote!('tcx); + let newtcx: syn::GenericParam = parse_quote!('__lifted); + + let lifted = { + let ast = s.ast(); + let ident = &ast.ident; + + // Replace `'tcx` lifetime by the `'__lifted` lifetime + let (_, generics, _) = ast.generics.split_for_impl(); + let mut generics: syn::AngleBracketedGenericArguments = syn::parse_quote! { #generics }; + for arg in generics.args.iter_mut() { + match arg { + syn::GenericArgument::Lifetime(l) if *l == tcx => { + *arg = parse_quote!('__lifted); + } + syn::GenericArgument::Type(t) => { + *arg = syn::parse_quote! { #t::Lifted }; + } + _ => {} + } + } + + quote! { #ident #generics } + }; + + let body = s.each_variant(|vi| { + let bindings = &vi.bindings(); + vi.construct(|_, index| { + let bi = &bindings[index]; + quote! { __tcx.lift(#bi)? } + }) + }); + + s.add_impl_generic(newtcx); + s.bound_impl( + quote!(::rustc_middle::ty::Lift<'__lifted>), + quote! { + type Lifted = #lifted; + + fn lift_to_tcx(self, __tcx: ::rustc_middle::ty::TyCtxt<'__lifted>) -> Option<#lifted> { + Some(match self { #body }) + } + }, + ) +} |