From f6472522da8a756e300ca6914d081b8ea82f4646 Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Fri, 22 Mar 2024 16:14:35 -0400 Subject: [PATCH] [repro] Fixture for incorrect lowering for break (label within loop) --- unlabeled breaks must goto the nearest iteration or switch boundary (not labeled block) --- Sprout fails with the following: ``` FAIL: bug-unlabeled-break-within-label-loop >> Unexpected error during test: Found differences in evaluator results Non-forget (expected): (kind: ok) ["0 @A","0 @B","0 @C","1 @A"] Forget: (kind: ok) ["0 @A","0 @B","0 @C","1 @A","1 @C"] ``` --- ...nlabeled-break-within-label-loop.expect.md | 62 +++++++++++++++++++ .../bug-unlabeled-break-within-label-loop.ts | 19 ++++++ .../packages/snap/src/SproutTodoFilter.ts | 1 + 3 files changed, 82 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-unlabeled-break-within-label-loop.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-unlabeled-break-within-label-loop.ts diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-unlabeled-break-within-label-loop.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-unlabeled-break-within-label-loop.expect.md new file mode 100644 index 0000000000..8348b32036 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-unlabeled-break-within-label-loop.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +function useHook(end) { + const log = []; + for (let i = 0; i < end + 1; i++) { + log.push(`${i} @A`); + bb0: { + if (i === end) { + break; + } + log.push(`${i} @B`); + } + log.push(`${i} @C`); + } + return log; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [1], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +function useHook(end) { + const $ = useMemoCache(2); + let log; + if ($[0] !== end) { + log = []; + for (let i = 0; i < end + 1; i++) { + log.push(`${i} @A`); + bb6: { + if (i === end) { + break bb6; + } + + log.push(`${i} @B`); + } + + log.push(`${i} @C`); + } + $[0] = end; + $[1] = log; + } else { + log = $[1]; + } + return log; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [1], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-unlabeled-break-within-label-loop.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-unlabeled-break-within-label-loop.ts new file mode 100644 index 0000000000..9d3ac383b9 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-unlabeled-break-within-label-loop.ts @@ -0,0 +1,19 @@ +function useHook(end) { + const log = []; + for (let i = 0; i < end + 1; i++) { + log.push(`${i} @A`); + bb0: { + if (i === end) { + break; + } + log.push(`${i} @B`); + } + log.push(`${i} @C`); + } + return log; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [1], +}; diff --git a/compiler/packages/snap/src/SproutTodoFilter.ts b/compiler/packages/snap/src/SproutTodoFilter.ts index fef339c363..f956fe10bf 100644 --- a/compiler/packages/snap/src/SproutTodoFilter.ts +++ b/compiler/packages/snap/src/SproutTodoFilter.ts @@ -534,6 +534,7 @@ const skipFilter = new Set([ // bugs "bug-reduce-reactive-deps-return-in-scope", "bug-reduce-reactive-deps-break-in-scope", + "bug-unlabeled-break-within-label-loop", // 'react-forget-runtime' not yet supported "flag-enable-emit-hook-guards",