Skip to content

Commit

Permalink
More robust vdbg! macro (vercel/turborepo#4995)
Browse files Browse the repository at this point in the history
### Description

I ran into a couple of issues with the vdbg! macro:
* The macro tries to move its parameters within an async block, which
requires everything that's moved to be both `Copy` and `'static` to work
properly. This would break when trying to pass `&obj.property`, where
the reference doesn't have a `'static` lifetime. Instead, I'm adding the
requirement on parameters to impl `ToOwned` so we can ensure the
`'static` lifetime, and creating an owned version of the parameter to
move into the future.
* The `CELL_COUNTERS` thread local could be unset when calling
`.value_debug_format().try_to_string()`, which would panic the thread.
  • Loading branch information
alexkirsz authored May 22, 2023
1 parent 61556d5 commit 339d9fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
26 changes: 19 additions & 7 deletions crates/turbo-tasks/src/debug/vdbg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,34 @@ macro_rules! vdbg {
eprintln!("[{}:{}]", file!(), line!())
};

(__init $depth:expr ; $($val:expr),* ) => {
(__init $depth:expr ; [ $val:expr $(, $rest:expr)* ] [ $($tt:tt)* ] ) => {
{
let valstr = stringify!($val);
// We move the value into a new binding so we may refer to it multiple
// times without re-evaluating the expression.
let valmove = $val;
// We convert the value to an owned value which will be moved into the
// spawned thread. This is necessary in order to ensure a 'static lifetime
// for the value, but it may require a clone.
let valowned = valmove.to_owned();
$crate::vdbg!(__init $depth ; [ $($rest),* ] [ $($tt)* valstr valmove valowned ])
}
};
(__init $depth:expr ; [ ] [ $($valstr:ident $valmove:ident $valowned:ident)* ] ) => {
{
use $crate::debug::ValueDebugFormat;
let depth = $depth;
$crate::macro_helpers::spawn_detached(async move {
$crate::vdbg!(__expand depth ; [ $($val),* ] []);
$crate::vdbg!(__expand depth ; [ $($valstr $valowned)* ] []);
Ok(())
});
($($val),*)
($($valmove),*)
}
};

(__expand $depth:ident ; [ $val:expr $(, $rest:expr )* ] [ $($tt:tt)* ]) => {
let valstr = stringify!($val);
(__expand $depth:ident ; [ $valstr:ident $val:ident $($rest:tt)* ] [ $($tt:tt)* ]) => {
let valdbg = (&$val).value_debug_format($depth).try_to_string().await?;
$crate::vdbg!(__expand $depth ; [ $($rest),* ] [ $($tt)* valstr valdbg ]);
$crate::vdbg!(__expand $depth ; [ $($rest)* ] [ $($tt)* $valstr valdbg ]);
};
(__expand $depth:ident ; [] [ $( $valstr:ident $valdbg:ident )* ]) => {
// By pre-awaiting, then printing everything at once, we ensure that the
Expand All @@ -57,6 +69,6 @@ macro_rules! vdbg {
$crate::vdbg!(__init $depth ; $($val),*)
};
($($val:expr),+ $(,)?) => {
$crate::vdbg!(__init usize::MAX ; $($val),*)
$crate::vdbg!(__init usize::MAX ; [ $($val),* ] [])
};
}
5 changes: 4 additions & 1 deletion crates/turbo-tasks/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,10 @@ impl<B: Backend + 'static> TurboTasksApi for TurboTasks<B> {
turbo_tasks(),
CURRENT_TASK_ID.scope(
current_task_id,
self.backend.execution_scope(current_task_id, f),
CELL_COUNTERS.scope(
Default::default(),
self.backend.execution_scope(current_task_id, f),
),
),
))
}
Expand Down

0 comments on commit 339d9fd

Please sign in to comment.