Skip to content

Commit

Permalink
refactor(turbo-tasks): Codemod/migrate all ResolvedVc casting callsit…
Browse files Browse the repository at this point in the history
…es to use the new synchronous APIs
  • Loading branch information
bgw committed Feb 3, 2025
1 parent 98d4bba commit ef0b8aa
Show file tree
Hide file tree
Showing 55 changed files with 122 additions and 182 deletions.
10 changes: 5 additions & 5 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,8 +1634,7 @@ impl AppEndpoint {
.edge_rsc_runtime_entries()
.await?
.clone_value();
let evaluatable = ResolvedVc::try_sidecast(app_entry.rsc_entry)
.await?
let evaluatable = ResolvedVc::try_sidecast_sync(app_entry.rsc_entry)
.context("Entry module must be evaluatable")?;
evaluatable_assets.push(evaluatable);
evaluatable_assets.push(server_action_manifest_loader);
Expand Down Expand Up @@ -1676,9 +1675,10 @@ impl AppEndpoint {
.server_utils
.iter()
.map(|m| async move {
Ok(*ResolvedVc::try_downcast::<Box<dyn ChunkableModule>>(*m)
.await?
.context("Expected server utils to be chunkable")?)
Ok(
*ResolvedVc::try_downcast_sync::<Box<dyn ChunkableModule>>(*m)
.context("Expected server utils to be chunkable")?,
)
})
.try_join()
.await?;
Expand Down
4 changes: 2 additions & 2 deletions crates/next-api/src/dynamic_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub async fn map_next_dynamic(graph: Vc<SingleModuleGraph>) -> Result<Vc<Dynamic
.is_some_and(|layer| &**layer == "app-client" || &**layer == "client")
{
if let Some(dynamic_entry_module) =
ResolvedVc::try_downcast_type::<NextDynamicEntryModule>(*module).await?
ResolvedVc::try_downcast_type_sync::<NextDynamicEntryModule>(*module)
{
return Ok(Some((
*module,
Expand All @@ -143,7 +143,7 @@ pub async fn map_next_dynamic(graph: Vc<SingleModuleGraph>) -> Result<Vc<Dynamic
// TODO add this check once these modules have the correct layer
// if layer.is_some_and(|layer| &**layer == "app-rsc") {
if let Some(client_reference_module) =
ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(*module).await?
ResolvedVc::try_downcast_type_sync::<EcmascriptClientReferenceModule>(*module)
{
return Ok(Some((
*module,
Expand Down
6 changes: 3 additions & 3 deletions crates/next-api/src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ impl InstrumentationEndpoint {
.clone_value();

let Some(module) =
ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(module).await?
ResolvedVc::try_downcast_sync::<Box<dyn EcmascriptChunkPlaceable>>(module)
else {
bail!("Entry module must be evaluatable");
};

let Some(evaluatable) = ResolvedVc::try_sidecast(module).await? else {
let Some(evaluatable) = ResolvedVc::try_sidecast_sync(module) else {
bail!("Entry module must be evaluatable");
};
evaluatable_assets.push(evaluatable);
Expand All @@ -155,7 +155,7 @@ impl InstrumentationEndpoint {
let userland_module = self.core_modules().await?.userland_module;
let module_graph = this.project.module_graph(*userland_module);

let Some(module) = ResolvedVc::try_downcast(userland_module).await? else {
let Some(module) = ResolvedVc::try_downcast_sync(userland_module) else {
bail!("Entry module must be evaluatable");
};

Expand Down
3 changes: 1 addition & 2 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,7 @@ impl PageEndpoint {
let is_edge = matches!(runtime, NextRuntime::Edge);
if is_edge {
let mut evaluatable_assets = edge_runtime_entries.await?.clone_value();
let evaluatable = ResolvedVc::try_sidecast(ssr_module)
.await?
let evaluatable = ResolvedVc::try_sidecast_sync(ssr_module)
.context("could not process page loader entry module")?;
evaluatable_assets.push(evaluatable);

Expand Down
2 changes: 1 addition & 1 deletion crates/next-api/src/versioned_content_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl VersionedContentMap {
};

if let Some(generate_source_map) =
ResolvedVc::try_sidecast::<Box<dyn GenerateSourceMap>>(*asset).await?
ResolvedVc::try_sidecast_sync::<Box<dyn GenerateSourceMap>>(*asset)
{
Ok(if let Some(section) = section {
generate_source_map.by_section(section)
Expand Down
2 changes: 1 addition & 1 deletion crates/next-api/src/webpack_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
continue;
};

if let Some(chunk) = ResolvedVc::try_downcast_type::<EcmascriptDevChunk>(*asset).await? {
if let Some(chunk) = ResolvedVc::try_downcast_type_sync::<EcmascriptDevChunk>(*asset) {
let chunk_ident = normalize_client_path(&chunk.ident().path().await?.path);
chunks.push(WebpackStatsChunk {
size: asset_len,
Expand Down
3 changes: 1 addition & 2 deletions crates/next-core/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ pub async fn bootstrap(
.to_resolved()
.await?;

let asset = ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(asset)
.await?
let asset = ResolvedVc::try_sidecast_sync::<Box<dyn EvaluatableAsset>>(asset)
.context("internal module must be evaluatable")?;

Ok(*asset)
Expand Down
3 changes: 1 addition & 2 deletions crates/next-core/src/next_client/runtime_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ impl RuntimeEntry {

let mut runtime_entries = Vec::with_capacity(modules.len());
for &module in &modules {
if let Some(entry) =
ResolvedVc::try_downcast::<Box<dyn EvaluatableAsset>>(module).await?
if let Some(entry) = ResolvedVc::try_downcast_sync::<Box<dyn EvaluatableAsset>>(module)
{
runtime_entries.push(entry);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ impl Transition for NextEcmascriptClientReferenceTransition {
};

let Some(client_module) =
ResolvedVc::try_sidecast::<Box<dyn EcmascriptChunkPlaceable>>(client_module).await?
ResolvedVc::try_sidecast_sync::<Box<dyn EcmascriptChunkPlaceable>>(client_module)
else {
bail!("client asset is not ecmascript chunk placeable");
};

let Some(ssr_module) =
ResolvedVc::try_sidecast::<Box<dyn EcmascriptChunkPlaceable>>(ssr_module).await?
ResolvedVc::try_sidecast_sync::<Box<dyn EcmascriptChunkPlaceable>>(ssr_module)
else {
bail!("SSR asset is not ecmascript chunk placeable");
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ pub async fn client_reference_graph(
.map(|module| async move {
Ok(VisitClientReferenceNode {
state: if let Some(server_component) =
ResolvedVc::try_downcast_type::<NextServerComponentModule>(module)
.await?
{
ResolvedVc::try_downcast_type_sync::<NextServerComponentModule>(
module,
) {
VisitClientReferenceNodeState::InServerComponent {
server_component,
}
Expand Down Expand Up @@ -414,9 +414,9 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
})
.flatten()
.map(|module| async move {
if let Some(client_reference_module) =
ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(*module)
.await?
if let Some(client_reference_module) = ResolvedVc::try_downcast_type_sync::<
EcmascriptClientReferenceModule,
>(*module)
{
return Ok(VisitClientReferenceNode {
state: node.state,
Expand All @@ -433,7 +433,7 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
}

if let Some(client_reference_module) =
ResolvedVc::try_downcast_type::<CssClientReferenceModule>(*module).await?
ResolvedVc::try_downcast_type_sync::<CssClientReferenceModule>(*module)
{
return Ok(VisitClientReferenceNode {
state: node.state,
Expand All @@ -450,7 +450,7 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
}

if let Some(server_component_asset) =
ResolvedVc::try_downcast_type::<NextServerComponentModule>(*module).await?
ResolvedVc::try_downcast_type_sync::<NextServerComponentModule>(*module)
{
return Ok(VisitClientReferenceNode {
state: VisitClientReferenceNodeState::InServerComponent {
Expand All @@ -463,8 +463,7 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
});
}

if ResolvedVc::try_downcast_type::<NextServerUtilityModule>(*module)
.await?
if ResolvedVc::try_downcast_type_sync::<NextServerUtilityModule>(*module)
.is_some()
{
return Ok(VisitClientReferenceNode {
Expand Down
7 changes: 3 additions & 4 deletions crates/next-core/src/next_dynamic/dynamic_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ impl Transition for NextDynamicTransition {

Ok(match &*module.try_into_module().await? {
Some(client_module) => {
let Some(client_module) =
ResolvedVc::try_sidecast::<Box<dyn EcmascriptChunkPlaceable>>(*client_module)
.await?
else {
let Some(client_module) = ResolvedVc::try_sidecast_sync::<
Box<dyn EcmascriptChunkPlaceable>,
>(*client_module) else {
bail!("not an ecmascript client_module");
};

Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/next_dynamic/visit_dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl Visit<VisitDynamicNode> for VisitDynamic {

let referenced_modules = referenced_modules.iter().map(|module| async move {
if let Some(next_dynamic_module) =
ResolvedVc::try_downcast_type::<NextDynamicEntryModule>(*module).await?
ResolvedVc::try_downcast_type_sync::<NextDynamicEntryModule>(*module)
{
return Ok(VisitDynamicNode::Dynamic(
next_dynamic_module,
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ pub async fn parse_config_from_source(
module: ResolvedVc<Box<dyn Module>>,
) -> Result<Vc<NextSourceConfig>> {
if let Some(ecmascript_asset) =
ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(module).await?
ResolvedVc::try_sidecast_sync::<Box<dyn EcmascriptParsable>>(module)
{
if let ParseResult::Ok {
program: Program::Module(module_ast),
Expand Down
46 changes: 1 addition & 45 deletions turbopack/crates/turbo-tasks/src/vc/resolved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
debug::{ValueDebug, ValueDebugFormat, ValueDebugFormatString},
trace::{TraceRawVcs, TraceRawVcsContext},
vc::Vc,
ResolveTypeError, Upcast, VcRead, VcTransparentRead, VcValueTrait, VcValueType,
Upcast, VcRead, VcTransparentRead, VcValueTrait, VcValueType,
};

/// A "subtype" (via [`Deref`]) of [`Vc`] that represents a specific [`Vc::cell`]/`.cell()` or
Expand Down Expand Up @@ -211,23 +211,6 @@ impl<T> ResolvedVc<T>
where
T: VcValueTrait + ?Sized,
{
/// Attempts to sidecast the given `Vc<Box<dyn T>>` to a `Vc<Box<dyn K>>`.
///
/// Returns `None` if the underlying value type does not implement `K`.
///
/// **Note:** if the trait `T` is required to implement `K`, use [`ResolvedVc::upcast`] instead.
/// This provides stronger guarantees, removing the need for a [`Result`] return type.
///
/// See also: [`Vc::try_resolve_sidecast`].
pub async fn try_sidecast<K>(this: Self) -> Result<Option<ResolvedVc<K>>, ResolveTypeError>
where
K: VcValueTrait + ?Sized,
{
// TODO: Expose a synchronous API instead of this async one that returns `Result<Option<_>>`
Ok(Self::try_sidecast_sync(this))
}

/// Attempts to sidecast the given `ResolvedVc<Box<dyn T>>` to a `ResolvedVc<Box<dyn K>>`.
///
/// Returns `None` if the underlying value type does not implement `K`.
///
Expand All @@ -252,20 +235,6 @@ where
})
}

/// Attempts to downcast the given `ResolvedVc<Box<dyn T>>` to a `ResolvedVc<K>`, where `K`
/// is of the form `Box<dyn L>`, and `L` is a value trait.
///
/// Returns `None` if the underlying value type is not a `K`.
///
/// See also: [`Vc::try_resolve_downcast`].
pub async fn try_downcast<K>(this: Self) -> Result<Option<ResolvedVc<K>>, ResolveTypeError>
where
K: Upcast<T> + VcValueTrait + ?Sized,
{
// TODO: Expose a synchronous API instead of this async one that returns `Result<Option<_>>`
Ok(Self::try_downcast_sync(this))
}

/// Attempts to downcast the given `ResolvedVc<Box<dyn T>>` to a `ResolvedVc<K>`, where `K`
/// is of the form `Box<dyn L>`, and `L` is a value trait.
///
Expand All @@ -280,19 +249,6 @@ where
Self::try_sidecast_sync(this)
}

/// Attempts to downcast the given `Vc<Box<dyn T>>` to a `Vc<K>`, where `K` is a value type.
///
/// Returns `None` if the underlying value type is not a `K`.
///
/// See also: [`Vc::try_resolve_downcast_type`].
pub async fn try_downcast_type<K>(this: Self) -> Result<Option<ResolvedVc<K>>, ResolveTypeError>
where
K: Upcast<T> + VcValueType,
{
// TODO: Expose a synchronous API instead of this async one that returns `Result<Option<_>>`
Ok(Self::try_downcast_type_sync(this))
}

/// Attempts to downcast the given `Vc<Box<dyn T>>` to a `Vc<K>`, where `K` is a value type.
///
/// Returns `None` if the underlying value type is not a `K`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ impl EcmascriptDevEvaluateChunk {
let module_graph = this.module_graph;
move |entry| async move {
if let Some(placeable) =
ResolvedVc::try_sidecast::<Box<dyn EcmascriptChunkPlaceable>>(*entry)
.await?
ResolvedVc::try_sidecast_sync::<Box<dyn EcmascriptChunkPlaceable>>(*entry)
{
Ok(Some(
placeable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ impl EcmascriptDevChunkListContent {

for (chunk_path, chunk_content) in &self.chunks_contents {
if let Some(mergeable) =
ResolvedVc::try_sidecast::<Box<dyn MergeableVersionedContent>>(*chunk_content)
.await?
ResolvedVc::try_sidecast_sync::<Box<dyn MergeableVersionedContent>>(*chunk_content)
{
let merger = mergeable.get_merger().resolve().await?;
by_merger.entry(merger).or_default().push(*chunk_content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub(super) async fn update_chunk_list(

for (chunk_path, chunk_content) in &content.chunks_contents {
if let Some(mergeable) =
ResolvedVc::try_sidecast::<Box<dyn MergeableVersionedContent>>(*chunk_content).await?
ResolvedVc::try_sidecast_sync::<Box<dyn MergeableVersionedContent>>(*chunk_content)
{
let merger = mergeable.get_merger().to_resolved().await?;
by_merger.entry(merger).or_default().push(*chunk_content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl VersionedContentMerger for EcmascriptDevChunkContentMerger {
.iter()
.map(|content| async move {
if let Some(content) =
ResolvedVc::try_downcast_type::<EcmascriptDevChunkContent>(*content).await?
ResolvedVc::try_downcast_type_sync::<EcmascriptDevChunkContent>(*content)
{
Ok(content)
} else {
Expand Down
3 changes: 1 addition & 2 deletions turbopack/crates/turbopack-cli-utils/src/runtime_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ impl RuntimeEntry {

let mut runtime_entries = Vec::with_capacity(modules.len());
for &module in &modules {
if let Some(entry) =
ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module).await?
if let Some(entry) = ResolvedVc::try_sidecast_sync::<Box<dyn EvaluatableAsset>>(module)
{
runtime_entries.push(entry);
} else {
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ async fn build_internal(
.map(|entry_module| async move {
Ok(
if let Some(ecmascript) =
ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(entry_module).await?
ResolvedVc::try_sidecast_sync::<Box<dyn EvaluatableAsset>>(entry_module)
{
match target {
Target::Browser => {
Expand Down
6 changes: 3 additions & 3 deletions turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ pub async fn create_web_entry_source(
.into_iter()
.map(|module| async move {
if let (Some(chunkable_module), Some(entry)) = (
ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(module).await?,
ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module).await?,
ResolvedVc::try_sidecast_sync::<Box<dyn ChunkableModule>>(module),
ResolvedVc::try_sidecast_sync::<Box<dyn EvaluatableAsset>>(module),
) {
Ok(DevHtmlEntry {
chunkable_module,
Expand All @@ -173,7 +173,7 @@ pub async fn create_web_entry_source(
runtime_entries: Some(runtime_entries.with_entry(*entry).to_resolved().await?),
})
} else if let Some(chunkable_module) =
ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(module).await?
ResolvedVc::try_sidecast_sync::<Box<dyn ChunkableModule>>(module)
{
// TODO this is missing runtime code, so it's probably broken and we should also
// add an ecmascript chunk with the runtime code
Expand Down
7 changes: 4 additions & 3 deletions turbopack/crates/turbopack-core/src/introspect/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ pub struct IntrospectableModule(ResolvedVc<Box<dyn Module>>);
impl IntrospectableModule {
#[turbo_tasks::function]
pub async fn new(asset: ResolvedVc<Box<dyn Module>>) -> Result<Vc<Box<dyn Introspectable>>> {
Ok(*ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(asset)
.await?
.unwrap_or_else(|| ResolvedVc::upcast(IntrospectableModule(asset).resolved_cell())))
Ok(
*ResolvedVc::try_sidecast_sync::<Box<dyn Introspectable>>(asset)
.unwrap_or_else(|| ResolvedVc::upcast(IntrospectableModule(asset).resolved_cell())),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ impl IntrospectableOutputAsset {
pub async fn new(
asset: ResolvedVc<Box<dyn OutputAsset>>,
) -> Result<Vc<Box<dyn Introspectable>>> {
Ok(*ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(asset)
.await?
.unwrap_or_else(|| {
Ok(
*ResolvedVc::try_sidecast_sync::<Box<dyn Introspectable>>(asset).unwrap_or_else(|| {
ResolvedVc::upcast(IntrospectableOutputAsset(asset).resolved_cell())
}))
}),
)
}
}

Expand Down
Loading

0 comments on commit ef0b8aa

Please sign in to comment.