Repository: vercel/next.js
Pull request: Turbopack: remove Backend TaskState

### What?

Backend TaskState is no longer used with the new backend.

Closes PACK-5470

Changed files:
filename: turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
patch: @@ -2873,9 +2873,6 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> {
         self.0.get_task_description(task)
     }
 
-    type TaskState = ();
-    fn new_task_state(&self, _task: TaskId) -> Self::TaskState {}
-
     fn task_execution_canceled(&self, task: TaskId, turbo_tasks: &dyn TurboTasksBackendApi<Self>) {
         self.0.task_execution_canceled(task, turbo_tasks)
     }

filename: turbopack/crates/turbo-tasks/src/backend.rs
patch: @@ -530,29 +530,6 @@ pub trait Backend: Sync + Send {
 
     fn get_task_description(&self, task: TaskId) -> String;
 
-    /// Task-local state that stored inside of [`TurboTasksBackendApi`]. Constructed with
-    /// [`Self::new_task_state`].
-    ///
-    /// This value that can later be written to or read from using
-    /// [`crate::TurboTasksBackendApiExt::write_task_state`] or
-    /// [`crate::TurboTasksBackendApiExt::read_task_state`]
-    ///
-    /// This data may be shared across multiple threads (must be `Sync`) in order to support
-    /// detached futures ([`crate::TurboTasksApi::detached_for_testing`]) and [pseudo-tasks using
-    /// `local` execution][crate::function]. A [`RwLock`][std::sync::RwLock] is used to provide
-    /// concurrent access.
-    type TaskState: Send + Sync + 'static;
-
-    /// Constructs a new task-local [`Self::TaskState`] for the given `task_id`.
-    ///
-    /// If a task is re-executed (e.g. because it is invalidated), this function will be called
-    /// again with the same [`TaskId`].
-    ///
-    /// This value can be written to or read from using
-    /// [`crate::TurboTasksBackendApiExt::write_task_state`] and
-    /// [`crate::TurboTasksBackendApiExt::read_task_state`]
-    fn new_task_state(&self, task: TaskId) -> Self::TaskState;
-
     fn try_start_task_execution<'a>(
         &'a self,
         task: TaskId,

filename: turbopack/crates/turbo-tasks/src/lib.rs
patch: @@ -109,9 +109,9 @@ pub use key_value_pair::KeyValuePair;
 pub use magic_any::MagicAny;
 pub use manager::{
     CurrentCellRef, ReadConsistency, TaskPersistence, TurboTasks, TurboTasksApi,
-    TurboTasksBackendApi, TurboTasksBackendApiExt, TurboTasksCallApi, Unused, UpdateInfo,
-    dynamic_call, emit, mark_finished, mark_root, mark_session_dependent, mark_stateful,
-    prevent_gc, run_once, run_once_with_reason, trait_call, turbo_tasks, turbo_tasks_scope,
+    TurboTasksBackendApi, TurboTasksCallApi, Unused, UpdateInfo, dynamic_call, emit, mark_finished,
+    mark_root, mark_session_dependent, mark_stateful, prevent_gc, run_once, run_once_with_reason,
+    trait_call, turbo_tasks, turbo_tasks_scope,
 };
 pub use output::OutputContent;
 pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError};

filename: turbopack/crates/turbo-tasks/src/manager.rs
patch: @@ -1,5 +1,4 @@
 use std::{
-    any::Any,
     future::Future,
     hash::BuildHasherDefault,
     mem::take,
@@ -262,54 +261,13 @@ pub trait TurboTasksBackendApi<B: Backend + 'static>: TurboTasksCallApi + Sync +
     /// Returns the duration from the start of the program to the given instant.
     fn program_duration_until(&self, instant: Instant) -> Duration;
 
-    /// An untyped object-safe version of [`TurboTasksBackendApiExt::read_task_state`]. Callers
-    /// should prefer the extension trait's version of this method.
-    fn read_task_state_dyn(&self, func: &mut dyn FnMut(&B::TaskState));
-
-    /// An untyped object-safe version of [`TurboTasksBackendApiExt::write_task_state`]. Callers
-    /// should prefer the extension trait's version of this method.
-    fn write_task_state_dyn(&self, func: &mut dyn FnMut(&mut B::TaskState));
-
     /// Returns true if the system is idle.
     fn is_idle(&self) -> bool;
 
     /// Returns a reference to the backend.
     fn backend(&self) -> &B;
 }
 
-/// An extension trait for methods of [`TurboTasksBackendApi`] that are not object-safe. This is
-/// automatically implemented for all [`TurboTasksBackendApi`]s using a blanket impl.
-pub trait TurboTasksBackendApiExt<B: Backend + 'static>: TurboTasksBackendApi<B> {
-    /// Allows modification of the [`Backend::TaskState`].
-    ///
-    /// This function holds open a non-exclusive read lock that blocks writes, so `func` is expected
-    /// to execute quickly in order to release the lock.
-    fn read_task_state<T>(&self, func: impl FnOnce(&B::TaskState) -> T) -> T {
-        let mut func = Some(func);
-        let mut out = None;
-        self.read_task_state_dyn(&mut |ts| out = Some((func.take().unwrap())(ts)));
-        out.expect("read_task_state_dyn must call `func`")
-    }
-
-    /// Allows modification of the [`Backend::TaskState`].
-    ///
-    /// This function holds open a write lock, so `func` is expected to execute quickly in order to
-    /// release the lock.
-    fn write_task_state<T>(&self, func: impl FnOnce(&mut B::TaskState) -> T) -> T {
-        let mut func = Some(func);
-        let mut out = None;
-        self.write_task_state_dyn(&mut |ts| out = Some((func.take().unwrap())(ts)));
-        out.expect("write_task_state_dyn must call `func`")
-    }
-}
-
-impl<TT, B> TurboTasksBackendApiExt<B> for TT
-where
-    TT: TurboTasksBackendApi<B> + ?Sized,
-    B: Backend + 'static,
-{
-}
-
 #[allow(clippy::manual_non_exhaustive)]
 pub struct UpdateInfo {
     pub duration: Duration,
@@ -408,16 +366,10 @@ struct CurrentTaskState {
     /// Tracks currently running local tasks, and defers cleanup of the global task until those
     /// complete. Also used by `detached_for_testing`.
     local_task_tracker: TaskTracker,
-
-    backend_state: Box<dyn Any + Send + Sync>,
 }
 
 impl CurrentTaskState {
-    fn new(
-        task_id: TaskId,
-        execution_id: ExecutionId,
-        backend_state: Box<dyn Any + Send + Sync>,
-    ) -> Self {
+    fn new(task_id: TaskId, execution_id: ExecutionId) -> Self {
         Self {
             task_id,
             execution_id,
@@ -426,7 +378,6 @@ impl CurrentTaskState {
             cell_counters: Some(AutoMap::default()),
             local_tasks: Vec::new(),
             local_task_tracker: TaskTracker::new(),
-            backend_state,
         }
     }
 
@@ -688,14 +639,10 @@ impl<B: Backend + 'static> TurboTasks<B> {
         let future = async move {
             let mut schedule_again = true;
             while schedule_again {
-                let backend_state = this.backend.new_task_state(task_id);
                 // it's okay for execution ids to overflow and wrap, they're just used for an assert
                 let execution_id = this.execution_id_factory.wrapping_get();
-                let current_task_state = Arc::new(RwLock::new(CurrentTaskState::new(
-                    task_id,
-                    execution_id,
-                    Box::new(backend_state),
-                )));
+                let current_task_state =
+                    Arc::new(RwLock::new(CurrentTaskState::new(task_id, execution_id)));
                 let single_execution_future = async {
                     if this.stopped.load(Ordering::Acquire) {
                         this.backend.task_execution_canceled(task_id, &*this);
@@ -1461,16 +1408,6 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> {
         unsafe { self.transient_task_id_factory.reuse(id.into()) }
     }
 
-    fn read_task_state_dyn(&self, func: &mut dyn FnMut(&B::TaskState)) {
-        CURRENT_TASK_STATE
-            .with(move |ts| func(ts.read().unwrap().backend_state.downcast_ref().unwrap()))
-    }
-
-    fn write_task_state_dyn(&self, func: &mut dyn FnMut(&mut B::TaskState)) {
-        CURRENT_TASK_STATE
-            .with(move |ts| func(ts.write().unwrap().backend_state.downcast_mut().unwrap()))
-    }
-
     fn is_idle(&self) -> bool {
         self.currently_scheduled_foreground_jobs
             .load(Ordering::Acquire)
@@ -1588,7 +1525,6 @@ pub fn with_turbo_tasks_for_testing<T>(
             Arc::new(RwLock::new(CurrentTaskState::new(
                 current_task,
                 execution_id,
-                Box::new(()),
             ))),
             f,
         ),