Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(oxc_cfg): add implicit return instruction #5568

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/oxc_cfg/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Instruction {
pub enum InstructionKind {
Unreachable,
Statement,
ImplicitReturn,
Return(ReturnInstructionKind),
Break(LabeledInstruction),
Continue(LabeledInstruction),
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_cfg/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ impl<'a> ControlFlowGraphBuilder<'a> {
self.push_instruction(InstructionKind::Statement, Some(stmt));
}

pub fn push_return(&mut self, kind: ReturnInstructionKind, node: NodeId) {
self.push_instruction(InstructionKind::Return(kind), Some(node));
pub fn push_return(&mut self, kind: ReturnInstructionKind, node: Option<NodeId>) {
self.push_instruction(InstructionKind::Return(kind), node);
}

pub fn push_implicit_return(&mut self) {
self.push_instruction(InstructionKind::ImplicitReturn, None);
}

/// Creates and push a new `BasicBlockId` onto `self.error_path` stack.
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_cfg/src/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl DisplayDot for Instruction {
InstructionKind::Return(ReturnInstructionKind::ImplicitUndefined) => {
"return <implicit undefined>"
}
InstructionKind::ImplicitReturn => "return",
InstructionKind::Return(ReturnInstructionKind::NotImplicitUndefined) => {
"return <value>"
}
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ impl GetterReturn {
match it.kind {
// Throws are classified as returning.
InstructionKind::Return(_) | InstructionKind::Throw => true,

// Ignore irrelevant elements.
InstructionKind::Break(_)
InstructionKind::ImplicitReturn
| InstructionKind::Break(_)
| InstructionKind::Continue(_)
| InstructionKind::Iteration(_)
| InstructionKind::Unreachable
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/rules/react/require_render_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fn contains_return_statement(node: &AstNode, ctx: &LintContext) -> bool {
return (FoundReturn::No, STOP_WALKING_ON_THIS_PATH);
}
InstructionKind::Return(ReturnInstructionKind::ImplicitUndefined)
| InstructionKind::ImplicitReturn
| InstructionKind::Break(_)
| InstructionKind::Continue(_)
| InstructionKind::Iteration(_)
Expand Down
24 changes: 22 additions & 2 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hash::FxHashMap;

use oxc_ast::{ast::*, AstKind, Visit};
use oxc_cfg::{
ControlFlowGraphBuilder, CtxCursor, CtxFlags, EdgeType, ErrorEdgeKind,
ControlFlowGraphBuilder, CtxCursor, CtxFlags, EdgeType, ErrorEdgeKind, InstructionKind,
IterationInstructionKind, ReturnInstructionKind,
};
use oxc_diagnostics::OxcDiagnostic;
Expand Down Expand Up @@ -1286,7 +1286,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {

/* cfg */
control_flow!(self, |cfg| {
cfg.push_return(ret_kind, node_id);
cfg.push_return(ret_kind, Some(node_id));
cfg.append_unreachable();
});
/* cfg */
Expand Down Expand Up @@ -1678,6 +1678,16 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {

/* cfg */
control_flow!(self, |cfg| {
IWANABETHATGUY marked this conversation as resolved.
Show resolved Hide resolved
let c = cfg.current_basic_block();
// If the last is an unreachable instruction, it means there is already a explicit
// return or throw statement at the end of function body, we don't need to
// insert an implicit return.
if !matches!(
Boshen marked this conversation as resolved.
Show resolved Hide resolved
c.instructions().last().map(|inst| &inst.kind),
Some(InstructionKind::Unreachable)
) {
cfg.push_implicit_return();
}
cfg.ctx(None).resolve_expect(CtxFlags::FUNCTION);
cfg.release_error_harness(error_harness);
cfg.pop_finalization_stack();
Expand Down Expand Up @@ -1730,6 +1740,16 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {

/* cfg */
control_flow!(self, |cfg| {
let c = cfg.current_basic_block();
// If the last is an unreachable instruction, it means there is already a explicit
// return or throw statement at the end of function body, we don't need to
// insert an implicit return.
if !matches!(
c.instructions().last().map(|inst| &inst.kind),
Some(InstructionKind::Unreachable)
) {
cfg.push_implicit_return();
}
cfg.ctx(None).resolve_expect(CtxFlags::FUNCTION);
cfg.release_error_harness(error_harness);
cfg.pop_finalization_stack();
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_semantic/src/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl DebugDot for Instruction {
InstructionKind::Return(ReturnInstructionKind::ImplicitUndefined) => {
"return <implicit undefined>".to_string()
}
InstructionKind::ImplicitReturn => "return".to_string(),
InstructionKind::Return(ReturnInstructionKind::NotImplicitUndefined) => {
"return <value>".to_string()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: crates/oxc_semantic/tests/integration/cfg.rs
expression: snapshot
input_file: crates/oxc_semantic/tests/integration/cfg_fixtures/argument_map.js
snapshot_kind: text
---
bb0: {

Expand All @@ -17,6 +18,7 @@ bb2: {

bb3: {
statement
return
}

bb4: {
Expand All @@ -28,7 +30,8 @@ digraph {
1 [ label = "bb1" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ExpressionStatement" shape = box]
ExpressionStatement
return" shape = box]
4 [ label = "bb4" shape = box]
1 -> 0 [ label="Error(Implicit)", style=dashed, color=red]
3 -> 2 [ label="Error(Implicit)", style=dashed, color=red]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: crates/oxc_semantic/tests/integration/cfg.rs
expression: snapshot
input_file: crates/oxc_semantic/tests/integration/cfg_fixtures/arrow_expressions.js
snapshot_kind: text
---
bb0: {

Expand All @@ -18,6 +19,7 @@ bb2: {

bb3: {
statement
return
}

bb4: {
Expand All @@ -26,6 +28,7 @@ bb4: {

bb5: {
statement
return
}

digraph {
Expand All @@ -35,10 +38,12 @@ ExpressionStatement
VariableDeclaration" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ExpressionStatement" shape = box]
ExpressionStatement
return" shape = box]
4 [ label = "bb4" shape = box]
5 [ label = "bb5
ExpressionStatement" shape = box]
ExpressionStatement
return" shape = box]
1 -> 0 [ label="Error(Implicit)", style=dashed, color=red]
3 -> 2 [ label="Error(Implicit)", style=dashed, color=red]
1 -> 3 [ label="NewFunction"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: crates/oxc_semantic/tests/integration/cfg.rs
expression: snapshot
input_file: crates/oxc_semantic/tests/integration/cfg_fixtures/class.js
snapshot_kind: text
---
bb0: {

Expand All @@ -17,6 +18,7 @@ bb2: {

bb3: {
statement
return
}

bb4: {
Expand All @@ -28,7 +30,8 @@ digraph {
1 [ label = "bb1" shape = box]
2 [ label = "bb2" shape = box]
3 [ label = "bb3
ExpressionStatement" shape = box]
ExpressionStatement
return" shape = box]
4 [ label = "bb4" shape = box]
1 -> 0 [ label="Error(Implicit)", style=dashed, color=red]
3 -> 2 [ label="Error(Implicit)", style=dashed, color=red]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: crates/oxc_semantic/tests/integration/cfg.rs
expression: snapshot
input_file: crates/oxc_semantic/tests/integration/cfg_fixtures/cond_expr_in_arrow_fn.js
snapshot_kind: text
---
bb0: {

Expand Down Expand Up @@ -32,7 +33,7 @@ bb6: {
}

bb7: {

return
}

digraph {
Expand All @@ -46,7 +47,8 @@ ExpressionStatement" shape = box]
Condition(CallExpression(a))" shape = box]
5 [ label = "bb5" shape = box]
6 [ label = "bb6" shape = box]
7 [ label = "bb7" shape = box]
7 [ label = "bb7
return" shape = box]
1 -> 0 [ label="Error(Implicit)", style=dashed, color=red]
3 -> 2 [ label="Error(Implicit)", style=dashed, color=red]
1 -> 3 [ label="NewFunction"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: crates/oxc_semantic/tests/integration/cfg.rs
expression: snapshot
input_file: crates/oxc_semantic/tests/integration/cfg_fixtures/do_while_break.js
snapshot_kind: text
---
bb0: {

Expand Down Expand Up @@ -50,7 +51,7 @@ bb10: {
}

bb11: {

return
}

bb12: {
Expand Down Expand Up @@ -78,7 +79,8 @@ unreachable" shape = box]
9 [ label = "bb9
Condition(true)" shape = box]
10 [ label = "bb10" shape = box]
11 [ label = "bb11" shape = box]
11 [ label = "bb11
return" shape = box]
12 [ label = "bb12" shape = box]
1 -> 0 [ label="Error(Implicit)", style=dashed, color=red]
3 -> 2 [ label="Error(Implicit)", style=dashed, color=red]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: crates/oxc_semantic/tests/integration/cfg.rs
expression: snapshot
input_file: crates/oxc_semantic/tests/integration/cfg_fixtures/for_in.js
snapshot_kind: text
---
bb0: {

Expand Down Expand Up @@ -34,6 +35,7 @@ bb6: {

bb7: {
statement
return
}

bb8: {
Expand All @@ -53,7 +55,8 @@ Condition(test)" shape = box]
6 [ label = "bb6
ExpressionStatement" shape = box]
7 [ label = "bb7
ExpressionStatement" shape = box]
ExpressionStatement
return" shape = box]
8 [ label = "bb8" shape = box]
1 -> 0 [ label="Error(Implicit)", style=dashed, color=red]
3 -> 2 [ label="Error(Implicit)", style=dashed, color=red]
Expand Down
Loading
Loading