Unverified Commit 60b5df66 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Remove dead generic_type_macro code (#88455)

I deleted the code that used this a long time ago in https://github.com/vercel/next.js/pull/70817

The rational for why I deleted the uses of this code is here (generics in turbo-tasks would be nice, but this implementation was too limited to be worth the complexity): https://github.com/vercel/turborepo/pull/8843#issuecomment-2253158412
parent 7b63bd36
use syn::{
Generics, Result, Token, Type,
parse::{Parse, ParseStream},
};
/// The input of the `generic_type` macro.
#[derive(Debug)]
pub struct GenericTypeInput {
pub generics: Generics,
pub ty: Type,
}
impl Parse for GenericTypeInput {
fn parse(input: ParseStream) -> Result<Self> {
let generics: Generics = input.parse()?;
let _comma: Token![,] = input.parse()?;
let ty: Type = input.parse()?;
Ok(GenericTypeInput { generics, ty })
}
}
use proc_macro::TokenStream;
use quote::quote;
use rustc_hash::FxHashSet;
use syn::{
GenericParam, Lifetime, Type, TypeTuple, parse_macro_input, punctuated::Punctuated,
spanned::Spanned, token, visit_mut::VisitMut,
};
use crate::{
generic_type_input::GenericTypeInput, global_name::global_name, ident::get_type_ident,
value_macro::value_type_and_register,
};
pub fn generic_type(input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as GenericTypeInput);
for param in &input.generics.params {
match param {
syn::GenericParam::Type(ty) => {
if ty.ident == "Vc" {
ty.span()
.unwrap()
.error("Vc is a reserved name in generic_type")
.emit();
}
}
syn::GenericParam::Lifetime(lt) => {
lt.span()
.unwrap()
.error("lifetime parameters are not supported in generic_type")
.emit();
}
syn::GenericParam::Const(c) => {
c.span()
.unwrap()
.error("const parameters are not supported in generic_type")
.emit();
}
}
}
// Add Send bound to input generics.
for param in &mut input.generics.params {
if let GenericParam::Type(param) = param {
param.bounds.push(syn::parse_quote! { std::marker::Send });
}
}
let (impl_generics, _, where_clause) = input.generics.split_for_impl();
let ty = &input.ty;
let repr = replace_generics_with_unit(input.generics.params.iter(), ty);
let Some(ident) = get_type_ident(ty) else {
return quote! {
// An error occurred while parsing the ident.
}
.into();
};
let mut generics_with_static = input.generics.clone();
for param in &mut generics_with_static.params {
if let GenericParam::Type(param) = param {
param.bounds.push(syn::TypeParamBound::Lifetime(Lifetime {
ident: syn::Ident::new("static", param.ident.span()),
apostrophe: param.ident.span(),
}))
}
}
let name = global_name(quote! {stringify!(#repr) });
let value_type_and_register = value_type_and_register(
&ident,
quote! { #ty },
Some(&generics_with_static),
quote! {
turbo_tasks::VcTransparentRead<#ty, #ty, #repr>
},
quote! {
turbo_tasks::VcCellCompareMode<#ty>
},
quote! {
turbo_tasks::ValueType::new_with_any_serialization::<#repr>(#name)
},
quote! { true },
);
quote! {
#value_type_and_register
impl #impl_generics Vc<#ty> #where_clause {
/// Converts this `Vc` to a generic representation.
fn to_repr(vc: Self) -> Vc<#repr> {
unsafe {
turbo_tasks::Vc::from_raw(Vc::into_raw(vc))
}
}
/// Converts a generic representation of this `Vc` to the proper `Vc` type.
///
/// # Safety
///
/// The caller must ensure that the `repr` is a valid representation of this `Vc`.
unsafe fn from_repr(vc: Vc<#repr>) -> Self {
unsafe {
turbo_tasks::Vc::from_raw(Vc::into_raw(vc))
}
}
}
}
.into()
}
struct ReplaceGenericsVisitor<'a> {
generics: &'a FxHashSet<String>,
}
impl VisitMut for ReplaceGenericsVisitor<'_> {
fn visit_type_mut(&mut self, node: &mut Type) {
if let Type::Path(type_path) = node
&& type_path.qself.is_none()
&& type_path.path.segments.len() == 1
&& type_path.path.segments[0].arguments.is_none()
&& self
.generics
.contains(&type_path.path.segments[0].ident.to_string())
{
// Replace the whole path with ()
*node = Type::Tuple(TypeTuple {
paren_token: token::Paren::default(),
elems: Punctuated::new(),
});
return;
}
syn::visit_mut::visit_type_mut(self, node);
}
}
/// Replaces all instances of `params` generic types in `ty` with the unit type
/// `()`.
fn replace_generics_with_unit<'a, P>(params: P, ty: &Type) -> Type
where
P: IntoIterator<Item = &'a GenericParam>,
{
let generics_set: FxHashSet<_> = params
.into_iter()
.filter_map(|param| {
if let GenericParam::Type(type_param) = param {
Some(type_param.ident.to_string())
} else {
None
}
})
.collect();
let mut new_ty = ty.clone();
let mut visitor = ReplaceGenericsVisitor {
generics: &generics_set,
};
syn::visit_mut::visit_type_mut(&mut visitor, &mut new_ty);
new_ty
}
...@@ -7,7 +7,6 @@ mod assert_fields; ...@@ -7,7 +7,6 @@ mod assert_fields;
mod derive; mod derive;
mod func; mod func;
mod function_macro; mod function_macro;
mod generic_type_macro;
mod global_name; mod global_name;
mod primitive_macro; mod primitive_macro;
mod value_impl_macro; mod value_impl_macro;
...@@ -15,7 +14,6 @@ mod value_macro; ...@@ -15,7 +14,6 @@ mod value_macro;
mod value_trait_macro; mod value_trait_macro;
mod expand; mod expand;
mod generic_type_input;
mod ident; mod ident;
mod primitive_input; mod primitive_input;
mod self_filter; mod self_filter;
...@@ -117,28 +115,3 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -117,28 +115,3 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
pub fn primitive(input: TokenStream) -> TokenStream { pub fn primitive(input: TokenStream) -> TokenStream {
primitive_macro::primitive(input) primitive_macro::primitive(input)
} }
/// Registers a value type that is generic over the `Vc` it contains.
///
/// # Example
///
/// ```
/// use crate::generic_type as __turbo_tasks_internal_generic_type;
///
/// __turbo_tasks_internal_generic_type!(<A, B>, GenericType<Vc<A>, Vc<B>>);
///
/// // Now you can do the following, for any `A` and `B` value types:
///
/// let vc: Vc<GenericType<Vc<u32>, Vc<RcStr>>> = Vc::cell(
/// GenericType::new(
/// Vc::cell(42),
/// Vc::cell("hello".to_string())
/// )
/// );
/// ```
#[allow_internal_unstable(min_specialization, into_future, trivial_bounds)]
#[proc_macro_error]
#[proc_macro]
pub fn generic_type(input: TokenStream) -> TokenStream {
generic_type_macro::generic_type(input)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment