Files
next.js/turbopack/crates/turbo-tasks-backend/tests/debug.rs
Luke SandbergandGitHub af83b4b309 Drop turbo_tasks::function from ValueDebug trait items (#92593)
## What?

Simplify the `ValueDebug` trait in turbo-tasks by removing turbo-tasks function overhead from `dbg` and `dbg_depth`.

## Why?

Every `#[turbo_tasks::value]` and `#[turbo_tasks::value_trait]` type previously generated two `#[turbo_tasks::function]` implementations of `dbg` and `dbg_depth`. These are debugging utilities that are almost never called in production, but the turbo-tasks function machinery (task registration, vtable entries, `NativeFunction` statics, inline extension traits) was emitted for every type regardless — contributing to binary bloat.

## How?

- Change `dbg` and `dbg_depth` on the `ValueDebug` trait from `#[turbo_tasks::function]` methods to plain methods returning `Pin<Box<dyn Future<...>>>` (required for object safety without `async_trait`)
- Add a default `dbg` implementation on the trait itself that delegates to `dbg_depth(usize::MAX)`, so derived impls only need to implement `dbg_depth`
- Remove `ValueDebugString` wrapper type — callers now get a plain `String` back
- Callers use `vc.into_trait_ref().await?` then call `.dbg()` / `.dbg_depth()` directly on the trait ref
- Update the four macro locations that generate `ValueDebug` impls: `value_debug_macro.rs`, `value_macro.rs`, `primitive_macro.rs`, and the call site in `vc/mod.rs`
- `#[turbo_tasks::value_impl]` is kept on the generated `impl ValueDebug for T` blocks so that `Upcast`/`UpcastStrict` and `CollectableTraitCastFunctions` (needed for `into_trait_ref`) are still registered

## Binary size impact (darwin-arm64, compared against canary)

| Metric | Canary | This PR | Delta |
|---|---|---|---|
| Unstripped dylib | 128.6 MB | 124.6 MB | **-4.0 MB (-3.1%)** |
| Stripped | 87.2 MB | 84.7 MB | **-2.5 MB (-2.9%)** |
| Stripped + gzip (npm) | 29.6 MB | 29.2 MB | **-0.4 MB (-1.4%)** |

<!-- NEXT_JS_LLM_PR -->
2026-04-10 12:15:40 -07:00

292 lines
7.6 KiB
Rust

#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
use std::sync::Mutex;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc, debug::ValueDebug};
use turbo_tasks_testing::{Registration, register, run_once};
static REGISTRATION: Registration = register!();
#[turbo_tasks::function(operation)]
async fn dbg_operation(value: ResolvedVc<Box<dyn ValueDebug>>) -> anyhow::Result<Vc<RcStr>> {
let trait_ref = value.into_trait_ref().await?;
let s = trait_ref.dbg().await?;
Ok(Vc::cell(s.into()))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_primitive_debug() {
run_once(&REGISTRATION, move || async move {
let a = ResolvedVc::<u32>::cell(42);
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"42",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_transparent_debug() {
run_once(&REGISTRATION, move || async move {
let a = Transparent(42).resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"42",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_enum_none_debug() {
run_once(&REGISTRATION, move || async move {
let a = Enum::None.resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"Enum :: None",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_enum_transparent_debug() {
run_once(&REGISTRATION, move || async move {
let a = Enum::Transparent(Transparent(42).resolved_cell()).resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"Enum :: Transparent(\n 42,\n)",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_enum_inner_vc_debug() {
run_once(&REGISTRATION, move || async move {
let a = Enum::Enum(Enum::None.resolved_cell()).resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"Enum :: Enum(\n Enum :: None,\n)",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_struct_unit_debug() {
run_once(&REGISTRATION, move || async move {
let a = StructUnit.resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"StructUnit",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_struct_transparent_debug() {
run_once(&REGISTRATION, move || async move {
let a = StructWithTransparent {
transparent: Transparent(42).resolved_cell(),
}
.resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"StructWithTransparent {\n transparent: 42,\n}",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_struct_option_debug() {
run_once(&REGISTRATION, || async {
let a = StructWithOption { option: None }.resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"StructWithOption {\n option: None,\n}",
);
let b = StructWithOption {
option: Some(Transparent(42).resolved_cell()),
}
.resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(b))
.read_strongly_consistent()
.await?,
),
"StructWithOption {\n option: Some(\n 42,\n ),\n}",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_struct_vec_debug() {
run_once(&REGISTRATION, || async {
let a = StructWithVec { vec: Vec::new() }.resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"StructWithVec {\n vec: [],\n}"
);
let b = StructWithVec {
vec: vec![Transparent(42).resolved_cell()],
}
.resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(b))
.read_strongly_consistent()
.await?,
),
"StructWithVec {\n vec: [\n 42,\n ],\n}",
);
Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_struct_ignore_debug() {
run_once(&REGISTRATION, || async {
let a = StructWithIgnore {
dont_ignore: 42,
ignore: Mutex::new(()),
}
.resolved_cell();
assert_eq!(
format!(
"{}",
dbg_operation(ResolvedVc::upcast(a))
.read_strongly_consistent()
.await?,
),
"StructWithIgnore {\n dont_ignore: 42,\n}",
);
Ok(())
})
.await
.unwrap()
}
#[turbo_tasks::value(transparent, shared)]
struct Transparent(u32);
// Allow Enum::Enum
#[allow(clippy::enum_variant_names)]
#[turbo_tasks::value(shared)]
enum Enum {
None,
Transparent(ResolvedVc<Transparent>),
Enum(ResolvedVc<Enum>),
}
#[turbo_tasks::value(shared)]
struct StructUnit;
#[turbo_tasks::value(shared)]
struct StructWithTransparent {
transparent: ResolvedVc<Transparent>,
}
#[turbo_tasks::value(shared)]
struct StructWithOption {
option: Option<ResolvedVc<Transparent>>,
}
#[turbo_tasks::value(shared)]
struct StructWithVec {
vec: Vec<ResolvedVc<Transparent>>,
}
#[turbo_tasks::value(shared, eq = "manual")]
struct StructWithIgnore {
dont_ignore: u32,
// We're using a `Mutex` instead of a `T: Debug` type to ensure we support `T: !Debug`.
#[turbo_tasks(debug_ignore, trace_ignore)]
ignore: Mutex<()>,
}
impl PartialEq for StructWithIgnore {
fn eq(&self, other: &Self) -> bool {
self.dont_ignore == other.dont_ignore
}
}
impl Eq for StructWithIgnore {}