Unverified Commit 1633c4ac authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

fix(turbopack): Consider break label as conditional (#77953)

### What?

Fix the ES analyzer, for the case below.

```js
import { assert } from "./assert";

function runGetClassesOrUseCache() {
  use_cache: {
    assert(1, 1);
    if (true) {
      break use_cache;
    }
    return "value a";
  }
  {
    assert(2, 2);
  }

  assert(3, 3);

  return "value b";
}

```

### Why?

### How?

Fixes #77083 
Closes PACK-4258

---------
Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent e04b09a6
...@@ -69,6 +69,8 @@ pub enum ConditionalKind { ...@@ -69,6 +69,8 @@ pub enum ConditionalKind {
Or { expr: Box<EffectsBlock> }, Or { expr: Box<EffectsBlock> },
/// The expression on the right side of the `??` operator. /// The expression on the right side of the `??` operator.
NullishCoalescing { expr: Box<EffectsBlock> }, NullishCoalescing { expr: Box<EffectsBlock> },
/// The expression on the right side of a labeled statement.
Labeled { body: Box<EffectsBlock> },
} }
impl ConditionalKind { impl ConditionalKind {
...@@ -100,6 +102,11 @@ impl ConditionalKind { ...@@ -100,6 +102,11 @@ impl ConditionalKind {
} }
} }
} }
ConditionalKind::Labeled { body } => {
for effect in &mut body.effects {
effect.normalize();
}
}
} }
} }
} }
...@@ -2077,6 +2084,10 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -2077,6 +2084,10 @@ impl VisitAstPath for Analyzer<'_> {
AstParentNodeRef::TryStmt(_, TryStmtField::Handler), AstParentNodeRef::TryStmt(_, TryStmtField::Handler),
AstParentNodeRef::CatchClause(_, CatchClauseField::Body) AstParentNodeRef::CatchClause(_, CatchClauseField::Body)
] ]
| [
AstParentNodeRef::LabeledStmt(_, LabeledStmtField::Body),
AstParentNodeRef::Stmt(_, StmtField::Block)
]
) { ) {
None None
} else if matches!( } else if matches!(
...@@ -2138,6 +2149,40 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -2138,6 +2149,40 @@ impl VisitAstPath for Analyzer<'_> {
n.visit_children_with_ast_path(self, ast_path); n.visit_children_with_ast_path(self, ast_path);
} }
fn visit_labeled_stmt<'ast: 'r, 'r>(
&mut self,
stmt: &'ast LabeledStmt,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
let mut prev_effects = take(&mut self.effects);
let prev_early_return_stack = take(&mut self.early_return_stack);
stmt.visit_children_with_ast_path(self, ast_path);
self.end_early_return_block();
let effects = take(&mut self.effects);
prev_effects.push(Effect::Conditional {
condition: Box::new(JsValue::unknown_empty(true, "labeled statement")),
kind: Box::new(ConditionalKind::Labeled {
body: Box::new(EffectsBlock {
effects,
range: AstPathRange::Exact(as_parent_path_with(
ast_path,
AstParentKind::LabeledStmt(LabeledStmtField::Body),
)),
}),
}),
ast_path: as_parent_path(ast_path),
span: stmt.span,
in_try: is_in_try(ast_path),
});
self.effects = prev_effects;
self.early_return_stack = prev_early_return_stack;
}
} }
impl Analyzer<'_> { impl Analyzer<'_> {
...@@ -2252,6 +2297,9 @@ impl Analyzer<'_> { ...@@ -2252,6 +2297,9 @@ impl Analyzer<'_> {
| ConditionalKind::NullishCoalescing { expr } => { | ConditionalKind::NullishCoalescing { expr } => {
self.effects.append(&mut expr.effects); self.effects.append(&mut expr.effects);
} }
ConditionalKind::Labeled { body } => {
self.effects.append(&mut body.effects);
}
} }
} else { } else {
self.add_effect(Effect::Conditional { self.add_effect(Effect::Conditional {
......
...@@ -4311,7 +4311,8 @@ mod tests { ...@@ -4311,7 +4311,8 @@ mod tests {
} }
ConditionalKind::And { expr } ConditionalKind::And { expr }
| ConditionalKind::Or { expr } | ConditionalKind::Or { expr }
| ConditionalKind::NullishCoalescing { expr } => { | ConditionalKind::NullishCoalescing { expr }
| ConditionalKind::Labeled { body: expr } => {
queue queue
.extend(expr.effects.into_iter().rev().map(|e| (i, e))); .extend(expr.effects.into_iter().rev().map(|e| (i, e)));
} }
......
...@@ -1216,6 +1216,9 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -1216,6 +1216,9 @@ pub(crate) async fn analyse_ecmascript_module_internal(
} }
} }
} }
ConditionalKind::Labeled { body } => {
active!(body);
}
} }
} }
Effect::Call { Effect::Call {
......
[
(
"runGetClassesOrUseCache",
Function(
4,
52,
Alternatives {
total_nodes: 3,
values: [
Constant(
Str(
Atom(
"value a",
),
),
),
Constant(
Str(
Atom(
"value b",
),
),
),
],
logical_property: None,
},
),
),
]
// app/page.js
import { assert } from "./assert";
function runGetClassesOrUseCache() {
use_cache: {
assert(1, 1);
if (true) {
break use_cache;
}
return "value a";
}
{
assert(2, 2);
}
assert(3, 3);
return "value b";
}
\ No newline at end of file
0 -> 1 conditional = ???*0*
- *0* labeled statement
⚠️ This value might have side effects
1 -> 3 call = module<./assert, {}>["assert"](1, 1)
1 -> 4 unreachable = ???*0*
- *0* unreachable
⚠️ This value might have side effects
0 -> 6 call = module<./assert, {}>["assert"](2, 2)
0 -> 8 call = module<./assert, {}>["assert"](3, 3)
0 -> 9 unreachable = ???*0*
- *0* unreachable
⚠️ This value might have side effects
// app/assert.js
export function assert(a, b) {
if (a !== b) {
throw new Error(`Assertion failed: ${a} !== ${b}`);
}
}
\ No newline at end of file
// app/page.js
import { assert } from "./assert";
function runGetClassesOrUseCache() {
use_cache: {
assert(1, 1);
if (true) {
break use_cache;
}
return "value a";
}
{
assert(2, 2);
}
assert(3, 3);
return "value b";
}
it('should handle break label in use_cache', () => {
expect(runGetClassesOrUseCache()).toBe("value b");
})
\ No newline at end of file
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