Skip to content
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

#[turbo_tasks::value(transparent)]: Generate docs & fail on invalid callsites #8087

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turbo-tasks-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ anyhow = { workspace = true }
proc-macro-error = "1.0.4"
proc-macro2 = { workspace = true }
quote = { workspace = true }
regex = { workspace = true }
syn = { workspace = true, features = ["full", "extra-traits", "visit-mut"] }
turbo-tasks-macros-shared = { workspace = true }
77 changes: 59 additions & 18 deletions crates/turbo-tasks-macros/src/value_macro.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::sync::OnceLock;

use proc_macro::TokenStream;
use proc_macro2::Ident;
use quote::quote;
use quote::{quote, ToTokens};
use regex::Regex;
use syn::{
parse::{Parse, ParseStream},
parse_macro_input,
parse_macro_input, parse_quote,
punctuated::Punctuated,
spanned::Spanned,
Error, Fields, FieldsUnnamed, Generics, Item, ItemEnum, ItemStruct, Lit, LitStr, Meta,
Expand Down Expand Up @@ -189,7 +192,7 @@ impl Parse for ValueArguments {
}

pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as Item);
let mut item = parse_macro_input!(input as Item);
let ValueArguments {
serialization_mode,
into_mode,
Expand All @@ -198,6 +201,58 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
transparent,
} = parse_macro_input!(args as ValueArguments);

let mut inner_type = None;
if transparent {
if let Item::Struct(ItemStruct {
attrs,
fields: Fields::Unnamed(FieldsUnnamed { unnamed, .. }),
..
}) = &mut item
{
if unnamed.len() == 1 {
let field = unnamed.iter().next().unwrap();
inner_type = Some(field.ty.clone());

// generate a type string to add to the docs
let inner_type_string = inner_type.to_token_stream().to_string();

// HACK: proc_macro2 inserts whitespace between every token. It's ugly, so
// remove it, assuming these whitespace aren't syntatically important. Using
// prettyplease (or similar) would be more correct, but slower and add another
// dependency.
static WHITESPACE_RE: OnceLock<Regex> = OnceLock::new();
// Remove whitespace, as long as there is a non-word character (e.g. `>` or `,`)
// on either side. Try not to remove whitespace between `dyn Trait`.
let whitespace_re = WHITESPACE_RE.get_or_init(|| {
Regex::new(r"\b \B|\B \b|\B \B").expect("WHITESPACE_RE is valid")
});
let inner_type_string = whitespace_re.replace_all(&inner_type_string, "");

// Add a couple blank lines in case there's already a doc comment we're
// effectively appending to. If there's not, rustdoc will strip
// the leading whitespace.
let doc_str = format!(
"\n\nThis is a [transparent value type][::turbo_tasks::value#transparent] \
wrapping [`{}`].",
inner_type_string,
);

attrs.push(parse_quote! {
#[doc = #doc_str]
});
}
}
if inner_type.is_none() {
item.span()
.unwrap()
.error(
"#[turbo_tasks::value(transparent)] is only valid with single-item unit \
structs",
)
.emit();
}
}

let ident = match &item {
Item::Enum(ItemEnum { ident, .. }) => ident,
Item::Struct(ItemStruct { ident, .. }) => ident,
Expand All @@ -211,20 +266,6 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
}
};

let mut inner_type = None;
if transparent {
if let Item::Struct(ItemStruct {
fields: Fields::Unnamed(FieldsUnnamed { unnamed, .. }),
..
}) = &item
{
if unnamed.len() == 1 {
let field = unnamed.iter().next().unwrap();
inner_type = Some(&field.ty);
}
}
}

let cell_mode = match cell_mode {
CellMode::New => quote! {
turbo_tasks::VcCellNewMode<#ident>
Expand All @@ -234,7 +275,7 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
},
};

let (cell_prefix, cell_access_content, read) = if let Some(inner_type) = inner_type {
let (cell_prefix, cell_access_content, read) = if let Some(inner_type) = &inner_type {
(
quote! { pub },
quote! {
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-css/src/module_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Asset for ModuleCssAsset {
/// A CSS class that is exported from a CSS module.
///
/// See [`ModuleCssClasses`] for more information.
#[turbo_tasks::value(transparent)]
#[turbo_tasks::value]
#[derive(Debug, Clone)]
enum ModuleCssClass {
Local {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ use turbopack_ecmascript::{CustomTransformer, TransformContext};
/// Internally this contains a `CompiledPluginModuleBytes`, which points to the
/// compiled, serialized wasmer::Module instead of raw file bytes to reduce the
/// cost of the compilation.
#[turbo_tasks::value(
transparent,
serialization = "none",
eq = "manual",
into = "new",
cell = "new"
)]
#[turbo_tasks::value(serialization = "none", eq = "manual", into = "new", cell = "new")]
pub struct SwcPluginModule(
#[turbo_tasks(trace_ignore)]
#[cfg(feature = "swc_ecma_transform_plugin")]
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-node/src/route_matcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use indexmap::IndexMap;
use turbo_tasks::Vc;

#[turbo_tasks::value(transparent)]
#[turbo_tasks::value]
#[derive(Debug, Clone)]
#[serde(untagged)]
pub enum Param {
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-node/src/transforms/postcss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::{

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[turbo_tasks::value(transparent, serialization = "custom")]
#[turbo_tasks::value(serialization = "custom")]
struct PostCssProcessingResult {
css: String,
map: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-node/src/transforms/webpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::{

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[turbo_tasks::value(transparent, serialization = "custom")]
#[turbo_tasks::value(serialization = "custom")]
struct WebpackLoadersProcessingResult {
source: String,
map: Option<String>,
Expand Down
Loading