Skip to content

Commit 3d40711

Browse files
committed
fix: fix span for (x)=>{} expression; fix crash
If an arrow function expression has parentheses, we are calculating the begin span for the expression incorrectly. This leads to an assertion failure with the following code: // expression.h:438: internal check failed in arrow_function: !this->children_.empty() ((x)) => { } Fix the assertion failure by keeping track of where the first '(' is.
1 parent 51a2ba7 commit 3d40711

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Semantic Versioning.
3131
no longer falsely interpreted as an async function. It is instead interpreted
3232
as the use of a variable called `async` followed by a non-async function, per
3333
the language standard.
34+
* `((x)) => {}` no longer crashes the parser with an assertion failure.
3435

3536
## 2.5.0 (2022-05-23)
3637

src/parse-expression.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,14 +1573,15 @@ void parser::parse_arrow_function_expression_remainder(
15731573
expression* lhs = binary_builder.last_expression();
15741574
function_attributes attributes = function_attributes::normal;
15751575

1576+
const char8* left_paren_begin = nullptr;
15761577
if (lhs->kind() == expression_kind::paren) {
1578+
left_paren_begin = lhs->span().begin();
15771579
lhs = static_cast<expression::paren*>(lhs)->child_;
15781580
}
15791581

15801582
expression_arena::vector<expression*> parameters(
15811583
"parse_arrow_function_expression_remainder",
15821584
this->expressions_.allocator());
1583-
const char8* left_paren_begin = nullptr;
15841585
switch (lhs->kind()) {
15851586
case expression_kind::binary_operator:
15861587
case expression_kind::trailing_comma:

test/test-parse-expression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2928,7 +2928,7 @@ TEST_F(test_parse_expression, arrow_function) {
29282928
EXPECT_EQ(ast->attributes(), function_attributes::normal);
29292929
EXPECT_EQ(ast->child_count(), 1);
29302930
EXPECT_EQ(summarize(ast->child(0)), "var a");
2931-
// TODO(strager): Implement begin_offset.
2931+
EXPECT_EQ(p.range(ast).begin_offset(), 0);
29322932
EXPECT_EQ(p.range(ast).end_offset(), 8);
29332933
EXPECT_THAT(p.errors(), IsEmpty());
29342934
}

0 commit comments

Comments
 (0)