Unverified Commit e3c1de4d authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(turbo-tasks-macros): Strip (already hidden) doc attributes from inline…

refactor(turbo-tasks-macros): Strip (already hidden) doc attributes from inline function signatures (#73931)

A minor optimization to the generated code. We were duplicating doc comments on the hidden `_turbo_tasks_function_inline` versions of functions.

We only need the docs on the public function.

This change should mean *slightly* less code for the macro to emit, and *slightly* less code for rustc to parse.

## Before

```
// ===========================================
// Recursive expansion of the value_impl macro
// ===========================================

impl Completion {
    #[doc = " This will always be the same and never invalidates the reading task."]
    pub fn immutable() -> Vc<Self> {
        let inputs = std::boxed::Box::new(());
        let persistence =
            turbo_tasks::macro_helpers::get_non_local_persistence_from_inputs(&*inputs);
        <Vc<Self> as turbo_tasks::task::TaskOutput>::try_from_raw_vc(turbo_tasks::dynamic_call(
            *COMPLETION_IMPL_IMMUTABLE_FUNCTION_ID,
            inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
            persistence,
        ))
    }
}
#[doc(hidden)]
impl Completion {
    #[doc = " This will always be the same and never invalidates the reading task."]
    #[doc(hidden)]
    #[deprecated(
        note = "This function is only exposed for use in macros. Do not call it directly."
    )]
    pub(self) fn immutable_turbo_tasks_function_inline() -> Vc<Self> {
        Completion::cell(Completion)
    }
}
#[doc(hidden)]
pub(crate) static COMPLETION_IMPL_IMMUTABLE_FUNCTION: turbo_tasks::macro_helpers::Lazy<
    turbo_tasks::NativeFunction,
> = turbo_tasks::macro_helpers::Lazy::new(|| {
    #[allow(deprecated)]
    turbo_tasks::NativeFunction::new_function(
        "Completion::immutable".to_owned(),
        turbo_tasks::FunctionMeta { local_cells: false },
        <Completion>::immutable_turbo_tasks_function_inline,
    )
});
#[doc(hidden)]
pub(crate) static COMPLETION_IMPL_IMMUTABLE_FUNCTION_ID: turbo_tasks::macro_helpers::Lazy<
    turbo_tasks::FunctionId,
> = turbo_tasks::macro_helpers::Lazy::new(|| {
    turbo_tasks::registry::get_function_id(&*COMPLETION_IMPL_IMMUTABLE_FUNCTION)
});
```

## After

```
// ===========================================
// Recursive expansion of the value_impl macro
// ===========================================

impl Completion {
    #[doc = " This will always be the same and never invalidates the reading task."]
    pub fn immutable() -> Vc<Self> {
        let inputs = std::boxed::Box::new(());
        let persistence =
            turbo_tasks::macro_helpers::get_non_local_persistence_from_inputs(&*inputs);
        <Vc<Self> as turbo_tasks::task::TaskOutput>::try_from_raw_vc(turbo_tasks::dynamic_call(
            *COMPLETION_IMPL_IMMUTABLE_FUNCTION_ID,
            inputs as std::boxed::Box<dyn turbo_tasks::MagicAny>,
            persistence,
        ))
    }
}
#[doc(hidden)]
impl Completion {
    #[doc(hidden)]
    #[deprecated(
        note = "This function is only exposed for use in macros. Do not call it directly."
    )]
    pub(self) fn immutable_turbo_tasks_function_inline() -> Vc<Self> {
        Completion::cell(Completion)
    }
}
#[doc(hidden)]
pub(crate) static COMPLETION_IMPL_IMMUTABLE_FUNCTION: turbo_tasks::macro_helpers::Lazy<
    turbo_tasks::NativeFunction,
> = turbo_tasks::macro_helpers::Lazy::new(|| {
    #[allow(deprecated)]
    turbo_tasks::NativeFunction::new_function(
        "Completion::immutable".to_owned(),
        turbo_tasks::FunctionMeta { local_cells: false },
        <Completion>::immutable_turbo_tasks_function_inline,
    )
});
#[doc(hidden)]
pub(crate) static COMPLETION_IMPL_IMMUTABLE_FUNCTION_ID: turbo_tasks::macro_helpers::Lazy<
    turbo_tasks::FunctionId,
> = turbo_tasks::macro_helpers::Lazy::new(|| {
    turbo_tasks::registry::get_function_id(&*COMPLETION_IMPL_IMMUTABLE_FUNCTION)
});
```
parent 7ae9c790
...@@ -10,9 +10,9 @@ use syn::{ ...@@ -10,9 +10,9 @@ use syn::{
spanned::Spanned, spanned::Spanned,
token::Paren, token::Paren,
visit_mut::VisitMut, visit_mut::VisitMut,
AngleBracketedGenericArguments, Block, Expr, ExprBlock, ExprPath, FnArg, GenericArgument, AngleBracketedGenericArguments, Attribute, Block, Expr, ExprBlock, ExprPath, FnArg,
Local, Meta, Pat, PatIdent, PatType, Path, PathArguments, PathSegment, Receiver, ReturnType, GenericArgument, Local, Meta, Pat, PatIdent, PatType, Path, PathArguments, PathSegment,
Signature, Stmt, Token, Type, TypeGroup, TypePath, TypeTuple, Receiver, ReturnType, Signature, Stmt, Token, Type, TypeGroup, TypePath, TypeTuple,
}; };
#[derive(Debug)] #[derive(Debug)]
...@@ -1020,3 +1020,13 @@ impl NativeFn { ...@@ -1020,3 +1020,13 @@ impl NativeFn {
} }
} }
} }
pub fn filter_inline_attributes<'a>(
attrs: impl IntoIterator<Item = &'a Attribute>,
) -> Vec<&'a Attribute> {
// inline functions use #[doc(hidden)], so it's not useful to preserve/duplicate docs
attrs
.into_iter()
.filter(|attr| attr.path.get_ident().is_none_or(|id| id != "doc"))
.collect()
}
...@@ -3,7 +3,9 @@ use quote::quote; ...@@ -3,7 +3,9 @@ use quote::quote;
use syn::{parse_macro_input, parse_quote, ItemFn}; use syn::{parse_macro_input, parse_quote, ItemFn};
use turbo_tasks_macros_shared::{get_native_function_id_ident, get_native_function_ident}; use turbo_tasks_macros_shared::{get_native_function_id_ident, get_native_function_ident};
use crate::func::{DefinitionContext, FunctionArguments, NativeFn, TurboFn}; use crate::func::{
filter_inline_attributes, DefinitionContext, FunctionArguments, NativeFn, TurboFn,
};
/// This macro generates the virtual function that powers turbo tasks. /// This macro generates the virtual function that powers turbo tasks.
/// An annotated task is replaced with a stub function that returns a /// An annotated task is replaced with a stub function that returns a
...@@ -50,6 +52,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -50,6 +52,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream {
let inline_function_ident = turbo_fn.inline_ident(); let inline_function_ident = turbo_fn.inline_ident();
let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(&block); let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(&block);
let inline_attrs = filter_inline_attributes(&attrs[..]);
let native_fn = NativeFn::new( let native_fn = NativeFn::new(
&ident.to_string(), &ident.to_string(),
...@@ -72,7 +75,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -72,7 +75,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream {
#(#attrs)* #(#attrs)*
#vis #exposed_signature #exposed_block #vis #exposed_signature #exposed_block
#(#attrs)* #(#inline_attrs)*
#[doc(hidden)] #[doc(hidden)]
#inline_signature #inline_block #inline_signature #inline_block
......
...@@ -15,7 +15,10 @@ use turbo_tasks_macros_shared::{ ...@@ -15,7 +15,10 @@ use turbo_tasks_macros_shared::{
get_trait_impl_function_ident, get_type_ident, get_trait_impl_function_ident, get_type_ident,
}; };
use crate::func::{DefinitionContext, FunctionArguments, MaybeParenthesized, NativeFn, TurboFn}; use crate::func::{
filter_inline_attributes, DefinitionContext, FunctionArguments, MaybeParenthesized, NativeFn,
TurboFn,
};
fn is_attribute(attr: &Attribute, name: &str) -> bool { fn is_attribute(attr: &Attribute, name: &str) -> bool {
let path = &attr.path; let path = &attr.path;
...@@ -130,6 +133,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -130,6 +133,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
}; };
let inline_function_ident = turbo_fn.inline_ident(); let inline_function_ident = turbo_fn.inline_ident();
let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(block); let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(block);
let inline_attrs = filter_inline_attributes(attrs.iter().copied());
let native_fn = NativeFn::new( let native_fn = NativeFn::new(
&format!("{ty}::{ident}", ty = ty.to_token_stream()), &format!("{ty}::{ident}", ty = ty.to_token_stream()),
...@@ -160,7 +164,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -160,7 +164,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
// By declaring the native function's body within an `impl` block, we ensure // By declaring the native function's body within an `impl` block, we ensure
// that `Self` refers to `#ty`. This is necessary because the function's // that `Self` refers to `#ty`. This is necessary because the function's
// body is originally declared within an `impl` block already. // body is originally declared within an `impl` block already.
#(#attrs)* #(#inline_attrs)*
#[doc(hidden)] #[doc(hidden)]
#[deprecated(note = "This function is only exposed for use in macros. Do not call it directly.")] #[deprecated(note = "This function is only exposed for use in macros. Do not call it directly.")]
pub(self) #inline_signature #inline_block pub(self) #inline_signature #inline_block
...@@ -233,6 +237,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -233,6 +237,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
ident.span(), ident.span(),
); );
let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(block); let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(block);
let inline_attrs = filter_inline_attributes(attrs.iter().copied());
let native_fn = NativeFn::new( let native_fn = NativeFn::new(
&format!( &format!(
...@@ -270,14 +275,14 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -270,14 +275,14 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
#[doc(hidden)] #[doc(hidden)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
trait #inline_extension_trait_ident: std::marker::Send { trait #inline_extension_trait_ident: std::marker::Send {
#(#attrs)* #(#inline_attrs)*
#[doc(hidden)] #[doc(hidden)]
#inline_signature; #inline_signature;
} }
#[doc(hidden)] #[doc(hidden)]
impl #impl_generics #inline_extension_trait_ident for #ty #where_clause { impl #impl_generics #inline_extension_trait_ident for #ty #where_clause {
#(#attrs)* #(#inline_attrs)*
#[doc(hidden)] #[doc(hidden)]
#[deprecated(note = "This function is only exposed for use in macros. Do not call it directly.")] #[deprecated(note = "This function is only exposed for use in macros. Do not call it directly.")]
#inline_signature #inline_block #inline_signature #inline_block
......
...@@ -9,7 +9,9 @@ use turbo_tasks_macros_shared::{ ...@@ -9,7 +9,9 @@ use turbo_tasks_macros_shared::{
get_trait_type_id_ident, get_trait_type_ident, ValueTraitArguments, get_trait_type_id_ident, get_trait_type_ident, ValueTraitArguments,
}; };
use crate::func::{DefinitionContext, FunctionArguments, NativeFn, TurboFn}; use crate::func::{
filter_inline_attributes, DefinitionContext, FunctionArguments, NativeFn, TurboFn,
};
pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let ValueTraitArguments { let ValueTraitArguments {
...@@ -114,6 +116,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -114,6 +116,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let inline_extension_trait_ident = let inline_extension_trait_ident =
Ident::new(&format!("{}_{}_inline", trait_ident, ident), ident.span()); Ident::new(&format!("{}_{}_inline", trait_ident, ident), ident.span());
let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(default); let (inline_signature, inline_block) = turbo_fn.inline_signature_and_block(default);
let inline_attrs = filter_inline_attributes(&attrs[..]);
let native_function = NativeFn::new( let native_function = NativeFn::new(
&format!("{trait_ident}::{ident}"), &format!("{trait_ident}::{ident}"),
...@@ -146,7 +149,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -146,7 +149,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
#[doc(hidden)] #[doc(hidden)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
trait #inline_extension_trait_ident: std::marker::Send { trait #inline_extension_trait_ident: std::marker::Send {
#(#attrs)* #(#inline_attrs)*
#inline_signature; #inline_signature;
} }
...@@ -155,7 +158,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -155,7 +158,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
// in the inline signature. // in the inline signature.
impl #inline_extension_trait_ident for Box<dyn #trait_ident> { impl #inline_extension_trait_ident for Box<dyn #trait_ident> {
// put the function body here so that `Self` points to `Box<dyn ...>` // put the function body here so that `Self` points to `Box<dyn ...>`
#(#attrs)* #(#inline_attrs)*
#inline_signature #inline_block #inline_signature #inline_block
} }
......
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