Files
next.js/turbopack/crates/turbo-tasks-backend/tests/immutable.rs
548fbca6df turbopack: Remove Vc::resolve(), migrate all callsites to Vc::to_resolved() (#91725)
### What?

Remove the `Vc::resolve()` method and migrate all ~37 callsites across
the turbopack and next crates to use `Vc::to_resolved()` instead.

### Why?

`Vc::resolve()` returned `Result<Vc<T>>` — a resolved `Vc` with no
static guarantee that it is resolved. This is a footgun: the type is
indistinguishable from any other unresolved `Vc<T>`, so it's easy to
accidentally lose the "resolved" property at a type boundary or store it
in a struct without the compiler enforcing invariants.

`Vc::to_resolved()` returns `Result<ResolvedVc<T>>`, which statically
encodes the resolved guarantee in the type. `ResolvedVc<T>` implements
`Deref<Target = Vc<T>>`, so it coerces seamlessly to `Vc<T>` at call
sites that still need the plain type. Removing `Vc::resolve()`
eliminates this footgun entirely and pushes callers toward the type-safe
API.

### How?

- Removed the `Vc::resolve()` method from `turbo-tasks/src/vc/mod.rs`.
- Updated `Vc::to_resolved()` to construct the inner `Vc` directly via
`self.node.resolve()` rather than calling `self.resolve()`.
- Updated `Vc::debug_identifier` to go through `to_resolved()`.
- Updated `TaskInput::resolve_input` for `Vc<T>` to use `to_resolved()`.
- Updated all callsites across turbopack-core, turbopack-ecmascript,
turbopack-resolve, turbopack-browser, turbopack-css, turbopack-node,
turbopack-ecmascript-runtime, next-api, next-core, and test/fuzz files:
- Where `ResolvedVc<T>` is usable (deref coercion handles method calls,
or the value is stored in a `HashMap` etc.), simply replaced
`.resolve()` with `.to_resolved()`.
- Where a plain `Vc<T>` is strictly required by a callee signature,
dereferenced with `*x.to_resolved().await?`.
- Updated the `AfterResolvePluginWithCondition` type alias in
`resolve/mod.rs` to use `ResolvedVc<AfterResolvePluginCondition>` now
that the tuple member is produced by `to_resolved()`.
- Updated doc comments referencing `Vc::resolve` to point to
`Vc::to_resolved`.

Verified with `cargo check --all-targets` — compiles cleanly with no
errors.

---------

Co-authored-by: Tobias Koppers <[email protected]>
Co-authored-by: Claude <[email protected]>
2026-03-26 11:30:36 -07:00

108 lines
3.4 KiB
Rust

#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
use anyhow::Result;
use turbo_tasks::{State, Vc, unmark_top_level_task_may_leak_eventually_consistent_state};
use turbo_tasks_testing::{Registration, register, run_once};
static REGISTRATION: Registration = register!();
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_hidden_mutate() {
run_once(&REGISTRATION, || async {
unmark_top_level_task_may_leak_eventually_consistent_state();
let input = *create_input().to_resolved().await?;
input.await?.state.set(1);
let changing_value = compute(input);
assert_eq!(changing_value.await?.value, 1);
let changing_value_resolved = *changing_value.to_resolved().await?;
let read_input = read_input(changing_value_resolved);
let static_immutable = immutable_fn(changing_value_resolved);
let read_self = changing_value_resolved.read_self();
let static_immutable_self = changing_value_resolved.immutable_self_fn();
assert_eq!(*read_input.await?, 1);
assert_eq!(*static_immutable.await?, 42);
assert_eq!(*read_self.await?, 1);
assert_eq!(*static_immutable_self.await?, 42);
println!("changing input");
input.await?.state.set(10);
assert_eq!(changing_value.strongly_consistent().await?.value, 10);
assert_eq!(*read_input.strongly_consistent().await?, 10);
assert_eq!(*static_immutable.strongly_consistent().await?, 42);
assert_eq!(*read_self.strongly_consistent().await?, 10);
assert_eq!(*static_immutable_self.strongly_consistent().await?, 42);
println!("changing input");
input.await?.state.set(5);
assert_eq!(changing_value.strongly_consistent().await?.value, 5);
assert_eq!(*read_input.strongly_consistent().await?, 5);
assert_eq!(*static_immutable.strongly_consistent().await?, 42);
assert_eq!(*read_self.strongly_consistent().await?, 5);
assert_eq!(*static_immutable_self.strongly_consistent().await?, 42);
anyhow::Ok(())
})
.await
.unwrap();
}
#[turbo_tasks::value]
struct ChangingInput {
state: State<u32>,
}
#[turbo_tasks::value]
struct Value {
value: u32,
}
#[turbo_tasks::function]
async fn create_input() -> Result<Vc<ChangingInput>> {
println!("create_input()");
Ok(ChangingInput {
state: State::new(0),
}
.cell())
}
#[turbo_tasks::function]
async fn compute(input: Vc<ChangingInput>) -> Result<Vc<Value>> {
println!("compute()");
let input = input.await?;
let value = input.state.get();
Ok(Value { value: *value }.cell())
}
#[turbo_tasks::function]
async fn read_input(input: Vc<Value>) -> Result<Vc<u32>> {
println!("read_input()");
let value = input.await?;
Ok(Vc::cell(value.value))
}
#[turbo_tasks::function]
fn immutable_fn(input: Vc<Value>) -> Vc<u32> {
let _ = input;
println!("immutable_fn()");
Vc::cell(42)
}
#[turbo_tasks::value_impl]
impl Value {
#[turbo_tasks::function]
fn read_self(&self) -> Vc<u32> {
println!("read_self()");
Vc::cell(self.value)
}
#[turbo_tasks::function]
fn immutable_self_fn(self: Vc<Value>) -> Vc<u32> {
let _ = self;
println!("immutable_self_fn()");
Vc::cell(42)
}
}