-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic types over Vcs #5738
Merged
alexkirsz
merged 6 commits into
main
from
alexkirsz/web-101-auto-generate-option-and-vec-types-for-values
Aug 24, 2023
Merged
Generic types over Vcs #5738
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e611467
Generic types over Vcs
alexkirsz 3e5edff
Add comments
alexkirsz 3bec62f
Update lockfile + lints
alexkirsz a0bef3d
Apply suggestions from code review
alexkirsz 13a68d0
Small fix to support generics in value_impl
alexkirsz 1ae2fe9
Merge branch 'main' into alexkirsz/web-101-auto-generate-option-and-v…
sokra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
crates/turbo-tasks-macros-shared/src/generic_type_input.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
Generics, Result, Token, Type, | ||
}; | ||
|
||
/// 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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, spanned::Spanned, visit_mut::VisitMut, GenericParam, Lifetime, Type}; | ||
use turbo_tasks_macros_shared::{get_type_ident, GenericTypeInput}; | ||
|
||
use crate::value_macro::value_type_and_register; | ||
|
||
pub fn generic_type(input: TokenStream) -> TokenStream { | ||
let 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(); | ||
} | ||
} | ||
} | ||
|
||
let (impl_generics, _, where_clause) = input.generics.split_for_impl(); | ||
|
||
let repr = replace_generics_with_unit(input.generics.params.iter(), &input.ty); | ||
|
||
let ty = input.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 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::VcCellSharedMode<#ty> | ||
}, | ||
quote! { | ||
turbo_tasks::ValueType::new_with_any_serialization::<#repr>() | ||
}, | ||
); | ||
|
||
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 std::collections::HashSet<String>, | ||
} | ||
|
||
impl<'a> VisitMut for ReplaceGenericsVisitor<'a> { | ||
fn visit_type_mut(&mut self, node: &mut Type) { | ||
if let Type::Path(type_path) = node { | ||
if 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 = syn::parse_quote! { () }; | ||
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: std::collections::HashSet<_> = 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to ensure that the generic type only appears in a VC<>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to omit the
Vc<T>
from this macro, instead requiring the following:Which would then be replaced with
Vec<Vc<()>>
orVec<Vc<T>>
depending on usage. This ensures the macro can't be used improperly.I've also added validation to ensure that you can't add bounds to the generics param of this macro.