Skip to content

Commit faf73eb

Browse files
committed
[compiler] Fix invariant error for optional chaining in try/catch
Optional chaining and other value blocks within try/catch blocks were throwing an Invariant error ("Unexpected terminal in optional") instead of the expected Todo error. This caused hard failures instead of graceful bailouts. The issue occurred because DropManualMemoization and ValidateExhaustiveDependencies ran before BuildReactiveFunction, and encountered `maybe-throw` terminals in their switch statements without a handler, falling through to the invariant case. Fixed by adding explicit `maybe-throw` cases in both files that throw the proper Todo error with the message "Support value blocks (conditional, logical, optional chaining, etc) within a try/catch statement". Also renamed the existing bug test to reflect it's now correctly handled: - error.bug-invariant-unexpected-terminal-in-optional → error.todo-optional-chaining-within-try-catch Closes #35570
1 parent 9cf4daf commit faf73eb

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,14 @@ function findOptionalPlaces(fn: HIRFunction): Set<IdentifierId> {
583583
testBlock = fn.body.blocks.get(terminal.fallthrough)!;
584584
break;
585585
}
586+
case 'maybe-throw': {
587+
CompilerError.throwTodo({
588+
reason: `Support value blocks (conditional, logical, optional chaining, etc) within a try/catch statement`,
589+
description: null,
590+
loc: terminal.loc,
591+
suggestions: null,
592+
});
593+
}
586594
default: {
587595
CompilerError.invariant(false, {
588596
reason: `Unexpected terminal in optional`,

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateExhaustiveDependencies.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,14 @@ export function findOptionalPlaces(
10161016
testBlock = fn.body.blocks.get(terminal.block)!;
10171017
break;
10181018
}
1019+
case 'maybe-throw': {
1020+
CompilerError.throwTodo({
1021+
reason: `Support value blocks (conditional, logical, optional chaining, etc) within a try/catch statement`,
1022+
description: null,
1023+
loc: terminal.loc,
1024+
suggestions: null,
1025+
});
1026+
}
10191027
default: {
10201028
CompilerError.simpleInvariant(false, {
10211029
reason: `Unexpected terminal in optional`,

0 commit comments

Comments
 (0)