@samitouri / QOS-React-1 / commits / dbf2538355

[compiler] Repro for false positive mutation of a value derived from props (#35139)

Repro from the compiler WG (Thanks Cody!) of a case where the compiler incorrectly thinks a value is mutable. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/35139). * #35140 * __->__ #35139

Joseph Savona committed Nov 14, 2025 at 12:14 UTC dbf2538355c288c984a58f4d417aff2396924882
2 files changed +58
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.todo-repro-destructure-from-prop-with-default-value.expect.md new
+43
@@ -0,0 +1,43 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +export function useFormatRelativeTime(opts = {}) {
6 + const {timeZone, minimal} = opts;
7 + const format = useCallback(function formatWithUnit() {}, [minimal]);
8 + // We record `{timeZone}` as capturing timeZone into the object,
9 + // then assume that dateTimeFormat() mutates that object,
10 + // which in turn can mutate timeZone and the object it came from,
11 + // which means that the value `minimal` is derived from can change.
12 + //
13 + // The bug is that we shouldn't be recording a Capture effect given
14 + // that `opts` is known maybe-frozen. If we correctly recorded
15 + // an ImmutableCapture, this wouldn't be mistaken as mutating
16 + // opts/minimal
17 + dateTimeFormat({timeZone});
18 + return format;
19 +}
20 +
21 +```
22 +
23 +
24 +## Error
25 +
26 +```
27 +Found 1 error:
28 +
29 +Compilation Skipped: Existing memoization could not be preserved
30 +
31 +React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This dependency may be mutated later, which could cause the value to change unexpectedly.
32 +
33 +error.todo-repro-destructure-from-prop-with-default-value.ts:3:60
34 + 1 | export function useFormatRelativeTime(opts = {}) {
35 + 2 | const {timeZone, minimal} = opts;
36 +> 3 | const format = useCallback(function formatWithUnit() {}, [minimal]);
37 + | ^^^^^^^ This dependency may be modified later
38 + 4 | // We record `{timeZone}` as capturing timeZone into the object,
39 + 5 | // then assume that dateTimeFormat() mutates that object,
40 + 6 | // which in turn can mutate timeZone and the object it came from,
41 +```
42 +
43 +
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.todo-repro-destructure-from-prop-with-default-value.js new
+15
@@ -0,0 +1,15 @@
1 +export function useFormatRelativeTime(opts = {}) {
2 + const {timeZone, minimal} = opts;
3 + const format = useCallback(function formatWithUnit() {}, [minimal]);
4 + // We record `{timeZone}` as capturing timeZone into the object,
5 + // then assume that dateTimeFormat() mutates that object,
6 + // which in turn can mutate timeZone and the object it came from,
7 + // which means that the value `minimal` is derived from can change.
8 + //
9 + // The bug is that we shouldn't be recording a Capture effect given
10 + // that `opts` is known maybe-frozen. If we correctly recorded
11 + // an ImmutableCapture, this wouldn't be mistaken as mutating
12 + // opts/minimal
13 + dateTimeFormat({timeZone});
14 + return format;
15 +}