Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion turbopack/crates/turbo-tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ erased-serde = { workspace = true }
event-listener = "5.4.0"
futures = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
mopa = "0.2.0"
once_cell = { workspace = true }
parking_lot = { workspace = true, features = ["serde"]}
pin-project-lite = { workspace = true }
Expand Down
69 changes: 10 additions & 59 deletions turbopack/crates/turbo-tasks/src/magic_any.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,43 @@
use core::fmt;
use std::{
any::{Any, TypeId},
fmt::Debug,
hash::Hash,
sync::Arc,
};
use std::{any::Any, fmt::Debug, hash::Hash};

use serde::{Deserialize, Serialize, de::DeserializeSeed};
use turbo_dyn_eq_hash::{
DynEq, DynHash, DynPartialEq, impl_eq_for_dyn, impl_hash_for_dyn, impl_partial_eq_for_dyn,
DynEq, DynHash, impl_eq_for_dyn, impl_hash_for_dyn, impl_partial_eq_for_dyn,
};

use crate::trace::{TraceRawVcs, TraceRawVcsContext};

pub trait MagicAny: mopa::Any + DynPartialEq + DynEq + DynHash + Send + Sync {
fn magic_any_arc(self: Arc<Self>) -> Arc<dyn Any + Sync + Send>;

fn magic_debug(&self, f: &mut fmt::Formatter) -> fmt::Result;

fn magic_trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext);
use crate::trace::TraceRawVcs;

pub trait MagicAny: Debug + DynEq + DynHash + TraceRawVcs + Send + Sync + 'static {
#[cfg(debug_assertions)]
fn magic_type_name(&self) -> &'static str;
}

#[allow(clippy::transmute_ptr_to_ref)] // can't fix as it's in the macro
mod clippy {
use mopa::mopafy;

use super::MagicAny;

mopafy!(MagicAny);
}

impl<T: Debug + Eq + Hash + Send + Sync + TraceRawVcs + 'static> MagicAny for T {
fn magic_any_arc(self: Arc<Self>) -> Arc<dyn Any + Sync + Send> {
self
}

fn magic_debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut d = f.debug_tuple("MagicAny");
d.field(&TypeId::of::<Self>());

#[cfg(debug_assertions)]
d.field(&std::any::type_name::<Self>());

d.field(&(self as &Self));
d.finish()
}

fn magic_trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
self.trace_raw_vcs(trace_context);
}

impl<T> MagicAny for T
where
T: Debug + Eq + Hash + Send + Sync + TraceRawVcs + 'static,
{
#[cfg(debug_assertions)]
fn magic_type_name(&self) -> &'static str {
std::any::type_name::<T>()
}
}

impl fmt::Debug for dyn MagicAny {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.magic_debug(f)
}
}

impl_partial_eq_for_dyn!(dyn MagicAny);
impl_eq_for_dyn!(dyn MagicAny);
impl_hash_for_dyn!(dyn MagicAny);

impl TraceRawVcs for dyn MagicAny {
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
self.magic_trace_raw_vcs(trace_context)
}
}

impl dyn MagicAny {
pub fn as_serialize<T: Debug + Eq + Hash + Serialize + Send + Sync + TraceRawVcs + 'static>(
&self,
) -> &dyn erased_serde::Serialize {
if let Some(r) = self.downcast_ref::<T>() {
if let Some(r) = (self as &dyn Any).downcast_ref::<T>() {
r
} else {
#[cfg(debug_assertions)]
panic!(
"MagicAny::as_serializable broken: got {} but expected {}",
self.magic_type_name(),
std::any::type_name::<T>()
std::any::type_name::<T>(),
);
#[cfg(not(debug_assertions))]
panic!("MagicAny::as_serializable bug");
Expand Down
28 changes: 16 additions & 12 deletions turbopack/crates/turbo-tasks/src/native_function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, hash::Hash, pin::Pin};
use std::{any::Any, fmt::Debug, hash::Hash, pin::Pin};

use anyhow::Result;
use futures::Future;
Expand Down Expand Up @@ -105,32 +105,36 @@ fn resolve_functor_impl<T: MagicAny + TaskInput>(value: &dyn MagicAny) -> Resolv

#[cfg(debug_assertions)]
#[inline(never)]
pub fn debug_downcast_args_error_msg(expected: &str, actual: &dyn MagicAny) -> String {
format!(
"Invalid argument type, expected {expected} got {}",
(*actual).magic_type_name()
)
pub fn debug_downcast_args_error_msg(expected: &str, actual: &str) -> String {
format!("Invalid argument type, expected {expected} got {actual}")
}

pub fn downcast_args_owned<T: MagicAny>(args: Box<dyn MagicAny>) -> Box<T> {
#[allow(unused_variables)]
args.downcast::<T>()
.map_err(|args| {
#[cfg(debug_assertions)]
let args_type_name = args.magic_type_name();

(args as Box<dyn Any>)
.downcast::<T>()
.map_err(|_args| {
#[cfg(debug_assertions)]
return debug_downcast_args_error_msg(std::any::type_name::<T>(), &*args);
return anyhow::anyhow!(debug_downcast_args_error_msg(
std::any::type_name::<T>(),
args_type_name,
));
#[cfg(not(debug_assertions))]
return anyhow::anyhow!("Invalid argument type");
})
.unwrap()
}

pub fn downcast_args_ref<T: MagicAny>(args: &dyn MagicAny) -> &T {
args.downcast_ref::<T>()
(args as &dyn Any)
.downcast_ref::<T>()
.ok_or_else(|| {
#[cfg(debug_assertions)]
return anyhow::anyhow!(debug_downcast_args_error_msg(
std::any::type_name::<T>(),
args
args.magic_type_name(),
));
#[cfg(not(debug_assertions))]
return anyhow::anyhow!("Invalid argument type");
Expand Down
7 changes: 5 additions & 2 deletions turbopack/crates/turbo-tasks/src/task/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,13 @@ macro_rules! task_inputs_impl {
/// gives the compiler more chances to dedupe monomorphized code across small functions with less
/// typevars.
fn get_args<T: MagicAny + Clone>(arg: &dyn MagicAny) -> Result<T> {
let value = arg.downcast_ref::<T>().cloned();
let value = (arg as &dyn std::any::Any).downcast_ref::<T>().cloned();
#[cfg(debug_assertions)]
return anyhow::Context::with_context(value, || {
crate::native_function::debug_downcast_args_error_msg(std::any::type_name::<T>(), arg)
crate::native_function::debug_downcast_args_error_msg(
std::any::type_name::<T>(),
arg.magic_type_name(),
)
});
#[cfg(not(debug_assertions))]
return anyhow::Context::context(value, "Invalid argument type");
Expand Down
Loading