Skip to content

Commit b211d70

Browse files
authored
[compiler] Add repros for various invariants (facebook#34099)
We received some bug reports about invariants reported by the compiler in their codebase. Adding them as repros. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34099). * facebook#34100 * __->__ facebook#34099
1 parent ba4bdb2 commit b211d70

16 files changed

+402
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {useCallback, useRef} from 'react';
6+
7+
export default function useThunkDispatch(state, dispatch, extraArg) {
8+
const stateRef = useRef(state);
9+
stateRef.current = state;
10+
11+
return useCallback(
12+
function thunk(action) {
13+
if (typeof action === 'function') {
14+
return action(thunk, () => stateRef.current, extraArg);
15+
} else {
16+
dispatch(action);
17+
return undefined;
18+
}
19+
},
20+
[dispatch, extraArg]
21+
);
22+
}
23+
24+
```
25+
26+
27+
## Error
28+
29+
```
30+
Found 1 error:
31+
32+
Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized
33+
34+
<unknown> thunk$14.
35+
36+
error.bug-infer-mutation-aliasing-effects.ts:10:22
37+
8 | function thunk(action) {
38+
9 | if (typeof action === 'function') {
39+
> 10 | return action(thunk, () => stateRef.current, extraArg);
40+
| ^^^^^ [InferMutationAliasingEffects] Expected value kind to be initialized
41+
11 | } else {
42+
12 | dispatch(action);
43+
13 | return undefined;
44+
```
45+
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {useCallback, useRef} from 'react';
2+
3+
export default function useThunkDispatch(state, dispatch, extraArg) {
4+
const stateRef = useRef(state);
5+
stateRef.current = state;
6+
7+
return useCallback(
8+
function thunk(action) {
9+
if (typeof action === 'function') {
10+
return action(thunk, () => stateRef.current, extraArg);
11+
} else {
12+
dispatch(action);
13+
return undefined;
14+
}
15+
},
16+
[dispatch, extraArg]
17+
);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
## Input
3+
4+
```javascript
5+
const YearsAndMonthsSince = () => {
6+
const diff = foo();
7+
const months = Math.floor(diff.bar());
8+
return <>{months}</>;
9+
};
10+
11+
```
12+
13+
14+
## Error
15+
16+
```
17+
Found 1 error:
18+
19+
Invariant: [Codegen] Internal error: MethodCall::property must be an unpromoted + unmemoized MemberExpression. Got a `Identifier`
20+
21+
error.bug-invariant-codegen-methodcall.ts:3:17
22+
1 | const YearsAndMonthsSince = () => {
23+
2 | const diff = foo();
24+
> 3 | const months = Math.floor(diff.bar());
25+
| ^^^^^^^^^^ [Codegen] Internal error: MethodCall::property must be an unpromoted + unmemoized MemberExpression. Got a `Identifier`
26+
4 | return <>{months}</>;
27+
5 | };
28+
6 |
29+
```
30+
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const YearsAndMonthsSince = () => {
2+
const diff = foo();
3+
const months = Math.floor(diff.bar());
4+
return <>{months}</>;
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {useEffect} from 'react';
6+
7+
export function Foo() {
8+
useEffect(() => {
9+
try {
10+
// do something
11+
} catch ({status}) {
12+
// do something
13+
}
14+
}, []);
15+
}
16+
17+
```
18+
19+
20+
## Error
21+
22+
```
23+
Found 1 error:
24+
25+
Invariant: (BuildHIR::lowerAssignment) Could not find binding for declaration.
26+
27+
error.bug-invariant-couldnt-find-binding-for-decl.ts:7:14
28+
5 | try {
29+
6 | // do something
30+
> 7 | } catch ({status}) {
31+
| ^^^^^^ (BuildHIR::lowerAssignment) Could not find binding for declaration.
32+
8 | // do something
33+
9 | }
34+
10 | }, []);
35+
```
36+
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {useEffect} from 'react';
2+
3+
export function Foo() {
4+
useEffect(() => {
5+
try {
6+
// do something
7+
} catch ({status}) {
8+
// do something
9+
}
10+
}, []);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {useMemo} from 'react';
6+
7+
export default function useFoo(text) {
8+
return useMemo(() => {
9+
try {
10+
let formattedText = '';
11+
try {
12+
formattedText = format(text);
13+
} catch {
14+
console.log('error');
15+
}
16+
return formattedText || '';
17+
} catch (e) {}
18+
}, [text]);
19+
}
20+
21+
```
22+
23+
24+
## Error
25+
26+
```
27+
Found 1 error:
28+
29+
Invariant: Expected a break target
30+
```
31+
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {useMemo} from 'react';
2+
3+
export default function useFoo(text) {
4+
return useMemo(() => {
5+
try {
6+
let formattedText = '';
7+
try {
8+
formattedText = format(text);
9+
} catch {
10+
console.log('error');
11+
}
12+
return formattedText || '';
13+
} catch (e) {}
14+
}, [text]);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {useMemo} from 'react';
6+
import {useFoo, formatB, Baz} from './lib';
7+
8+
export const Example = ({data}) => {
9+
let a;
10+
let b;
11+
12+
if (data) {
13+
({a, b} = data);
14+
}
15+
16+
const foo = useFoo(a);
17+
const bar = useMemo(() => formatB(b), [b]);
18+
19+
return <Baz foo={foo} bar={bar} />;
20+
};
21+
22+
```
23+
24+
25+
## Error
26+
27+
```
28+
Found 1 error:
29+
30+
Invariant: Expected consistent kind for destructuring
31+
32+
Other places were `Reassign` but 'mutate? #t8$46[7:9]{reactive}' is const.
33+
34+
error.bug-invariant-expected-consistent-destructuring.ts:9:9
35+
7 |
36+
8 | if (data) {
37+
> 9 | ({a, b} = data);
38+
| ^ Expected consistent kind for destructuring
39+
10 | }
40+
11 |
41+
12 | const foo = useFoo(a);
42+
```
43+
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {useMemo} from 'react';
2+
import {useFoo, formatB, Baz} from './lib';
3+
4+
export const Example = ({data}) => {
5+
let a;
6+
let b;
7+
8+
if (data) {
9+
({a, b} = data);
10+
}
11+
12+
const foo = useFoo(a);
13+
const bar = useMemo(() => formatB(b), [b]);
14+
15+
return <Baz foo={foo} bar={bar} />;
16+
};

0 commit comments

Comments
 (0)