Skip to content

fix(parser): replace expect_expression panic with try_into_expression…#4923

Open
tkshsbcue wants to merge 4 commits intoboa-dev:mainfrom
tkshsbcue:fix/parser-expect-expression-to-error
Open

fix(parser): replace expect_expression panic with try_into_expression…#4923
tkshsbcue wants to merge 4 commits intoboa-dev:mainfrom
tkshsbcue:fix/parser-expect-expression-to-error

Conversation

@tkshsbcue
Copy link
Contributor

fix(parser): replace expect_expression panic with try_into_expression error handling

Summary

Replaces all uses of expect_expression() with try_into_expression() in the parser, and removes the panicking expect_expression() method. When the parser encounters arrow-function parameter list syntax in a context that requires an expression (e.g. conditional operator a ? b : c, call expression foo(), optional chaining a?.b), it now returns a recoverable parse error instead of panicking.

Motivation

The FormalParameterListOrExpression type represents the grammar ambiguity between (a, b) as a comma expression vs (a, b) as arrow-function parameters. In contexts like the conditional operator (x ? y : z), call expressions (f()), and optional chaining (a?.b), the left-hand side must be an expression—arrow params are invalid. Previously, call sites used expect_expression(), which panics with "Unexpected arrow-function arguments" when the parser produced a FormalParameterList instead of an Expression. This can occur with edge-case input such as (a) ? 1 : 2 when (a) is parsed as a single arrow param. For Boa as an embedded engine or in tooling, panics are unacceptable; the host expects a parse error. The try_into_expression() method already existed and returns Err(Error::General { ... }) with a helpful message ("invalid arrow-function arguments (parentheses around the arrow-function may help)"). Replacing expect_expression() with try_into_expression()? propagates that error instead of panicking, aligning with Boa's stability goals.

Changes

Category Description
Replaced lhs.expect_expression() with lhs.try_into_expression()? in conditional expression parser
Replaced member.expect_expression() with member.try_into_expression()? in left-hand side (call expression)
Replaced lhs.expect_expression() with lhs.try_into_expression()? in left-hand side (optional expression)
Removed expect_expression() method from FormalParameterListOrExpression to prevent future misuse

Technical Details

  • Files modified: core/parser/src/parser/expression/assignment/conditional.rs, core/parser/src/parser/expression/left_hand_side/mod.rs, core/parser/src/parser/expression/fpl_or_exp.rs
  • Lines changed: ~15 lines across 3 files
  • Behavioral impact: Invalid input that previously caused a panic now produces Err(Error::General { message: "invalid arrow-function arguments (parentheses around the arrow-function may help)", position }). Valid input is unchanged.

Testing

  • cargo test -p boa_parser — all 296 tests pass
  • cargo test -p boa_engine --lib — all 921 tests pass
  • cargo clippy — no warnings

@tkshsbcue tkshsbcue requested a review from a team as a code owner March 7, 2026 09:04
@github-actions
Copy link

github-actions bot commented Mar 7, 2026

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 49,687 49,687 0
Ignored 2,262 2,262 0
Failed 1,014 1,014 0
Panics 0 0 0
Conformance 93.81% 93.81% 0.00%

Tested main commit: 28648a3207b482955f3f93935de1b181ab72c477
Tested PR commit: 0eedab4650bc2076d31857eb933a37162e6c3431
Compare commits: 28648a3...0eedab4

@codecov
Copy link

codecov bot commented Mar 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.97%. Comparing base (6ddc2b4) to head (0eedab4).
⚠️ Report is 776 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #4923       +/-   ##
===========================================
+ Coverage   47.24%   57.97%   +10.73%     
===========================================
  Files         476      557       +81     
  Lines       46892    60931    +14039     
===========================================
+ Hits        22154    35325    +13171     
- Misses      24738    25606      +868     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tkshsbcue
Copy link
Contributor Author

@nekevss i hope you can review my PR
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant