Skip to content

fix(semantic): consider update expressions in implicit returns as reads #5158

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

Closed
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
2 changes: 2 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,8 @@ pub struct ArrowFunctionExpression<'a> {
#[serde(flatten)]
pub span: Span,
/// Is the function body an arrow expression? i.e. `() => expr` instead of `() => {}`
///
/// When `true`, this function implicitly returns the expression.
pub expression: bool,
pub r#async: bool,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'s, 'a> Symbol<'s, 'a> {
let Some(AstKind::FunctionBody(body)) = self.nodes().parent_kind(parent.id()) else {
return false;
};
return body.span.contains_inclusive(self.span()) && body.statements.len() == 1 && !self.get_snippet(body.span).starts_with('{')
return self.is_implicit_return(body);
}
_ => {
parent.kind().debug_name();
Expand Down Expand Up @@ -103,6 +103,10 @@ impl<'s, 'a> Symbol<'s, 'a> {
.map(|node_id| nodes.get_node(node_id))
.any(|node| matches!(node.kind(), AstKind::TSModuleDeclaration(namespace) if is_ambient_namespace(namespace)))
}

pub fn is_implicit_return(&self, body: &FunctionBody) -> bool {
body.statements.len() == 1 && !self.get_snippet(body.span).starts_with('{')
}
}

#[inline]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ fn test_vars_reassignment() {
",
"let i = 0; i > 0 ? 'positive' : 'negative';",
"let i = 0; i > 0 && console.log('positive');",
"export function counter() { let i = 0; return () => ++i }",
];

let fail = vec![
Expand Down
13 changes: 13 additions & 0 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct SemanticBuilder<'a> {
// and when we reach a value declaration we set it
// to value like
pub(crate) namespace_stack: Vec<SymbolId>,
pub(crate) is_in_return: bool,
current_reference_flags: ReferenceFlags,
pub(crate) hoisting_variables: FxHashMap<ScopeId, FxHashMap<Atom<'a>, SymbolId>>,

Expand Down Expand Up @@ -134,6 +135,7 @@ impl<'a> SemanticBuilder<'a> {
current_scope_id,
function_stack: vec![],
namespace_stack: vec![],
is_in_return: false,
nodes: AstNodes::default(),
hoisting_variables: FxHashMap::default(),
scope,
Expand Down Expand Up @@ -1229,7 +1231,10 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
/* cfg */

let ret_kind = if let Some(arg) = &stmt.argument {
let in_return = self.is_in_return;
self.is_in_return = true;
self.visit_expression(arg);
self.is_in_return = in_return;
Comment on lines +1234 to +1237
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause a bit of difficulty in maintaining. We should avoid writing any logic in the visit function.

ReturnInstructionKind::NotImplicitUndefined
} else {
ReturnInstructionKind::ImplicitUndefined
Expand Down Expand Up @@ -1624,7 +1629,10 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
self.visit_ts_type_annotation(return_type);
}
if let Some(body) = &func.body {
let in_return = self.is_in_return;
self.is_in_return = false;
self.visit_function_body(body);
self.is_in_return = in_return;
}

/* cfg */
Expand Down Expand Up @@ -1677,7 +1685,10 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
self.visit_ts_type_annotation(return_type);
}

let in_return = self.is_in_return;
self.is_in_return = expr.expression;
self.visit_function_body(&expr.body);
self.is_in_return = in_return;

/* cfg */
control_flow!(self, |cfg| {
Expand Down Expand Up @@ -1990,6 +2001,8 @@ impl<'a> SemanticBuilder<'a> {
fn resolve_reference_usages(&self) -> ReferenceFlags {
if self.current_reference_flags.is_empty() {
ReferenceFlags::Read
} else if self.is_in_return {
self.current_reference_flags | ReferenceFlags::Read
} else {
self.current_reference_flags
}
Expand Down
48 changes: 48 additions & 0 deletions crates/oxc_semantic/tests/integration/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,51 @@ fn test_ts_interface_heritage() {
.has_number_of_references(1)
.test();
}

#[test]
fn test_arrow_implicit_return() {
SemanticTester::js("let i = 0; const x = () => i")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(0)
.test();

SemanticTester::js("let i = 0; const x = () => ++i")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();

SemanticTester::js("let i = 0; const x = () => { ++i }")
.has_root_symbol("i")
.has_number_of_reads(0)
.has_number_of_writes(1)
.test();

SemanticTester::js("let i = 0; const x = () => (0, ++i)")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();

SemanticTester::js("let i = 0; const x = () => (++i, 0)")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();
Comment on lines +385 to +389
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test correct? I don't think i is being read from here (but see my general comment - I'm not sure).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, I am not sure what i ReferenceFlags should be. The x function return result is 0 when it is called, so i should be write only. I think we can try to make i only contain write if no tests are broken

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to get to the bottom of it! #5165

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's not, but there are other test cases that enforce this behavior. I mentioned this to you (@Dunqing) a while ago, when you made that change, and your rationale was that "we don't know what's a read and what's not a read, since things could be proxies and/or getters/setters"

}

#[test]
fn test_arrow_explicit_return() {
SemanticTester::js("let i = 0; const x = () => { return i }")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(0)
.test();

SemanticTester::js("let i = 0; const x = () => { return ++i }")
.has_root_symbol("i")
.has_number_of_reads(1)
.has_number_of_writes(1)
.test();
}
12 changes: 6 additions & 6 deletions tasks/coverage/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ after transform: ["t"]
rebuilt : []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/arrow-function/arrow-like-in-conditional-3/input.ts
semantic error: Unresolved references mismatch:
after transform: ["v"]
rebuilt : []
semantic error: Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1)]
rebuilt : SymbolId(0): [ReferenceId(0)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/arrow-function/arrow-like-in-conditional-4/input.ts
semantic error: Unresolved references mismatch:
after transform: ["v"]
rebuilt : []
semantic error: Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(0)]
rebuilt : SymbolId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/arrow-function/async-await-null/input.ts
semantic error: Bindings mismatch:
Expand Down
6 changes: 3 additions & 3 deletions tasks/coverage/semantic_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10884,9 +10884,9 @@ after transform: ScopeId(1): ["T", "i", "o"]
rebuilt : ScopeId(1): ["i", "o"]

tasks/coverage/typescript/tests/cases/compiler/inferenceLimit.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["BrokenClass", "MyModule"]
rebuilt : ScopeId(0): ["BrokenClass"]
semantic error: Symbol reference IDs mismatch:
after transform: SymbolId(0): [ReferenceId(2), ReferenceId(13), ReferenceId(17)]
rebuilt : SymbolId(0): []
Unresolved references mismatch:
after transform: ["Array", "Promise"]
rebuilt : ["Promise"]
Expand Down
3 changes: 2 additions & 1 deletion tasks/coverage/transformer_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ commit: d8086f14

transformer_typescript Summary:
AST Parsed : 6456/6456 (100.00%)
Positive Passed: 6455/6456 (99.98%)
Positive Passed: 6454/6456 (99.97%)
Mismatch: tasks/coverage/typescript/tests/cases/compiler/inferenceLimit.ts
Mismatch: tasks/coverage/typescript/tests/cases/conformance/jsx/inline/inlineJsxAndJsxFragPragmaOverridesCompilerOptions.tsx