Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(turbo-tasks-macros): Remove use of associated items for NativeF…
…unction construction (#73929) After the toolchain upgrade, causing a compilation error in a `#[turbo_tasks::value]` (or somewhere adjacent) can cause this additional non-fatal compilation warning/note: ``` note: erroneous constant encountered --> crates/next-api/src/project.rs:580:1 | 580 | #[turbo_tasks::value_impl] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ in this procedural macro expansion | ::: /home/bgw.linux/next.js/turbopack/crates/turbo-tasks-macros/src/lib.rs:119:1 | 119 | #[proc_macro_error] | ------------------- in this expansion of `#[turbo_tasks::value_impl]` ``` The use of const expressions here was a little whacky. This removes the need for the const associated items by inlining their (one and only) use later when creating the `Lazy` static. This simplifies the macro implementation, reduces the total resulting code size, and makes the expanded output a little more legible. ## 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 { #[allow(declare_interior_mutable_const)] #[doc(hidden)] const 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, ) }); #[allow(declare_interior_mutable_const)] #[doc(hidden)] const 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) }); #[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, > = <Completion>::COMPLETION_IMPL_IMMUTABLE_FUNCTION; #[doc(hidden)] pub(crate) static COMPLETION_IMPL_IMMUTABLE_FUNCTION_ID: turbo_tasks::macro_helpers::Lazy< turbo_tasks::FunctionId, > = <Completion>::COMPLETION_IMPL_IMMUTABLE_FUNCTION_ID; ``` ## 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 = " 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) }); ```
- Loading branch information