Skip to content

Commit

Permalink
Support ResolvedVc arguments to operations
Browse files Browse the repository at this point in the history
  • Loading branch information
bgw committed Dec 16, 2024
1 parent b575b6d commit 8e88c0b
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 90 deletions.
1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks-backend/tests/operation_vc.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![allow(dead_code)]
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

use turbo_tasks::Vc;

#[turbo_tasks::value]
struct Foobar;

#[turbo_tasks::value_impl]
impl Foobar {
#[turbo_tasks::function(operation)]
fn self_ref(&self) -> Vc<()> {
Vc::cell(())
}
}

fn main() {}

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

Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,10 @@ struct Foobar;

#[turbo_tasks::value_impl]
impl Foobar {
#[turbo_tasks::function(operation)]
fn self_ref(&self) -> Vc<()> {
Vc::cell(())
}

#[turbo_tasks::function(operation)]
fn arbitrary_self_type(self: OperationVc<Self>) -> Vc<()> {
Vc::cell(())
}

#[turbo_tasks::function(operation)]
fn arbitrary_self_type_base_vc(self: Vc<Self>) -> Vc<()> {
Vc::cell(())
}
}

fn main() {}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![allow(dead_code)]
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

use turbo_tasks::Vc;

#[turbo_tasks::value]
struct Foobar;

#[turbo_tasks::value_impl]
impl Foobar {
#[turbo_tasks::function(operation)]
fn arbitrary_self_type_base_vc(self: Vc<Self>) -> Vc<()> {
Vc::cell(())
}
}

fn main() {}

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use anyhow::Result;
use turbo_tasks::{OperationVc, ResolvedVc, Vc};

#[turbo_tasks::function(operation)]
fn bare_op_fn() -> Vc<i32> {
Vc::cell(21)
}

// operations can take `ResolvedVc`s too (anything that's a `NonLocalValue`).
#[turbo_tasks::function(operation)]
async fn multiply(value: OperationVc<i32>, coefficient: ResolvedVc<i32>) -> Result<Vc<i32>> {
Ok(Vc::cell((*value.connect().await?) * (*coefficient.await?)))
}

#[allow(dead_code)]
fn use_operations() {
let twenty_one: OperationVc<i32> = bare_op_fn();
let _fourty_two: OperationVc<i32> = multiply(twenty_one, ResolvedVc::cell(2));
}

fn main() {}
15 changes: 13 additions & 2 deletions turbopack/crates/turbo-tasks-macros/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ impl TurboFn<'_> {
subpat: None,
})),
colon_token: Default::default(),
ty: Box::new(expand_task_input_type(&input.ty).into_owned()),
ty: if self.operation {
// operations shouldn't have their arguments rewritten, they require all
// arguments are explicitly `NonLocalValue`s
Box::new(input.ty.clone())
} else {
Box::new(expand_task_input_type(&input.ty).into_owned())
},
})
})
.collect();
Expand Down Expand Up @@ -349,6 +355,11 @@ impl TurboFn<'_> {
.map(|(idx, arg)| match arg {
FnArg::Receiver(_) => (arg.clone(), None),
FnArg::Typed(pat_type) => {
if self.operation {
// operations shouldn't have their arguments rewritten, they require all
// arguments are explicitly `NonLocalValue`s
return (arg.clone(), None);
}
let Cow::Owned(expanded_ty) = expand_task_input_type(&pat_type.ty) else {
// common-case: skip if no type conversion is needed
return (arg.clone(), None);
Expand Down Expand Up @@ -530,7 +541,7 @@ impl TurboFn<'_> {
let ty = &pat_type.ty;
assertions.push(quote_spanned! {
ty.span() =>
turbo_tasks::macro_helpers::assert_argument_is_operation_value::<#ty>();
turbo_tasks::macro_helpers::assert_argument_is_non_local_value::<#ty>();
});
}
}
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks-memory/tests/operation_vc.rs
36 changes: 36 additions & 0 deletions turbopack/crates/turbo-tasks-testing/tests/operation_vc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this

use anyhow::Result;
use turbo_tasks::{OperationVc, ResolvedVc, Vc};
use turbo_tasks_testing::{register, run, Registration};

static REGISTRATION: Registration = register!();

#[turbo_tasks::function(operation)]
fn bare_op_fn() -> Vc<i32> {
Vc::cell(21)
}

// operations can take `ResolvedVc`s too (anything that's a `NonLocalValue`).
#[turbo_tasks::function(operation)]
async fn multiply(value: OperationVc<i32>, coefficient: ResolvedVc<i32>) -> Result<Vc<i32>> {
Ok(Vc::cell((*value.connect().await?) * (*coefficient.await?)))
}

#[turbo_tasks::function]
fn use_operations() -> Vc<i32> {
let twenty_one: OperationVc<i32> = bare_op_fn();
let fourty_two: OperationVc<i32> = multiply(twenty_one, ResolvedVc::cell(2));
fourty_two.connect()
}

#[tokio::test]
async fn test_use_operations() -> Result<()> {
run(&REGISTRATION, || async {
assert_eq!(*use_operations().await?, 42);
Ok(())
})
.await
}
4 changes: 2 additions & 2 deletions turbopack/crates/turbo-tasks/src/macro_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use super::{
};
use crate::{
debug::ValueDebugFormatString, shrink_to_fit::ShrinkToFit, task::TaskOutput, NonLocalValue,
OperationValue, RawVc, TaskInput, TaskPersistence, Vc,
RawVc, TaskInput, TaskPersistence, Vc,
};

#[inline(never)]
Expand Down Expand Up @@ -52,7 +52,7 @@ where
{
}

pub fn assert_argument_is_operation_value<Argument: OperationValue>() {}
pub fn assert_argument_is_non_local_value<Argument: NonLocalValue>() {}

#[macro_export]
macro_rules! stringify_path {
Expand Down

0 comments on commit 8e88c0b

Please sign in to comment.