Unverified Commit c15f0249 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Clean up `VcRead::Repr` associated type (#88456)

It used to be that a cell could contain a transmuted representation. This was a hack for the previous limited generic support that I removed in https://github.com/vercel/next.js/pull/70817.

Clean this up! `Repr` and `T` are always the same type!
parent 8fe154f4
...@@ -59,7 +59,7 @@ pub fn primitive(input: TokenStream) -> TokenStream { ...@@ -59,7 +59,7 @@ pub fn primitive(input: TokenStream) -> TokenStream {
quote! { #ty }, quote! { #ty },
None, None,
quote! { quote! {
turbo_tasks::VcTransparentRead<#ty, #ty, #ty> turbo_tasks::VcTransparentRead<#ty, #ty>
}, },
quote! { quote! {
turbo_tasks::VcCellCompareMode<#ty> turbo_tasks::VcCellCompareMode<#ty>
......
...@@ -256,7 +256,7 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -256,7 +256,7 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
content.0 content.0
}, },
quote! { quote! {
turbo_tasks::VcTransparentRead::<#ident, #inner_type, #ident> turbo_tasks::VcTransparentRead::<#ident, #inner_type>
}, },
) )
} else { } else {
......
...@@ -26,11 +26,10 @@ use turbo_rcstr::RcStr; ...@@ -26,11 +26,10 @@ use turbo_rcstr::RcStr;
use crate::{ use crate::{
RawVc, ReadCellOptions, ReadOutputOptions, ReadRef, SharedReference, TaskId, TaskIdSet, RawVc, ReadCellOptions, ReadOutputOptions, ReadRef, SharedReference, TaskId, TaskIdSet,
TraitRef, TraitTypeId, TurboTasksPanic, ValueTypeId, VcRead, VcValueTrait, VcValueType, TraitRef, TraitTypeId, TurboTasksPanic, ValueTypeId, VcValueTrait, VcValueType,
event::EventListener, macro_helpers::NativeFunction, magic_any::MagicAny, event::EventListener, macro_helpers::NativeFunction, magic_any::MagicAny,
manager::TurboTasksBackendApi, raw_vc::CellId, registry, manager::TurboTasksBackendApi, raw_vc::CellId, registry,
task::shared_reference::TypedSharedReference, task_statistics::TaskStatisticsApi, task::shared_reference::TypedSharedReference, task_statistics::TaskStatisticsApi,
triomphe_utils::unchecked_sidecast_triomphe_arc,
}; };
pub type TransientTaskRoot = pub type TransientTaskRoot =
...@@ -162,11 +161,8 @@ impl TypedCellContent { ...@@ -162,11 +161,8 @@ impl TypedCellContent {
pub fn cast<T: VcValueType>(self) -> Result<ReadRef<T>> { pub fn cast<T: VcValueType>(self) -> Result<ReadRef<T>> {
let data = self.1.0.ok_or_else(|| anyhow!("Cell is empty"))?; let data = self.1.0.ok_or_else(|| anyhow!("Cell is empty"))?;
let data = data let data = data
.downcast::<<T::Read as VcRead<T>>::Repr>() .downcast::<T>()
.map_err(|_err| anyhow!("Unexpected type in cell"))?; .map_err(|_err| anyhow!("Unexpected type in cell"))?;
// SAFETY: `T` and `T::Read::Repr` must have equivalent memory representations,
// guaranteed by the unsafe implementation of `VcValueType`.
let data = unsafe { unchecked_sidecast_triomphe_arc(data) };
Ok(ReadRef::new_arc(data)) Ok(ReadRef::new_arc(data))
} }
......
...@@ -1786,7 +1786,6 @@ pub struct CurrentCellRef { ...@@ -1786,7 +1786,6 @@ pub struct CurrentCellRef {
is_serializable_cell_content: bool, is_serializable_cell_content: bool,
} }
type VcReadRepr<T> = <<T as VcValueType>::Read as VcRead<T>>::Repr;
type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target; type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target;
impl CurrentCellRef { impl CurrentCellRef {
...@@ -1798,14 +1797,10 @@ impl CurrentCellRef { ...@@ -1798,14 +1797,10 @@ impl CurrentCellRef {
T: VcValueType, T: VcValueType,
{ {
self.conditional_update_with_shared_reference(|old_shared_reference| { self.conditional_update_with_shared_reference(|old_shared_reference| {
let old_ref = old_shared_reference let old_ref = old_shared_reference.and_then(|sr| sr.0.downcast_ref::<T>());
.and_then(|sr| sr.0.downcast_ref::<VcReadRepr<T>>())
.map(|content| <T::Read as VcRead<T>>::repr_to_value_ref(content));
let (new_value, updated_key_hashes) = functor(old_ref)?; let (new_value, updated_key_hashes) = functor(old_ref)?;
Some(( Some((
SharedReference::new(triomphe::Arc::new(<T::Read as VcRead<T>>::value_to_repr( SharedReference::new(triomphe::Arc::new(new_value)),
new_value,
))),
updated_key_hashes, updated_key_hashes,
)) ))
}) })
...@@ -1891,22 +1886,21 @@ impl CurrentCellRef { ...@@ -1891,22 +1886,21 @@ impl CurrentCellRef {
}); });
} }
/// Replace the current cell's content with `new_shared_reference` if the /// Replace the current cell's content with `new_shared_reference` if the current content is not
/// current content is not equal by value with the existing content. /// equal by value with the existing content.
/// ///
/// If you already have a `SharedReference`, this is a faster version of /// If you already have a `SharedReference`, this is a faster version of
/// [`CurrentCellRef::compare_and_update`]. /// [`CurrentCellRef::compare_and_update`].
/// ///
/// The [`SharedReference`] is expected to use the `<T::Read as /// The value should be stored in [`SharedReference`] using the type `T`.
/// VcRead<T>>::Repr` type for its representation of the value.
pub fn compare_and_update_with_shared_reference<T>(&self, new_shared_reference: SharedReference) pub fn compare_and_update_with_shared_reference<T>(&self, new_shared_reference: SharedReference)
where where
T: VcValueType + PartialEq, T: VcValueType + PartialEq,
{ {
self.conditional_update_with_shared_reference(|old_sr| { self.conditional_update_with_shared_reference(|old_sr| {
if let Some(old_sr) = old_sr { if let Some(old_sr) = old_sr {
let old_value = extract_sr_value::<T>(old_sr)?; let old_value = extract_sr_value::<T>(old_sr);
let new_value = extract_sr_value::<T>(&new_shared_reference)?; let new_value = extract_sr_value::<T>(&new_shared_reference);
if old_value == new_value { if old_value == new_value {
return None; return None;
} }
...@@ -1955,9 +1949,9 @@ impl CurrentCellRef { ...@@ -1955,9 +1949,9 @@ impl CurrentCellRef {
let Some(old_sr) = old_sr else { let Some(old_sr) = old_sr else {
return Some((new_shared_reference, None)); return Some((new_shared_reference, None));
}; };
let old_value = extract_sr_value::<T>(old_sr)?; let old_value = extract_sr_value::<T>(old_sr);
let old_value = <T as VcValueType>::Read::value_to_target_ref(old_value); let old_value = <T as VcValueType>::Read::value_to_target_ref(old_value);
let new_value = extract_sr_value::<T>(&new_shared_reference)?; let new_value = extract_sr_value::<T>(&new_shared_reference);
let new_value = <T as VcValueType>::Read::value_to_target_ref(new_value); let new_value = <T as VcValueType>::Read::value_to_target_ref(new_value);
let updated_keys = old_value.different_keys(new_value); let updated_keys = old_value.different_keys(new_value);
if updated_keys.is_empty() { if updated_keys.is_empty() {
...@@ -1982,9 +1976,7 @@ impl CurrentCellRef { ...@@ -1982,9 +1976,7 @@ impl CurrentCellRef {
self.current_task, self.current_task,
self.index, self.index,
self.is_serializable_cell_content, self.is_serializable_cell_content,
CellContent(Some(SharedReference::new(triomphe::Arc::new( CellContent(Some(SharedReference::new(triomphe::Arc::new(new_value)))),
<T::Read as VcRead<T>>::value_to_repr(new_value),
)))),
None, None,
verification_mode, verification_mode,
) )
...@@ -1996,8 +1988,7 @@ impl CurrentCellRef { ...@@ -1996,8 +1988,7 @@ impl CurrentCellRef {
/// If the passed-in [`SharedReference`] is the same as the existing cell's /// If the passed-in [`SharedReference`] is the same as the existing cell's
/// by identity, no update is performed. /// by identity, no update is performed.
/// ///
/// The [`SharedReference`] is expected to use the `<T::Read as /// The value should be stored in [`SharedReference`] using the type `T`.
/// VcRead<T>>::Repr` type for its representation of the value.
pub fn update_with_shared_reference( pub fn update_with_shared_reference(
&self, &self,
shared_ref: SharedReference, shared_ref: SharedReference,
...@@ -2045,9 +2036,9 @@ impl From<CurrentCellRef> for RawVc { ...@@ -2045,9 +2036,9 @@ impl From<CurrentCellRef> for RawVc {
} }
} }
fn extract_sr_value<T: VcValueType>(sr: &SharedReference) -> Option<&T> { fn extract_sr_value<T: VcValueType>(sr: &SharedReference) -> &T {
sr.0.downcast_ref::<VcReadRepr<T>>() sr.0.downcast_ref::<T>()
.map(<T::Read as VcRead<T>>::repr_to_value_ref) .expect("cannot update SharedReference of different type")
} }
pub fn find_cell_by_type<T: VcValueType>() -> CurrentCellRef { pub fn find_cell_by_type<T: VcValueType>() -> CurrentCellRef {
......
...@@ -21,7 +21,6 @@ use crate::{ ...@@ -21,7 +21,6 @@ use crate::{
SharedReference, Vc, VcRead, VcValueType, SharedReference, Vc, VcRead, VcValueType,
debug::{ValueDebugFormat, ValueDebugFormatString}, debug::{ValueDebugFormat, ValueDebugFormatString},
trace::{TraceRawVcs, TraceRawVcsContext}, trace::{TraceRawVcs, TraceRawVcsContext},
triomphe_utils::unchecked_sidecast_triomphe_arc,
vc::VcCellMode, vc::VcCellMode,
}; };
...@@ -277,14 +276,9 @@ where ...@@ -277,14 +276,9 @@ where
/// reference. /// reference.
pub fn cell(read_ref: ReadRef<T>) -> Vc<T> { pub fn cell(read_ref: ReadRef<T>) -> Vc<T> {
let type_id = T::get_value_type_id(); let type_id = T::get_value_type_id();
// SAFETY: `T` and `T::Read::Repr` must have equivalent memory representations,
// guaranteed by the unsafe implementation of `VcValueType`.
let value = unsafe {
unchecked_sidecast_triomphe_arc::<T, <T::Read as VcRead<T>>::Repr>(read_ref.0)
};
Vc { Vc {
node: <T::CellMode as VcCellMode<T>>::raw_cell( node: <T::CellMode as VcCellMode<T>>::raw_cell(
SharedReference::new(value).into_typed(type_id), SharedReference::new(read_ref.0).into_typed(type_id),
), ),
_t: PhantomData, _t: PhantomData,
} }
......
...@@ -7,7 +7,6 @@ use crate::{ ...@@ -7,7 +7,6 @@ use crate::{
}; };
type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target; type VcReadTarget<T> = <<T as VcValueType>::Read as VcRead<T>>::Target;
type VcReadRepr<T> = <<T as VcValueType>::Read as VcRead<T>>::Repr;
/// Trait that controls the behavior of [`Vc::cell`] based on the value type's /// Trait that controls the behavior of [`Vc::cell`] based on the value type's
/// [`VcValueType::CellMode`]. /// [`VcValueType::CellMode`].
...@@ -51,7 +50,7 @@ where ...@@ -51,7 +50,7 @@ where
} }
fn raw_cell(content: TypedSharedReference) -> RawVc { fn raw_cell(content: TypedSharedReference) -> RawVc {
debug_assert_repr::<T>(&content); debug_assert_type::<T>(&content);
let cell = find_cell_by_type::<T>(); let cell = find_cell_by_type::<T>();
cell.update_with_shared_reference(content.reference, VerificationMode::Skip); cell.update_with_shared_reference(content.reference, VerificationMode::Skip);
cell.into() cell.into()
...@@ -78,7 +77,7 @@ where ...@@ -78,7 +77,7 @@ where
} }
fn raw_cell(content: TypedSharedReference) -> RawVc { fn raw_cell(content: TypedSharedReference) -> RawVc {
debug_assert_repr::<T>(&content); debug_assert_type::<T>(&content);
let cell = find_cell_by_type::<T>(); let cell = find_cell_by_type::<T>();
cell.compare_and_update_with_shared_reference::<T>(content.reference); cell.compare_and_update_with_shared_reference::<T>(content.reference);
cell.into() cell.into()
...@@ -107,18 +106,17 @@ where ...@@ -107,18 +106,17 @@ where
} }
fn raw_cell(content: TypedSharedReference) -> RawVc { fn raw_cell(content: TypedSharedReference) -> RawVc {
debug_assert_repr::<T>(&content); debug_assert_type::<T>(&content);
let cell = find_cell_by_type::<T>(); let cell = find_cell_by_type::<T>();
cell.keyed_compare_and_update_with_shared_reference::<T>(content.reference); cell.keyed_compare_and_update_with_shared_reference::<T>(content.reference);
cell.into() cell.into()
} }
} }
fn debug_assert_repr<T: VcValueType>(content: &TypedSharedReference) { fn debug_assert_type<T: VcValueType>(content: &TypedSharedReference) {
debug_assert!( debug_assert!(
(*content.reference.0).is::<VcReadRepr<T>>(), (*content.reference.0).is::<T>(),
"SharedReference for type {} must use representation type {}", "SharedReference for type {} must contain data matching that type",
type_name::<T>(), type_name::<T>(),
type_name::<VcReadRepr<T>>(),
); );
} }
...@@ -378,11 +378,10 @@ where ...@@ -378,11 +378,10 @@ where
} }
} }
impl<T, Inner, Repr> Vc<T> impl<T, Inner> Vc<T>
where where
T: VcValueType<Read = VcTransparentRead<T, Inner, Repr>>, T: VcValueType<Read = VcTransparentRead<T, Inner>>,
Inner: Any + Send + Sync, Inner: Any + Send + Sync,
Repr: VcValueType,
{ {
pub fn cell(inner: Inner) -> Self { pub fn cell(inner: Inner) -> Self {
Self::cell_private(inner) Self::cell_private(inner)
......
...@@ -39,26 +39,12 @@ where ...@@ -39,26 +39,12 @@ where
/// target will be different than the value type. /// target will be different than the value type.
type Target; type Target;
/// The representation type. This is what will be used to
/// serialize/deserialize the value, and this determines the
/// type that the value will be upcasted to for storage.
///
/// For instance, when storing generic collection types such as
/// `Vec<Vc<ValueType>>`, we first cast them to a shared `Vec<Vc<()>>`
/// type instead, which has an equivalent memory representation to any
/// `Vec<Vc<T>>` type. This allows sharing implementations of methods and
/// traits between all `Vec<Vc<T>>`.
type Repr: VcValueType;
/// Convert a reference to a value to a reference to the target type. /// Convert a reference to a value to a reference to the target type.
fn value_to_target_ref(value: &T) -> &Self::Target; fn value_to_target_ref(value: &T) -> &Self::Target;
/// Convert a value to the target type. /// Convert a value to the target type.
fn value_to_target(value: T) -> Self::Target; fn value_to_target(value: T) -> Self::Target;
/// Convert the value type to the repr.
fn value_to_repr(value: T) -> Self::Repr;
/// Convert the target type to the value. /// Convert the target type to the value.
fn target_to_value(target: Self::Target) -> T; fn target_to_value(target: Self::Target) -> T;
...@@ -67,12 +53,6 @@ where ...@@ -67,12 +53,6 @@ where
/// Convert a mutable reference to a target type to a reference to a value. /// Convert a mutable reference to a target type to a reference to a value.
fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T; fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T;
/// Convert the target type to the repr.
fn target_to_repr(target: Self::Target) -> Self::Repr;
/// Convert a reference to a repr type to a reference to a value.
fn repr_to_value_ref(repr: &Self::Repr) -> &T;
} }
/// Representation for standard `#[turbo_tasks::value]`, where a read return a /// Representation for standard `#[turbo_tasks::value]`, where a read return a
...@@ -86,7 +66,6 @@ where ...@@ -86,7 +66,6 @@ where
T: VcValueType, T: VcValueType,
{ {
type Target = T; type Target = T;
type Repr = T;
fn value_to_target_ref(value: &T) -> &Self::Target { fn value_to_target_ref(value: &T) -> &Self::Target {
value value
...@@ -96,10 +75,6 @@ where ...@@ -96,10 +75,6 @@ where
value value
} }
fn value_to_repr(value: T) -> Self::Repr {
value
}
fn target_to_value(target: Self::Target) -> T { fn target_to_value(target: Self::Target) -> T {
target target
} }
...@@ -111,30 +86,20 @@ where ...@@ -111,30 +86,20 @@ where
fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T { fn target_to_value_mut_ref(target: &mut Self::Target) -> &mut T {
target target
} }
fn target_to_repr(target: Self::Target) -> Self::Repr {
target
}
fn repr_to_value_ref(repr: &Self::Repr) -> &T {
repr
}
} }
/// Representation for `#[turbo_tasks::value(transparent)]` types, where reads /// Representation for `#[turbo_tasks::value(transparent)]` types, where reads
/// return a reference to the target type. /// return a reference to the target type.
pub struct VcTransparentRead<T, Target, Repr> { pub struct VcTransparentRead<T, Target> {
_phantom: PhantomData<(T, Target, Repr)>, _phantom: PhantomData<(T, Target)>,
} }
impl<T, Target, Repr> VcRead<T> for VcTransparentRead<T, Target, Repr> impl<T, Target> VcRead<T> for VcTransparentRead<T, Target>
where where
T: VcValueType, T: VcValueType,
Target: Any + Send + Sync, Target: Any + Send + Sync,
Repr: VcValueType,
{ {
type Target = Target; type Target = Target;
type Repr = Repr;
fn value_to_target_ref(value: &T) -> &Self::Target { fn value_to_target_ref(value: &T) -> &Self::Target {
// Safety: the `VcValueType` implementor must guarantee that both `T` and // Safety: the `VcValueType` implementor must guarantee that both `T` and
...@@ -154,13 +119,6 @@ where ...@@ -154,13 +119,6 @@ where
} }
} }
fn value_to_repr(value: T) -> Self::Repr {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<T>, Self::Repr>(&ManuallyDrop::new(value))
}
}
fn target_to_value(target: Self::Target) -> T { fn target_to_value(target: Self::Target) -> T {
// Safety: see `Self::value_to_target_ref` above. // Safety: see `Self::value_to_target_ref` above.
unsafe { unsafe {
...@@ -183,22 +141,6 @@ where ...@@ -183,22 +141,6 @@ where
)) ))
} }
} }
fn target_to_repr(target: Self::Target) -> Self::Repr {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<Self::Target>, Self::Repr>(&ManuallyDrop::new(
target,
))
}
}
fn repr_to_value_ref(repr: &Self::Repr) -> &T {
// Safety: see `Self::value_to_target_ref` above.
unsafe {
std::mem::transmute_copy::<ManuallyDrop<&Self::Repr>, &T>(&ManuallyDrop::new(repr))
}
}
} }
pub struct ReadVcFuture<T, Cast = VcValueTypeCast<T>> pub struct ReadVcFuture<T, Cast = VcValueTypeCast<T>>
......
...@@ -135,11 +135,10 @@ where ...@@ -135,11 +135,10 @@ where
} }
} }
impl<T, Inner, Repr> Default for ResolvedVc<T> impl<T, Inner> Default for ResolvedVc<T>
where where
T: VcValueType<Read = VcTransparentRead<T, Inner, Repr>>, T: VcValueType<Read = VcTransparentRead<T, Inner>>,
Inner: Any + Send + Sync + Default, Inner: Any + Send + Sync + Default,
Repr: VcValueType,
{ {
fn default() -> Self { fn default() -> Self {
Self::cell(Default::default()) Self::cell(Default::default())
...@@ -178,11 +177,10 @@ where ...@@ -178,11 +177,10 @@ where
} }
} }
impl<T, Inner, Repr> ResolvedVc<T> impl<T, Inner> ResolvedVc<T>
where where
T: VcValueType<Read = VcTransparentRead<T, Inner, Repr>>, T: VcValueType<Read = VcTransparentRead<T, Inner>>,
Inner: Any + Send + Sync, Inner: Any + Send + Sync,
Repr: VcValueType,
{ {
pub fn cell(inner: Inner) -> Self { pub fn cell(inner: Inner) -> Self {
Self { Self {
......
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