Unverified Commit a3aadbd3 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Remove `Clone` from `Effect` (#86110)

Disallow cloning effects.

This was only performed in one location to address an issue with modeling calls functions that had alternates. However, this had the unfortunate effect of producing duplicate `CodeGen` objects. (it also could link values redundantly). This was fixed by refactoring the recursion and moving the the effect collection to one place.

Many CodeGens are naturally idempotent so this wouldn't typically cause trouble, but it is wasteful and potentially hides other bugs.  For example, this also adds detection for duplicate code gens and doing so found that we were generating unreachable effects for finalizers incorrectly.
parent 88fe8793
...@@ -35,7 +35,7 @@ use crate::{ ...@@ -35,7 +35,7 @@ use crate::{
utils::{AstPathRange, unparen}, utils::{AstPathRange, unparen},
}; };
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct EffectsBlock { pub struct EffectsBlock {
pub effects: Vec<Effect>, pub effects: Vec<Effect>,
pub range: AstPathRange, pub range: AstPathRange,
...@@ -47,7 +47,7 @@ impl EffectsBlock { ...@@ -47,7 +47,7 @@ impl EffectsBlock {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum ConditionalKind { pub enum ConditionalKind {
/// The blocks of an `if` statement without an `else` block. /// The blocks of an `if` statement without an `else` block.
If { then: Box<EffectsBlock> }, If { then: Box<EffectsBlock> },
...@@ -117,7 +117,7 @@ impl ConditionalKind { ...@@ -117,7 +117,7 @@ impl ConditionalKind {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum EffectArg { pub enum EffectArg {
Value(JsValue), Value(JsValue),
Closure(JsValue, Box<EffectsBlock>), Closure(JsValue, Box<EffectsBlock>),
...@@ -140,7 +140,7 @@ impl EffectArg { ...@@ -140,7 +140,7 @@ impl EffectArg {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum Effect { pub enum Effect {
/// Some condition which affects which effects might be executed. If the /// Some condition which affects which effects might be executed. If the
/// condition evaluates to some compile-time constant, we can use that /// condition evaluates to some compile-time constant, we can use that
...@@ -2428,14 +2428,16 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -2428,14 +2428,16 @@ impl VisitAstPath for Analyzer<'_> {
self.effects.append(&mut block); self.effects.append(&mut block);
self.effects.append(&mut handler); self.effects.append(&mut handler);
if let Some(finalizer) = stmt.finalizer.as_ref() { if let Some(finalizer) = stmt.finalizer.as_ref() {
{
let mut ast_path = let mut ast_path =
ast_path.with_guard(AstParentNodeRef::TryStmt(stmt, TryStmtField::Finalizer)); ast_path.with_guard(AstParentNodeRef::TryStmt(stmt, TryStmtField::Finalizer));
finalizer.visit_with_ast_path(self, &mut ast_path); finalizer.visit_with_ast_path(self, &mut ast_path);
}
// If a finally block early returns the parent block does too. // If a finally block early returns the parent block does too.
if self.end_early_return_block() { if self.end_early_return_block() {
self.early_return_stack.push(EarlyReturn::Always { self.early_return_stack.push(EarlyReturn::Always {
prev_effects: take(&mut self.effects), prev_effects: take(&mut self.effects),
start_ast_path: as_parent_path(&ast_path), start_ast_path: as_parent_path(ast_path),
}); });
} }
}; };
......
...@@ -173,7 +173,9 @@ impl_modify!(visit_mut_block_stmt, BlockStmt); ...@@ -173,7 +173,9 @@ impl_modify!(visit_mut_block_stmt, BlockStmt);
impl_modify!(visit_mut_switch_case, SwitchCase); impl_modify!(visit_mut_switch_case, SwitchCase);
impl_modify!(visit_mut_program, Program); impl_modify!(visit_mut_program, Program);
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub enum CodeGen { pub enum CodeGen {
// AMD occurs very rarely and makes the enum much bigger // AMD occurs very rarely and makes the enum much bigger
AmdDefineWithDependenciesCodeGen(Box<AmdDefineWithDependenciesCodeGen>), AmdDefineWithDependenciesCodeGen(Box<AmdDefineWithDependenciesCodeGen>),
......
...@@ -98,6 +98,7 @@ impl ChunkableModuleReference for AmdDefineAssetReference {} ...@@ -98,6 +98,7 @@ impl ChunkableModuleReference for AmdDefineAssetReference {}
TraceRawVcs, TraceRawVcs,
Clone, Clone,
NonLocalValue, NonLocalValue,
Hash,
)] )]
pub enum AmdDefineDependencyElement { pub enum AmdDefineDependencyElement {
Request { Request {
...@@ -120,6 +121,7 @@ pub enum AmdDefineDependencyElement { ...@@ -120,6 +121,7 @@ pub enum AmdDefineDependencyElement {
Copy, Copy,
Clone, Clone,
NonLocalValue, NonLocalValue,
Hash,
)] )]
pub enum AmdDefineFactoryType { pub enum AmdDefineFactoryType {
Unknown, Unknown,
...@@ -127,7 +129,9 @@ pub enum AmdDefineFactoryType { ...@@ -127,7 +129,9 @@ pub enum AmdDefineFactoryType {
Value, Value,
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct AmdDefineWithDependenciesCodeGen { pub struct AmdDefineWithDependenciesCodeGen {
dependencies_requests: Vec<AmdDefineDependencyElement>, dependencies_requests: Vec<AmdDefineDependencyElement>,
origin: ResolvedVc<Box<dyn ResolveOrigin>>, origin: ResolvedVc<Box<dyn ResolveOrigin>>,
......
...@@ -150,7 +150,9 @@ impl IntoCodeGenReference for CjsRequireAssetReference { ...@@ -150,7 +150,9 @@ impl IntoCodeGenReference for CjsRequireAssetReference {
} }
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct CjsRequireAssetReferenceCodeGen { pub struct CjsRequireAssetReferenceCodeGen {
reference: ResolvedVc<CjsRequireAssetReference>, reference: ResolvedVc<CjsRequireAssetReference>,
path: AstPath, path: AstPath,
...@@ -274,7 +276,9 @@ impl IntoCodeGenReference for CjsRequireResolveAssetReference { ...@@ -274,7 +276,9 @@ impl IntoCodeGenReference for CjsRequireResolveAssetReference {
} }
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct CjsRequireResolveAssetReferenceCodeGen { pub struct CjsRequireResolveAssetReferenceCodeGen {
reference: ResolvedVc<CjsRequireResolveAssetReference>, reference: ResolvedVc<CjsRequireResolveAssetReference>,
path: AstPath, path: AstPath,
...@@ -334,7 +338,9 @@ impl CjsRequireResolveAssetReferenceCodeGen { ...@@ -334,7 +338,9 @@ impl CjsRequireResolveAssetReferenceCodeGen {
} }
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct CjsRequireCacheAccess { pub struct CjsRequireCacheAccess {
pub path: AstPath, pub path: AstPath,
} }
......
...@@ -19,7 +19,9 @@ pub enum ConstantConditionValue { ...@@ -19,7 +19,9 @@ pub enum ConstantConditionValue {
Nullish, Nullish,
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct ConstantConditionCodeGen { pub struct ConstantConditionCodeGen {
value: ConstantConditionValue, value: ConstantConditionValue,
path: AstPath, path: AstPath,
......
...@@ -10,13 +10,17 @@ use crate::{ ...@@ -10,13 +10,17 @@ use crate::{
create_visitor, create_visitor,
}; };
#[derive(PartialEq, Eq, TraceRawVcs, Serialize, Deserialize, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, TraceRawVcs, Serialize, Deserialize, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
enum DynamicExpressionType { enum DynamicExpressionType {
Promise, Promise,
Normal, Normal,
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct DynamicExpression { pub struct DynamicExpression {
path: AstPath, path: AstPath,
ty: DynamicExpressionType, ty: DynamicExpressionType,
......
...@@ -121,7 +121,9 @@ impl IntoCodeGenReference for EsmAsyncAssetReference { ...@@ -121,7 +121,9 @@ impl IntoCodeGenReference for EsmAsyncAssetReference {
} }
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct EsmAsyncAssetReferenceCodeGen { pub struct EsmAsyncAssetReferenceCodeGen {
path: AstPath, path: AstPath,
reference: ResolvedVc<EsmAsyncAssetReference>, reference: ResolvedVc<EsmAsyncAssetReference>,
......
...@@ -26,7 +26,9 @@ use crate::{ ...@@ -26,7 +26,9 @@ use crate::{
/// in the file. But we must only initialize the binding a single time. /// in the file. But we must only initialize the binding a single time.
/// ///
/// This singleton behavior must be enforced by the caller! /// This singleton behavior must be enforced by the caller!
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct ImportMetaBinding { pub struct ImportMetaBinding {
path: FileSystemPath, path: FileSystemPath,
} }
...@@ -85,7 +87,9 @@ impl From<ImportMetaBinding> for CodeGen { ...@@ -85,7 +87,9 @@ impl From<ImportMetaBinding> for CodeGen {
/// ///
/// There can be many references to import.meta, and they appear at any nesting /// There can be many references to import.meta, and they appear at any nesting
/// in the file. But all references refer to the same mutable object. /// in the file. But all references refer to the same mutable object.
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct ImportMetaRef { pub struct ImportMetaRef {
ast_path: AstPath, ast_path: AstPath,
} }
......
...@@ -73,7 +73,9 @@ impl IntoCodeGenReference for EsmModuleIdAssetReference { ...@@ -73,7 +73,9 @@ impl IntoCodeGenReference for EsmModuleIdAssetReference {
} }
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct EsmModuleIdAssetReferenceCodeGen { pub struct EsmModuleIdAssetReferenceCodeGen {
path: AstPath, path: AstPath,
reference: ResolvedVc<EsmModuleIdAssetReference>, reference: ResolvedVc<EsmModuleIdAssetReference>,
......
...@@ -22,7 +22,9 @@ use crate::{ ...@@ -22,7 +22,9 @@ use crate::{
/// Makes code changes to remove export/import declarations and places the /// Makes code changes to remove export/import declarations and places the
/// expr/decl in a normal statement. Unnamed expr/decl will be named with the /// expr/decl in a normal statement. Unnamed expr/decl will be named with the
/// magic identifier "export default" /// magic identifier "export default"
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct EsmModuleItem { pub struct EsmModuleItem {
pub path: AstPath, pub path: AstPath,
} }
......
...@@ -146,7 +146,9 @@ impl IntoCodeGenReference for UrlAssetReference { ...@@ -146,7 +146,9 @@ impl IntoCodeGenReference for UrlAssetReference {
} }
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct UrlAssetReferenceCodeGen { pub struct UrlAssetReferenceCodeGen {
reference: ResolvedVc<UrlAssetReference>, reference: ResolvedVc<UrlAssetReference>,
path: AstPath, path: AstPath,
......
...@@ -23,7 +23,9 @@ use crate::{ ...@@ -23,7 +23,9 @@ use crate::{
/// initialize the binding a single time. /// initialize the binding a single time.
/// ///
/// This singleton behavior must be enforced by the caller! /// This singleton behavior must be enforced by the caller!
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct ExportsInfoBinding {} pub struct ExportsInfoBinding {}
impl ExportsInfoBinding { impl ExportsInfoBinding {
...@@ -89,7 +91,9 @@ impl From<ExportsInfoBinding> for CodeGen { ...@@ -89,7 +91,9 @@ impl From<ExportsInfoBinding> for CodeGen {
/// ///
/// There can be many references, and they appear at any nesting in the file. But all references /// There can be many references, and they appear at any nesting in the file. But all references
/// refer to the same mutable object. /// refer to the same mutable object.
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct ExportsInfoRef { pub struct ExportsInfoRef {
ast_path: AstPath, ast_path: AstPath,
} }
......
...@@ -11,7 +11,9 @@ use crate::{ ...@@ -11,7 +11,9 @@ use crate::{
create_visitor, create_visitor,
}; };
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct IdentReplacement { pub struct IdentReplacement {
value: RcStr, value: RcStr,
path: AstPath, path: AstPath,
......
...@@ -20,7 +20,9 @@ use crate::{ ...@@ -20,7 +20,9 @@ use crate::{
create_visitor, create_visitor,
}; };
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct MemberReplacement { pub struct MemberReplacement {
key: RcStr, key: RcStr,
value: RcStr, value: RcStr,
......
...@@ -317,7 +317,9 @@ impl IntoCodeGenReference for RequireContextAssetReference { ...@@ -317,7 +317,9 @@ impl IntoCodeGenReference for RequireContextAssetReference {
} }
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct RequireContextAssetReferenceCodeGen { pub struct RequireContextAssetReferenceCodeGen {
path: AstPath, path: AstPath,
reference: ResolvedVc<RequireContextAssetReference>, reference: ResolvedVc<RequireContextAssetReference>,
......
...@@ -31,7 +31,9 @@ use crate::{ ...@@ -31,7 +31,9 @@ use crate::{
utils::AstPathRange, utils::AstPathRange,
}; };
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct Unreachable { pub struct Unreachable {
range: AstPathRange, range: AstPathRange,
......
...@@ -125,7 +125,9 @@ impl IntoCodeGenReference for WorkerAssetReference { ...@@ -125,7 +125,9 @@ impl IntoCodeGenReference for WorkerAssetReference {
} }
} }
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct WorkerAssetReferenceCodeGen { pub struct WorkerAssetReferenceCodeGen {
reference: ResolvedVc<WorkerAssetReference>, reference: ResolvedVc<WorkerAssetReference>,
path: AstPath, path: AstPath,
......
...@@ -178,7 +178,7 @@ format_iter!(std::fmt::Pointer); ...@@ -178,7 +178,7 @@ format_iter!(std::fmt::Pointer);
format_iter!(std::fmt::UpperExp); format_iter!(std::fmt::UpperExp);
format_iter!(std::fmt::UpperHex); format_iter!(std::fmt::UpperHex);
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, Debug, NonLocalValue)] #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, Debug, NonLocalValue, Hash)]
pub enum AstPathRange { pub enum AstPathRange {
/// The ast path to the block or expression. /// The ast path to the block or expression.
Exact(#[turbo_tasks(trace_ignore)] Vec<AstParentKind>), Exact(#[turbo_tasks(trace_ignore)] Vec<AstParentKind>),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment