Repository: vercel/next.js
File: turbopack/crates/turbopack-core/src/reference/mod.rs (Rust)

Code before:
    pub chunking_type: ChunkingType,
    pub binding_usage: BindingUsage,
    pub modules: Vec<ResolvedVc<Box<dyn Module>>>,
}

#[turbo_tasks::value(transparent)]
pub struct ModulesWithRefData(Vec<(ResolvedVc<Box<dyn ModuleReference>>, ResolvedReference)>);

/// Aggregates all primary [Module]s referenced by an [Module] via [ChunkableModuleReference]s.
/// This does not include transitively referenced [Module]s, only includes
/// primary [Module]s referenced.
///
/// [Module]: crate::module::Module
#[turbo_tasks::function]
pub async fn primary_chunkable_referenced_modules(
    module: ResolvedVc<Box<dyn Module>>,
    include_traced: bool,
    include_binding_usage: bool,
) -> Result<Vc<ModulesWithRefData>> {
    let modules = module
        .references()
        .await?
        .iter()
        .map(|reference| async {
            if let Some(reference) =
                ResolvedVc::try_downcast::<Box<dyn ChunkableModuleReference>>(*reference)
                && let Some(chunking_type) = &*reference.chunking_type().await?
            {
                if !include_traced && matches!(chunking_type, ChunkingType::Traced) {
                    return Ok(None);
                }

                let resolved = reference
                    .resolve_reference()
                    .await?
                    .primary_modules_ref()
                    .await?;
                let binding_usage = if include_binding_usage {
                    reference.binding_usage().owned().await?
                } else {
                    BindingUsage::default()
                };

                return Ok(Some((
                    ResolvedVc::upcast(reference),
                    ResolvedReference {
                        chunking_type: chunking_type.clone(),
                        binding_usage,
                        modules: resolved,
                    },
                )));

Review comment (suggestion):
so this would be the most concerning try_downcast case, but since nearly everything is chunkable your theory is that this wasn't saving us very much

Code after:
#[derive(Clone, Eq, PartialEq, ValueDebugFormat, TraceRawVcs, NonLocalValue, Encode, Decode)]
pub struct ResolvedReference {
    pub chunking_type: ChunkingType,
    pub binding_usage: BindingUsage,
    pub modules: Vec<ResolvedVc<Box<dyn Module>>>,
}

#[turbo_tasks::value(transparent)]
pub struct ModulesWithRefData(Vec<(ResolvedVc<Box<dyn ModuleReference>>, ResolvedReference)>);

/// Aggregates all primary [Module]s referenced by an [Module] via [ModuleReference]s with a
/// non-empty chunking_type. This does not include transitively referenced [Module]s, only primary
/// [Module]s referenced.
///
/// [Module]: crate::module::Module
#[turbo_tasks::function]
pub async fn primary_chunkable_referenced_modules(
    module: ResolvedVc<Box<dyn Module>>,
    include_traced: bool,
    include_binding_usage: bool,
) -> Result<Vc<ModulesWithRefData>> {
    let modules = module
        .references()
        .await?
        .iter()
        .map(|reference| async {
            if let Some(chunking_type) = &*reference.chunking_type().await? {
                if !include_traced && matches!(chunking_type, ChunkingType::Traced) {
                    return Ok(None);
                }

                let resolved = reference
                    .resolve_reference()
                    .await?
                    .primary_modules_ref()
                    .await?;
                let binding_usage = if include_binding_usage {
                    reference.binding_usage().owned().await?
                } else {
                    BindingUsage::default()
                };

                return Ok(Some((
                    *reference,
                    ResolvedReference {
                        chunking_type: chunking_type.clone(),
                        binding_usage,
                        modules: resolved,
                    },
                )));
            }