[compiler] Keep labels on breaks out of a loop from inside a switch (…
Fixes #38545
React Compiler drops the label from break label when the break is inside a switch that is inside the labeled loop:
loop: for (const item of items) {
switch (item) {
case 'stop': break loop;
default: log.push(item);
}
}
It compiles to a bare break;, which only exits the switch, so the loop keeps running after 'stop'.
getBreakTarget in BuildReactiveFunction.ts only keeps the label for a break that targets a loop if another loop sits between the break and its target. An enclosing switch also captures an unlabeled break, so it has to count too.
That alone isn't enough: when no case falls through to the code after the switch, the switch's fallthrough is unreachable and the switch is never pushed onto the control-flow stack, so getBreakTarget can't see it. The switch is now scheduled whenever its fallthrough isn't already scheduled. The emitted label and the visited fallthrough block are unchanged.
getContinueTarget is unchanged, since a switch doesn't capture continue.
The Rust port had the same logic, so build_reactive_function.rs gets the matching change to keep the two compilers in sync and keep yarn snap --rust passing on the shared fixtures.
Snapshot changes
reactive-control-dependency-reactive-after-fixpoint:break loop;was compiled tobreak;. It is nowbreak bb0;with the label on the loop. Its eval output happened to match either way, which is why this wasn't caught.useMemo-switch-no-fallthrough: gains a redundantbreak bb0;at the end of the last case of a switch with no reachable fallthrough. Behavior is unchanged.
New fixtures
Each run with sequentialRenders so Sprout compares the original and compiled results:
labeled-break-loop-from-switch:for...of, mixed with unlabeledbreak,continue, and code after the switchlabeled-break-loop-from-switch-last-case: every case leaves the loop, withbreak loopin the last caselabeled-break-loop-from-switch-loop-kinds:while,do...while, andforlabeled-break-loop-from-nested-switch: nested switch, and anifinside a caseunlabeled-break-from-switch-in-loop: unlabeled breaks inside a switch must stay unlabeled
How did you test this change?
Without the fix (on main with the new fixture added), the fixture fails:
$ yarn snap -p labeled-break-loop-from-switch
FAIL: labeled-break-loop-from-switch
Found differences in evaluator results
Non-forget (expected): (kind: ok) ["a","|","twice","|"] ["a","|","b","|"] [] ["a","|","twice","|"]
Forget: (kind: ok) ["a","|","twice","|","|","b","|"] ["a","|","b","|"] ["|","a","|"] ["a","|","twice","|","|","b","|"]
1 Tests, 0 Passed, 1 Failed
With the fix reverted, the four labeled-break-* fixtures fail with Found differences in evaluator results on both yarn snap and yarn snap --rust, and the unlabeled-break-* control fixture passes.
With the fix, from compiler/:
$ yarn snap
1856 Tests, 1856 Passed, 0 Failed
$ yarn snap --rust
1856 Tests, 1856 Passed, 0 Failed
$ cargo check
Finished `dev` profile [unoptimized + debuginfo]
$ cargo test --workspace
all suites: test result: ok, 0 failed
$ bash scripts/test-babel-ast.sh
1821/1821 fixtures passed
$ bash scripts/test-rust-port.sh
Results: 1855 passed, 0 failed (1855 total)
BuildReactiveFunction 1746/1746
yarn workspace babel-plugin-react-compiler lint and prettier --check on the changed files pass.
Signed-off-by: Dhruv P. dhr**********@yahoo.com
Repository navigation
Commit 1b7f0e2 authored [compiler] Keep labels on breaks out of a loop from inside a switch (#38546)
1 parent 65cb72f commit 1b7f0e2 14 files changed Lines changed: 692 additions & 13 deletions File tree - compiler - crates/react_compiler_reactive_scopes/src - packages/babel-plugin-react-compiler/src - ReactiveScopes - tests/fixtures/compiler | Original file line number | Diff line number | Diff line change | | |---|---|---|---| | ||| 275 | 275 | | | 276 | 276 | | | 277 | 277 | | | 278 | | - | | | 278 | + | | | 279 | + | | | 280 | + | | 279 | 281 | | | 280 | 282 | | | 281 | 283 | | | | ||| 457 | 459 | | | 458 | 460 | | | 459 | 461 | | | 460 | | - | | 461 | | - | | | 462 | + | | | 463 | + | | 462 | 464 | | | 463 | 465 | | | 464 | 466 | | | | | Original file line number | Diff line number | Diff line change | | |---|---|---|---| | ||| 314 | 314 | | | 315 | 315 | | | 316 | 316 | | | 317 | | - | | 318 | | - | | | 317 | + | | | 318 | + | | 319 | 319 | | | 320 | 320 | | | 321 | 321 | | | | ||| 1384 | 1384 | | | 1385 | 1385 | | | 1386 | 1386 | | | 1387 | | - | | | 1387 | + | | 1388 | 1388 | | | 1389 | 1389 | | | 1390 | 1390 | | | 1391 | 1391 | | | 1392 | 1392 | | | 1393 | 1393 | | | 1394 | | - | | 1395 | | - | | | 1394 | + | | | 1395 | + | | | 1396 | + | | 1396 | 1397 | | | 1397 | | - | | | 1398 | + | | 1398 | 1399 | | | 1399 | 1400 | | | 1400 | 1401 | | | | ||| 1410 | 1411 | | | 1411 | 1412 | | | 1412 | 1413 | | | 1413 | | - | | | 1414 | + | | | 1415 | + | | 1414 | 1416 | | | 1415 | 1417 | | | 1416 | 1418 | | | | | Original file line number | Diff line number | Diff line change | | |---|---|---|---| | ||| | 1 | + | | | 2 | + | | | 3 | + | | | 4 | + | | | 5 | + | | | 6 | + | | | 7 | + | | | 8 | + | | | 9 | + | | | 10 | + | | | 11 | + | | | 12 | + | | | 13 | + | | | 14 | + | | | 15 | + | | | 16 | + | | | 17 | + | | | 18 | + | | | 19 | + | | | 20 | + | | | 21 | + | | | 22 | + | | | 23 | + | | | 24 | + | | | 25 | + | | | 26 | + | | | 27 | + | | | 28 | + | | | 29 | + | | | 30 | + | | | 31 | + | | | 32 | + | | | 33 | + | | | 34 | + | | | 35 | + | | | 36 | + | | | 37 | + | | | 38 | + | | | 39 | + | | | 40 | + | | | 41 | + | | | 42 | + | | | 43 | + | | | 44 | + | | | 45 | + | | | 46 | + | | | 47 | + | | | 48 | + | | | 49 | + | | | 50 | + | | | 51 | + | | | 52 | + | | | 53 | + | | | 54 | + | | | 55 | + | | | 56 | + | | | 57 | + | | | 58 | + | | | 59 | + | | | 60 | + | | | 61 | + | | | 62 | + | | | 63 | + | | | 64 | + | | | 65 | + | | | 66 | + | | | 67 | + | | | 68 | + | | | 69 | + | | | 70 | + | | | 71 | + | | | 72 | + | | | 73 | + | | | 74 | + | | | 75 | + | | | 76 | + | | | 77 | + | | | 78 | + | | | 79 | + | | | 80 | + | | | 81 | + | | | 82 | + | | | 83 | + | | | 84 | + | | | 85 | + | | | 86 | + | | | 87 | + | | | 88 | + | | | 89 | + | | | 90 | + | | | 91 | + | | | 92 | + | | | 93 | + | | | 94 | + | | | 95 | + | | | 96 | + | | | 97 | + | | | 98 | + | | | 99 | + | | | 100 | + | | | 101 | + | | | 102 | + | | | 103 | + | | Original file line number | Diff line number | Diff line change | | |---|---|---|---| | ||| | 1 | + | | | 2 | + | | | 3 | + | | | 4 | + | | | 5 | + | | | 6 | + | | | 7 | + | | | 8 | + | | | 9 | + | | | 10 | + | | | 11 | + | | | 12 | + | | | 13 | + | | | 14 | + | | | 15 | + | | | 16 | + | | | 17 | + | | | 18 | + | | | 19 | + | | | 20 | + | | | 21 | + | | | 22 | + | | | 23 | + | | | 24 | + | | | 25 | + | | | 26 | + | | | 27 | + | | | 28 | + | | | 29 | + | | | 30 | + | | | 31 | + | | | 32 | + | | | 33 | + | | | 34 | + | | | 35 | + | | Original file line number | Diff line number | Diff line change | | |---|---|---|---| | ||| | 1 | + | | | 2 | + | | | 3 | + | | | 4 | + | | | 5 | + | | | 6 | + | | | 7 | + | | | 8 | + | | | 9 | + | | | 10 | + | | | 11 | + | | | 12 | + | | | 13 | + | | | 14 | + | | | 15 | + | | | 16 | + | | | 17 | + | | | 18 | + | | | 19 | + | | | 20 | + | | | 21 | + | | | 22 | + | | | 23 | + | | | 24 | + | | | 25 | + | | | 26 | + | | | 27 | + | | | 28 | + | | | 29 | + | | | 30 | + | | | 31 | + | | | 32 | + | | | 33 | + | | | 34 | + | | | 35 | + | | | 36 | + | | | 37 | + | | | 38 | + | | | 39 | + | | | 40 | + | | | 41 | + | | | 42 | + | | | 43 | + | | | 44 | + | | | 45 | + | | | 46 | + | | | 47 | + | | | 48 | + | | | 49 | + | | | 50 | + | | | 51 | + | | | 52 | + | | | 53 | + | | | 54 | + | | | 55 | + | | | 56 | + | | | 57 | + | | | 58 | + | | | 59 | + | | | 60 | + | | | 61 | + | | | 62 | + | | | 63 | + | | | 64 | + | | | 65 | + | | | 66 | + | | | 67 | + | | | 68 | + | | | 69 | + | | | 70 | + | | | 71 | + | | | 72 | + | | | 73 | + | | | 74 | + | | | 75 | + | | | 76 | + | | | 77 | + | | | 78 | + | | | 79 | + | | | 80 | + | | | 81 | + | | | 82 | + | | | 83 | + | | | 84 | + | | Original file line number | Diff line number | Diff line change | | |---|---|---|---| | ||| | 1 | + | | | 2 | + | | | 3 | + | | | 4 | + | | | 5 | + | | | 6 | + | | | 7 | + | | | 8 | + | | | 9 | + | | | 10 | + | | | 11 | + | | | 12 | + | | | 13 | + | | | 14 | + | | | 15 | + | | | 16 | + | | | 17 | + | | | 18 | + | | | 19 | + | | | 20 | + | | | 21 | + | | | 22 | + | | | 23 | + | | | 24 | + | | | 25 | + | | | 26 | + | | | 27 | + | 0 commit comments
Comments
No comments yet. Start the discussion.