Unverified Commit 1095650d authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

chore(turbo-tasks): Delete vc generics support (#70817)

The previous PRs in this stack remove the only uses/callsites. This
removes support for vc generics!

There's still a small bit of support left in the form of
`VcValueType::Repr`, but that touches more things, so that'll be a
follow-up PR.

## Why?

Rather than extended support for `Vc` generics to `ResolvedVc`, I plan
to remove support for them entirely. That means fixing all the callsites
(there's not many).

Removing this is needed to get us to a point where 100% of structs can
be `ResolvedValue`, because structs using these fields cannot otherwise
be ported over to `ResolvedVc`.

## Okay, but Why?

Explained here:
https://github.com/vercel/turborepo/pull/8843#issuecomment-2253158412

I expect removing support for this will decrease the overall size of the
codebase, as the logic for supporting generics is rather complicated.
parent ed10d58e
../../turbo-tasks-testing/tests/generics.rs
\ No newline at end of file
../../turbo-tasks-testing/tests/generics.rs
\ No newline at end of file
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
use std::sync::{Arc, Mutex};
use indexmap::{IndexMap, IndexSet};
use turbo_tasks::{debug::ValueDebug, Invalidator, ReadRef, TaskId, Vc};
use turbo_tasks_testing::{register, run, Registration};
static REGISTRATION: Registration = register!();
#[tokio::test]
async fn test_option_some() {
run(&REGISTRATION, || async move {
let vc_42 = Vc::cell(42);
let option: Vc<Option<Vc<u32>>> = Vc::cell(Some(vc_42));
assert!(*option.is_some().await?);
assert!(!(*option.is_none().await?));
assert_eq!(&*option.await?, &Some(vc_42));
assert_eq!(option.dbg().await?.to_string(), "Some(\n 42,\n)");
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test]
async fn test_option_none() {
run(&REGISTRATION, || async move {
let option: Vc<Option<Vc<u32>>> = Default::default();
assert!(!(*option.is_some().await?));
assert!(*option.is_none().await?);
assert_eq!(&*option.await?, &None);
assert_eq!(option.dbg().await?.to_string(), "None");
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test]
async fn test_vec() {
run(&REGISTRATION, || async move {
let vc_42 = Vc::cell(42);
let vec: Vc<Vec<Vc<u32>>> = Vc::cell(vec![vc_42]);
assert_eq!(*vec.len().await?, 1);
assert!(!(*vec.is_empty().await?));
assert_eq!(&*vec.await?, &[vc_42]);
assert_eq!(vec.dbg().await?.to_string(), "[\n 42,\n]");
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test]
async fn test_empty_vec() {
run(&REGISTRATION, || async move {
let vec: Vc<Vec<Vc<u32>>> = Default::default();
assert_eq!(*vec.len().await?, 0);
assert!(*vec.is_empty().await?);
assert_eq!(vec.dbg().await?.to_string(), "[]");
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test]
async fn test_nested_empty_vec() {
run(&REGISTRATION, || async move {
let vec: Vc<Vec<Vc<Vec<Vc<u32>>>>> = Default::default();
assert_eq!(*vec.len().await?, 0);
assert_eq!(vec.dbg().await?.to_string(), "[]");
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test]
async fn test_index_set() {
run(&REGISTRATION, || async move {
let vc_42 = Vc::cell(42);
let set: Vc<IndexSet<Vc<u32>>> = Vc::cell(IndexSet::from([vc_42]));
assert_eq!(*set.len().await?, 1);
assert!(!(*set.is_empty().await?));
assert_eq!(&*set.await?, &IndexSet::from([vc_42]));
assert_eq!(set.dbg().await?.to_string(), "{\n 42,\n}");
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test]
async fn test_empty_index_set() {
run(&REGISTRATION, || async move {
let set: Vc<IndexSet<Vc<u32>>> = Default::default();
assert_eq!(*set.len().await?, 0);
assert!(*set.is_empty().await?);
assert_eq!(&*set.await?, &IndexSet::<Vc<u32>>::default());
assert_eq!(set.dbg().await?.to_string(), "{}");
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test]
async fn test_index_map() {
run(&REGISTRATION, || async move {
let vc_42 = Vc::cell(42);
let map: Vc<IndexMap<Vc<u32>, _>> = Vc::cell(IndexMap::from([(vc_42, vc_42)]));
assert_eq!(*map.len().await?, 1);
assert!(!(*map.is_empty().await?));
assert_eq!(&*map.await?, &IndexMap::from([(vc_42, vc_42)]));
assert_eq!(map.dbg().await?.to_string(), "{\n 42: 42,\n}");
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test]
async fn test_empty_index_map() {
run(&REGISTRATION, || async move {
let map: Vc<IndexMap<Vc<u32>, Vc<u32>>> = Default::default();
assert_eq!(*map.len().await?, 0);
assert!(*map.is_empty().await?);
assert_eq!(&*map.await?, &IndexMap::<Vc<u32>, Vc<u32>>::default());
assert_eq!(map.dbg().await?.to_string(), "{}");
anyhow::Ok(())
})
.await
.unwrap()
}
// Simulate a non-deterministic function that stores different generic types in
// it's cells each time it runs.
#[tokio::test]
async fn test_changing_generic() {
run(&REGISTRATION, || async move {
let state_vc = State::default().cell();
let state_ref = state_vc.await?;
for _i in 0..10 {
let _ = non_deterministic(state_vc)
.resolve_strongly_consistent()
.await
.unwrap();
state_ref
.inner
.lock()
.unwrap()
.last_invalidator
.take()
.unwrap()
.invalidate();
}
anyhow::Ok(())
})
.await
.unwrap()
}
// Test that we can convert a `Vc` to a `ReadRef`, and then back to a `Vc`.
#[tokio::test]
async fn test_read_ref_round_trip() {
run(&REGISTRATION, || async move {
let c: Vc<Option<Vc<u8>>> = Vc::cell(Some(Vc::cell(1)));
let _ = ReadRef::cell(c.await?).await?;
anyhow::Ok(())
})
.await
.unwrap()
}
#[turbo_tasks::value(eq = "manual")]
#[derive(Default)]
struct State {
#[turbo_tasks(debug_ignore, trace_ignore)]
#[serde(skip)]
inner: Arc<Mutex<StateInner>>,
}
#[derive(Default)]
struct StateInner {
branch: bool,
last_invalidator: Option<Invalidator>,
last_task_id: Option<TaskId>,
}
impl PartialEq for State {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self as *const _, other as *const _)
}
}
impl Eq for State {}
#[turbo_tasks::function]
async fn non_deterministic(state: Vc<State>) {
let state = state.await.unwrap();
let mut state_inner = state.inner.lock().unwrap();
let task_id = if state_inner.branch {
let c: Vc<Option<Vc<u8>>> = Vc::cell(Some(Vc::cell(1)));
println!("u8 branch");
Vc::into_raw(c).get_task_id()
} else {
let c: Vc<Option<Vc<u32>>> = Vc::cell(Some(Vc::cell(1)));
println!("u32 branch");
Vc::into_raw(c).get_task_id()
};
state_inner.branch = !state_inner.branch;
if let Some(last_task_id) = state_inner.last_task_id {
assert_eq!(last_task_id, task_id);
}
state_inner.last_task_id = Some(task_id);
state_inner.last_invalidator = Some(turbo_tasks::get_invalidator());
}
...@@ -33,26 +33,6 @@ async fn test_store_and_read() -> Result<()> { ...@@ -33,26 +33,6 @@ async fn test_store_and_read() -> Result<()> {
.await .await
} }
#[tokio::test]
async fn test_store_and_read_generic() -> Result<()> {
run(&REGISTRATION, || async {
// `Vc<Vec<Vc<T>>>` is stored as `Vc<Vec<Vc<()>>>` and requires special
// transmute handling
let cells: Vc<Vec<Vc<u32>>> =
Vc::local_cell(vec![Vc::local_cell(1), Vc::local_cell(2), Vc::cell(3)]);
let mut output = Vec::new();
for el in cells.await.unwrap() {
output.push(*el.await.unwrap());
}
assert_eq!(output, vec![1, 2, 3]);
Ok(())
})
.await
}
#[turbo_tasks::function(local_cells)] #[turbo_tasks::function(local_cells)]
async fn returns_resolved_local_vc() -> Vc<u32> { async fn returns_resolved_local_vc() -> Vc<u32> {
let cell = Vc::<u32>::cell(42); let cell = Vc::<u32>::cell(42);
......
use anyhow::Result;
use indexmap::IndexMap;
// This specific macro identifier is detected by turbo-tasks-build.
use turbo_tasks_macros::generic_type as __turbo_tasks_internal_generic_type;
use crate::{
self as turbo_tasks,
debug::{ValueDebug, ValueDebugFormat, ValueDebugString},
ValueDefault, Vc,
};
__turbo_tasks_internal_generic_type!(<K, V>, IndexMap<Vc<K>, Vc<V>>);
#[turbo_tasks::function]
async fn index_map_len(index_map: Vc<IndexMap<Vc<()>, Vc<()>>>) -> Result<Vc<usize>> {
let index_map = index_map.await?;
Ok(Vc::cell(index_map.len()))
}
#[turbo_tasks::function]
async fn index_map_is_empty(index_map: Vc<IndexMap<Vc<()>, Vc<()>>>) -> Result<Vc<bool>> {
let index_map = index_map.await?;
Ok(Vc::cell(index_map.is_empty()))
}
impl<K, V> Vc<IndexMap<Vc<K>, Vc<V>>>
where
K: Send,
V: Send,
{
/// See [`IndexMap::len`].
pub fn len(self) -> Vc<usize> {
index_map_len(Self::to_repr(self))
}
/// See [`IndexMap::is_empty`].
pub fn is_empty(self) -> Vc<bool> {
index_map_is_empty(Self::to_repr(self))
}
}
#[turbo_tasks::function]
fn index_map_default() -> Vc<IndexMap<Vc<()>, Vc<()>>> {
Vc::cell(Default::default())
}
impl<K, V> ValueDefault for IndexMap<Vc<K>, Vc<V>>
where
K: Send,
V: Send,
{
fn value_default() -> Vc<Self> {
// Safety: `index_map_default` creates an empty map, which is a valid
// representation of any index set of `Vc`.
unsafe { Vc::<Self>::from_repr(index_map_default()) }
}
}
#[turbo_tasks::function]
async fn index_map_dbg_depth(
index_map: Vc<IndexMap<Vc<()>, Vc<()>>>,
depth: usize,
) -> Result<Vc<ValueDebugString>> {
index_map
.await?
.value_debug_format(depth)
.try_to_value_debug_string()
.await
}
impl<K, V> ValueDebug for IndexMap<Vc<K>, Vc<V>>
where
K: Send,
V: Send,
{
fn dbg(self: Vc<Self>) -> Vc<ValueDebugString> {
index_map_dbg_depth(Vc::<Self>::to_repr(self), usize::MAX)
}
fn dbg_depth(self: Vc<Self>, depth: usize) -> Vc<ValueDebugString> {
index_map_dbg_depth(Vc::<Self>::to_repr(self), depth)
}
}
use anyhow::Result;
use indexmap::IndexSet;
// This specific macro identifier is detected by turbo-tasks-build.
use turbo_tasks_macros::generic_type as __turbo_tasks_internal_generic_type;
use crate::{
self as turbo_tasks,
debug::{ValueDebug, ValueDebugFormat, ValueDebugString},
ValueDefault, Vc,
};
__turbo_tasks_internal_generic_type!(<T>, IndexSet<Vc<T>>);
#[turbo_tasks::function]
async fn index_set_len(index_set: Vc<IndexSet<Vc<()>>>) -> Result<Vc<usize>> {
let index_set = index_set.await?;
Ok(Vc::cell(index_set.len()))
}
#[turbo_tasks::function]
async fn index_set_is_empty(index_set: Vc<IndexSet<Vc<()>>>) -> Result<Vc<bool>> {
let index_set = index_set.await?;
Ok(Vc::cell(index_set.is_empty()))
}
impl<T> Vc<IndexSet<Vc<T>>>
where
T: Send,
{
/// See [`IndexSet::len`].
pub fn len(self) -> Vc<usize> {
index_set_len(Self::to_repr(self))
}
/// See [`IndexSet::is_empty`].
pub fn is_empty(self) -> Vc<bool> {
index_set_is_empty(Self::to_repr(self))
}
}
#[turbo_tasks::function]
fn index_set_default() -> Vc<IndexSet<Vc<()>>> {
Vc::cell(Default::default())
}
impl<T> ValueDefault for IndexSet<Vc<T>>
where
T: Send,
{
fn value_default() -> Vc<Self> {
// Safety: `index_set_default` creates an empty set, which is a valid
// representation of any index set of `Vc`.
unsafe { Vc::<Self>::from_repr(index_set_default()) }
}
}
#[turbo_tasks::function]
async fn index_set_dbg_depth(
index_set: Vc<IndexSet<Vc<()>>>,
depth: usize,
) -> Result<Vc<ValueDebugString>> {
index_set
.await?
.value_debug_format(depth)
.try_to_value_debug_string()
.await
}
impl<T> ValueDebug for IndexSet<Vc<T>>
where
T: Send,
{
fn dbg(self: Vc<Self>) -> Vc<ValueDebugString> {
index_set_dbg_depth(Vc::<Self>::to_repr(self), usize::MAX)
}
fn dbg_depth(self: Vc<Self>, depth: usize) -> Vc<ValueDebugString> {
index_set_dbg_depth(Vc::<Self>::to_repr(self), depth)
}
}
pub(crate) mod index_map;
pub(crate) mod index_set;
pub(crate) mod option;
pub(crate) mod vec;
use anyhow::Result;
// This specific macro identifier is detected by turbo-tasks-build.
use turbo_tasks_macros::generic_type as __turbo_tasks_internal_generic_type;
use crate::{
self as turbo_tasks,
debug::{ValueDebug, ValueDebugFormat, ValueDebugString},
ValueDefault, Vc,
};
__turbo_tasks_internal_generic_type!(<T>, Option<Vc<T>>);
#[turbo_tasks::function]
async fn option_is_none(option: Vc<Option<Vc<()>>>) -> Result<Vc<bool>> {
let option = option.await?;
Ok(Vc::cell(option.is_none()))
}
#[turbo_tasks::function]
async fn option_is_some(option: Vc<Option<Vc<()>>>) -> Result<Vc<bool>> {
let option = option.await?;
Ok(Vc::cell(option.is_some()))
}
impl<T> Vc<Option<Vc<T>>>
where
T: Send,
{
/// See [`Option::is_none`].
pub fn is_none(self) -> Vc<bool> {
option_is_none(Self::to_repr(self))
}
/// See [`Option::is_some`].
pub fn is_some(self) -> Vc<bool> {
option_is_some(Self::to_repr(self))
}
}
#[turbo_tasks::function]
fn option_default() -> Vc<Option<Vc<()>>> {
Vc::cell(Default::default())
}
impl<T> ValueDefault for Option<Vc<T>>
where
T: Send,
{
fn value_default() -> Vc<Self> {
// Safety: `option_default` creates a None variant, which is a valid
// representation of any option of `Vc`.
unsafe { Vc::<Self>::from_repr(option_default()) }
}
}
#[turbo_tasks::function]
async fn option_dbg_depth(
option: Vc<Option<Vc<()>>>,
depth: usize,
) -> Result<Vc<ValueDebugString>> {
option
.await?
.value_debug_format(depth)
.try_to_value_debug_string()
.await
}
impl<T> ValueDebug for Option<Vc<T>>
where
T: Send,
{
fn dbg(self: Vc<Self>) -> Vc<ValueDebugString> {
option_dbg_depth(Vc::<Self>::to_repr(self), usize::MAX)
}
fn dbg_depth(self: Vc<Self>, depth: usize) -> Vc<ValueDebugString> {
option_dbg_depth(Vc::<Self>::to_repr(self), depth)
}
}
use anyhow::Result;
// This specific macro identifier is detected by turbo-tasks-build.
use turbo_tasks_macros::generic_type as __turbo_tasks_internal_generic_type;
use crate::{
self as turbo_tasks,
debug::{ValueDebug, ValueDebugFormat, ValueDebugString},
ValueDefault, Vc,
};
__turbo_tasks_internal_generic_type!(<T>, Vec<Vc<T>>);
#[turbo_tasks::function]
async fn vec_len(vec: Vc<Vec<Vc<()>>>) -> Result<Vc<usize>> {
let vec = vec.await?;
Ok(Vc::cell(vec.len()))
}
#[turbo_tasks::function]
async fn vec_is_empty(vec: Vc<Vec<Vc<()>>>) -> Result<Vc<bool>> {
let vec = vec.await?;
Ok(Vc::cell(vec.is_empty()))
}
impl<T> Vc<Vec<Vc<T>>>
where
T: Send,
{
/// See [`Vec::len`].
pub fn len(self) -> Vc<usize> {
vec_len(Self::to_repr(self))
}
/// See [`Vec::is_empty`].
pub fn is_empty(self) -> Vc<bool> {
vec_is_empty(Self::to_repr(self))
}
}
#[turbo_tasks::function]
fn vec_default() -> Vc<Vec<Vc<()>>> {
Vc::cell(Default::default())
}
impl<T> ValueDefault for Vec<Vc<T>>
where
T: Send,
{
fn value_default() -> Vc<Self> {
// Safety: `vec_default` creates an empty vector, which is a valid
// representation of any vector of `Vc`s.
unsafe { Vc::<Self>::from_repr(vec_default()) }
}
}
#[turbo_tasks::function]
async fn vec_dbg_depth(vec: Vc<Vec<Vc<()>>>, depth: usize) -> Result<Vc<ValueDebugString>> {
vec.await?
.value_debug_format(depth)
.try_to_value_debug_string()
.await
}
impl<T> ValueDebug for Vec<Vc<T>>
where
T: Send,
{
fn dbg(self: Vc<Self>) -> Vc<ValueDebugString> {
vec_dbg_depth(Vc::<Self>::to_repr(self), usize::MAX)
}
fn dbg_depth(self: Vc<Self>, depth: usize) -> Vc<ValueDebugString> {
vec_dbg_depth(Vc::<Self>::to_repr(self), depth)
}
}
...@@ -44,7 +44,6 @@ pub mod debug; ...@@ -44,7 +44,6 @@ pub mod debug;
mod display; mod display;
pub mod duration_span; pub mod duration_span;
pub mod event; pub mod event;
mod generics;
pub mod graph; pub mod graph;
mod id; mod id;
mod id_factory; mod id_factory;
......
...@@ -323,18 +323,6 @@ where ...@@ -323,18 +323,6 @@ where
vc.node vc.node
} }
/// Creates a `Vc` from a `RawVc`.
///
/// # Safety
///
/// The caller must ensure that `RawVc` points to a value of type `T`.
pub(crate) unsafe fn from_raw(vc: RawVc) -> Self {
Vc {
node: vc,
_t: std::marker::PhantomData,
}
}
/// Upcasts the given `Vc<T>` to a `Vc<Box<dyn K>>`. /// Upcasts the given `Vc<T>` to a `Vc<Box<dyn K>>`.
/// ///
/// This is also available as an `Into`/`From` conversion. /// This is also available as an `Into`/`From` conversion.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment