mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
### 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]>
130 lines
3.6 KiB
Rust
130 lines
3.6 KiB
Rust
#![feature(arbitrary_self_types)]
|
|
#![feature(arbitrary_self_types_pointers)]
|
|
#![allow(clippy::needless_return)] // clippy bug causes false positive
|
|
|
|
use anyhow::{Result, bail};
|
|
use turbo_rcstr::RcStr;
|
|
use turbo_tasks::{
|
|
CollectiblesSource, ResolvedVc, State, ValueToString, Vc, emit,
|
|
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 recompute() {
|
|
run_once(®ISTRATION, || async {
|
|
unmark_top_level_task_may_leak_eventually_consistent_state();
|
|
let input = *ChangingInput::new(1).to_resolved().await?;
|
|
let input2 = *ChangingInput::new(2).to_resolved().await?;
|
|
input.await?.state.set(1);
|
|
input2.await?.state.set(1000);
|
|
let output = compute(input, input2, 1);
|
|
let read = output.strongly_consistent().await?;
|
|
assert_eq!(read.value, 42);
|
|
assert_eq!(read.collectible, "1");
|
|
|
|
for i in 2..100 {
|
|
input.await?.state.set(i);
|
|
let read = output.strongly_consistent().await?;
|
|
assert_eq!(read.value, 42);
|
|
assert_eq!(read.collectible, i.to_string());
|
|
}
|
|
|
|
for i in 0..100 {
|
|
input2.await?.state.set(i);
|
|
let read = output.strongly_consistent().await?;
|
|
assert_eq!(read.value, 42);
|
|
assert_eq!(read.collectible, "99");
|
|
}
|
|
anyhow::Ok(())
|
|
})
|
|
.await
|
|
.unwrap()
|
|
}
|
|
|
|
#[turbo_tasks::value]
|
|
struct ChangingInput {
|
|
state: State<u32>,
|
|
}
|
|
|
|
#[turbo_tasks::value_impl]
|
|
impl ChangingInput {
|
|
#[turbo_tasks::function]
|
|
fn new(key: u32) -> Vc<Self> {
|
|
let _ = key;
|
|
Self {
|
|
state: State::new(1),
|
|
}
|
|
.cell()
|
|
}
|
|
}
|
|
|
|
#[turbo_tasks::value]
|
|
struct Output {
|
|
value: u32,
|
|
collectible: String,
|
|
}
|
|
|
|
#[turbo_tasks::value]
|
|
struct Collectible {
|
|
value: u32,
|
|
}
|
|
|
|
#[turbo_tasks::value_impl]
|
|
impl ValueToString for Collectible {
|
|
#[turbo_tasks::function]
|
|
fn to_string(&self) -> Vc<RcStr> {
|
|
Vc::cell(self.value.to_string().into())
|
|
}
|
|
}
|
|
|
|
#[turbo_tasks::function(operation)]
|
|
async fn inner_compute(
|
|
input: ResolvedVc<ChangingInput>,
|
|
input2: ResolvedVc<ChangingInput>,
|
|
) -> Result<Vc<u32>> {
|
|
println!("inner_compute()");
|
|
Ok(inner_compute2(*input, *input2.await?.state.get()))
|
|
}
|
|
|
|
#[turbo_tasks::function]
|
|
async fn inner_compute2(input: Vc<ChangingInput>, innerness: u32) -> Result<Vc<u32>> {
|
|
println!("inner_compute2({innerness})");
|
|
if innerness > 0 {
|
|
return Ok(inner_compute2(input, innerness - 1));
|
|
}
|
|
let value = *input.await?.state.get();
|
|
let collectible: ResolvedVc<Box<dyn ValueToString>> =
|
|
ResolvedVc::upcast(Collectible { value }.resolved_cell());
|
|
emit(collectible);
|
|
|
|
Ok(Vc::cell(42))
|
|
}
|
|
|
|
#[turbo_tasks::function]
|
|
async fn compute(
|
|
input: ResolvedVc<ChangingInput>,
|
|
input2: ResolvedVc<ChangingInput>,
|
|
innerness: u32,
|
|
) -> Result<Vc<Output>> {
|
|
println!("compute({innerness})");
|
|
if innerness > 0 {
|
|
return Ok(compute(*input, *input2, innerness - 1));
|
|
}
|
|
let operation = inner_compute(input, input2);
|
|
let value = *operation.connect().await?;
|
|
let collectibles = operation.peek_collectibles::<Box<dyn ValueToString>>();
|
|
if collectibles.len() != 1 {
|
|
bail!("expected 1 collectible, found {}", collectibles.len());
|
|
}
|
|
let first = *collectibles.iter().next().unwrap();
|
|
let collectible = first.to_string().await?;
|
|
Ok(Output {
|
|
value,
|
|
collectible: collectible.to_string(),
|
|
}
|
|
.cell())
|
|
}
|