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::{
utils::{AstPathRange, unparen},
};
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct EffectsBlock {
pub effects: Vec<Effect>,
pub range: AstPathRange,
......@@ -47,7 +47,7 @@ impl EffectsBlock {
}
}
#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum ConditionalKind {
/// The blocks of an `if` statement without an `else` block.
If { then: Box<EffectsBlock> },
......@@ -117,7 +117,7 @@ impl ConditionalKind {
}
}
#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum EffectArg {
Value(JsValue),
Closure(JsValue, Box<EffectsBlock>),
......@@ -140,7 +140,7 @@ impl EffectArg {
}
}
#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum Effect {
/// Some condition which affects which effects might be executed. If the
/// condition evaluates to some compile-time constant, we can use that
......@@ -2428,14 +2428,16 @@ impl VisitAstPath for Analyzer<'_> {
self.effects.append(&mut block);
self.effects.append(&mut handler);
if let Some(finalizer) = stmt.finalizer.as_ref() {
let mut ast_path =
ast_path.with_guard(AstParentNodeRef::TryStmt(stmt, TryStmtField::Finalizer));
finalizer.visit_with_ast_path(self, &mut ast_path);
{
let mut ast_path =
ast_path.with_guard(AstParentNodeRef::TryStmt(stmt, TryStmtField::Finalizer));
finalizer.visit_with_ast_path(self, &mut ast_path);
}
// If a finally block early returns the parent block does too.
if self.end_early_return_block() {
self.early_return_stack.push(EarlyReturn::Always {
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);
impl_modify!(visit_mut_switch_case, SwitchCase);
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 {
// AMD occurs very rarely and makes the enum much bigger
AmdDefineWithDependenciesCodeGen(Box<AmdDefineWithDependenciesCodeGen>),
......
......@@ -98,6 +98,7 @@ impl ChunkableModuleReference for AmdDefineAssetReference {}
TraceRawVcs,
Clone,
NonLocalValue,
Hash,
)]
pub enum AmdDefineDependencyElement {
Request {
......@@ -120,6 +121,7 @@ pub enum AmdDefineDependencyElement {
Copy,
Clone,
NonLocalValue,
Hash,
)]
pub enum AmdDefineFactoryType {
Unknown,
......@@ -127,7 +129,9 @@ pub enum AmdDefineFactoryType {
Value,
}
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
#[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct AmdDefineWithDependenciesCodeGen {
dependencies_requests: Vec<AmdDefineDependencyElement>,
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
......
......@@ -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 {
reference: ResolvedVc<CjsRequireAssetReference>,
path: AstPath,
......@@ -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 {
reference: ResolvedVc<CjsRequireResolveAssetReference>,
path: AstPath,
......@@ -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 path: AstPath,
}
......
......@@ -19,7 +19,9 @@ pub enum ConstantConditionValue {
Nullish,
}
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
#[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct ConstantConditionCodeGen {
value: ConstantConditionValue,
path: AstPath,
......
......@@ -10,13 +10,17 @@ use crate::{
create_visitor,
};
#[derive(PartialEq, Eq, TraceRawVcs, Serialize, Deserialize, ValueDebugFormat, NonLocalValue)]
#[derive(
PartialEq, Eq, TraceRawVcs, Serialize, Deserialize, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
enum DynamicExpressionType {
Promise,
Normal,
}
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
#[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct DynamicExpression {
path: AstPath,
ty: DynamicExpressionType,
......
......@@ -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 {
path: AstPath,
reference: ResolvedVc<EsmAsyncAssetReference>,
......
......@@ -26,7 +26,9 @@ use crate::{
/// in the file. But we must only initialize the binding a single time.
///
/// 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 {
path: FileSystemPath,
}
......@@ -85,7 +87,9 @@ impl From<ImportMetaBinding> for CodeGen {
///
/// 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.
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
#[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct ImportMetaRef {
ast_path: AstPath,
}
......
......@@ -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 {
path: AstPath,
reference: ResolvedVc<EsmModuleIdAssetReference>,
......
......@@ -22,7 +22,9 @@ use crate::{
/// 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
/// 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 path: AstPath,
}
......
......@@ -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 {
reference: ResolvedVc<UrlAssetReference>,
path: AstPath,
......
......@@ -23,7 +23,9 @@ use crate::{
/// initialize the binding a single time.
///
/// 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 {}
impl ExportsInfoBinding {
......@@ -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
/// 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 {
ast_path: AstPath,
}
......
......@@ -11,7 +11,9 @@ use crate::{
create_visitor,
};
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
#[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct IdentReplacement {
value: RcStr,
path: AstPath,
......
......@@ -20,7 +20,9 @@ use crate::{
create_visitor,
};
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
#[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Hash, Debug,
)]
pub struct MemberReplacement {
key: RcStr,
value: RcStr,
......
......@@ -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 {
path: AstPath,
reference: ResolvedVc<RequireContextAssetReference>,
......
......@@ -31,7 +31,9 @@ use crate::{
utils::AstPathRange,
};
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
#[derive(
PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue, Debug, Hash,
)]
pub struct Unreachable {
range: AstPathRange,
......
......@@ -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 {
reference: ResolvedVc<WorkerAssetReference>,
path: AstPath,
......
......@@ -178,7 +178,7 @@ format_iter!(std::fmt::Pointer);
format_iter!(std::fmt::UpperExp);
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 {
/// The ast path to the block or expression.
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