Skip to content

[compiler] Support optional/logical/etc within try/catch#408

Closed
everettbu wants to merge 5 commits into
mainfrom
worktree-2026-01-22-11-41-02-orange-river
Closed

[compiler] Support optional/logical/etc within try/catch#408
everettbu wants to merge 5 commits into
mainfrom
worktree-2026-01-22-11-41-02-orange-river

Conversation

@everettbu

@everettbu everettbu commented Jan 22, 2026

Copy link
Copy Markdown

Mirror of facebook/react#35606
Original author: josephsavona


Adds support for value terminals (optional/logical/ternary/sequence) within try/catch clauses.

Try/catch expressions insert maybe-throw terminals after each instruction, but BuildReactiveFunction's value block extraction was not expecting these terminals. The fix is to roughly treat maybe-throw similarly to goto, falling through to the continuation block, but there are a few edge cases to handle.

I've also added extensive tests, including testing that errors correctly flow to the catch handler.

@everettbu everettbu added CLA Signed React Core Team Opened by a member of the React Core Team labels Jan 22, 2026
@greptile-apps

greptile-apps Bot commented Jan 22, 2026

Copy link
Copy Markdown

Greptile Overview

Greptile Summary

Added support for value terminals (optional chaining, logical expressions, ternary, sequence expressions) within try/catch blocks by handling maybe-throw terminals in the value block extraction logic.

Key changes:

  • Refactored visitValueBlock in BuildReactiveFunction.ts to handle maybe-throw terminals by treating them similarly to goto terminals, falling through to continuation blocks
  • Extracted common logic into helper methods: wrapWithSequence, extractValueBlockResult, and visitTestBlock
  • Added maybe-throw terminal support to findOptionalPlaces functions in DropManualMemoization.ts and ValidateExhaustiveDependencies.ts
  • Converted 4 previously failing error tests to passing tests
  • Added 7 new comprehensive test cases covering various combinations of value terminals within try/catch

Test coverage:
Tests validate that value blocks correctly handle error flow to catch handlers, including edge cases like nested optional chaining, logical operators combined with optional chaining, and multiple value blocks within the same try/catch.

Confidence Score: 4/5

  • Safe to merge with thorough testing - comprehensive test coverage validates the implementation
  • Well-structured refactoring with extensive test coverage demonstrating correct behavior. The core implementation is complex but follows consistent patterns. Tests validate error handling and edge cases thoroughly.
  • Pay close attention to BuildReactiveFunction.ts - the visitValueBlock logic handles multiple edge cases around maybe-throw terminals

Important Files Changed

Filename Overview
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts Major refactor to support value terminals within try/catch - adds helper methods and handles maybe-throw terminals in value blocks
compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts Adds maybe-throw terminal handling to findOptionalPlaces, following continuation block
compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateExhaustiveDependencies.ts Adds maybe-throw terminal handling to findOptionalPlaces, following continuation block
compiler/packages/babel-plugin-react-compiler/src/tests/fixtures/compiler/try-catch-optional-chaining.js New test for optional chaining within try/catch, validates error handling
compiler/packages/babel-plugin-react-compiler/src/tests/fixtures/compiler/try-catch-logical-and-optional.js New test combining logical && with optional chaining in try/catch, validates error flow

@everettbu everettbu changed the title [compiler] Support optional/logical/etc within try/catch [wip][compiler] Support optional/logical/etc within try/catch Jan 23, 2026
@everettbu
everettbu marked this pull request as draft January 23, 2026 00:31
@everettbu
everettbu force-pushed the worktree-2026-01-22-11-41-02-orange-river branch 3 times, most recently from 415755c to 037094a Compare January 26, 2026 18:17
@everettbu everettbu changed the title [wip][compiler] Support optional/logical/etc within try/catch [compiler] Support optional/logical/etc within try/catch Jan 26, 2026
@everettbu
everettbu marked this pull request as ready for review January 26, 2026 19:17

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Control-flow expressions such as logicals, optionals, and ternaries were not supported within try/catch. BuildReactiveFunction recursively traversed these terminals, and was not expecting their condition blocks to end in a maybe-throw terminal, which can occur due to the try/catch. The fix here is to treat the maybe-throw similarly to goto, passing through to the continuation block.
@everettbu
everettbu force-pushed the worktree-2026-01-22-11-41-02-orange-river branch from 8d20a6d to 257dc27 Compare January 30, 2026 21:15

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

5 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@everettbu

Copy link
Copy Markdown
Author

Upstream PR was closed or merged. Code is synced via branch mirror.

@everettbu everettbu closed this Feb 2, 2026
@everettbu
everettbu deleted the worktree-2026-01-22-11-41-02-orange-river branch February 2, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants