Skip to content

Commit dbf2538

Browse files
authored
[compiler] Repro for false positive mutation of a value derived from props (facebook#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). * facebook#35140 * __->__ facebook#35139
1 parent 21f2824 commit dbf2538

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)