Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler] Don't error on ref-in-render on StartMemoize #30715

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ function validateNoRefAccessInRenderImpl(
}
break;
}
case 'StartMemoize':
case 'FinishMemoize':
break;
default: {
for (const operand of eachInstructionValueOperand(instr.value)) {
validateNoRefValueAccess(errors, refAccessingFunctions, operand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,13 @@ export const FIXTURE_ENTRYPOINT = {
## Error

```
5 | const ref = useRef({inner: null});
6 |
> 7 | const onChange = useCallback(event => {
| ^^^^^^^^^^
> 8 | // The ref should still be mutable here even though function deps are frozen in
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 9 | // @enablePreserveExistingMemoizationGuarantees mode
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 10 | ref.current.inner = event.target.value;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 11 | });
| ^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef). Cannot access ref value at freeze $44:TObject<BuiltInFunction> (7:11)

InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (14:14)
12 |
13 | // The ref is modified later, extending its range and preventing memoization of onChange
14 | ref.current.inner = null;
> 14 | ref.current.inner = null;
| ^^^^^^^^^^^^^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (14:14)
15 |
16 | return <input onChange={onChange} />;
17 | }
```


Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,15 @@ export const FIXTURE_ENTRYPOINT = {
## Error

```
5 | const ref = useRef({inner: null});
6 |
> 7 | const onChange = useCallback(event => {
| ^^^^^^^^^^
> 8 | // The ref should still be mutable here even though function deps are frozen in
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 9 | // @enablePreserveExistingMemoizationGuarantees mode
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 10 | ref.current.inner = event.target.value;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 11 | });
| ^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef). Cannot access ref value at freeze $53:TObject<BuiltInFunction> (7:11)

InvalidReact: This function accesses a ref value (the `current` property), which may not be accessed during render. (https://react.dev/reference/react/useRef). Function mutate? $77[20:22]:TObject<BuiltInFunction> accesses a ref (17:17)
15 | ref.current.inner = null;
16 | };
> 17 | reset();
| ^^^^^ InvalidReact: This function accesses a ref value (the `current` property), which may not be accessed during render. (https://react.dev/reference/react/useRef). Function mutate? $77[20:22]:TObject<BuiltInFunction> accesses a ref (17:17)

InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (17:17)
12 |
13 | // The ref is modified later, extending its range and preventing memoization of onChange
14 | const reset = () => {
18 |
19 | return <input onChange={onChange} />;
20 | }
```


This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

## Input

```javascript
// @flow @validateRefAccessDuringRender @validatePreserveExistingMemoizationGuarantees
import {useCallback, useRef} from 'react';

component Foo() {
const ref = useRef();

const s = useCallback(() => {
return ref.current;
});

return <A r={s} />;
}

component A(r: mixed) {
return <div />;
}

export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import { useCallback, useRef } from "react";

function Foo() {
const $ = _c(2);
const ref = useRef();
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = () => ref.current;
$[0] = t0;
} else {
t0 = $[0];
}
const s = t0;
let t1;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
t1 = <A r={s} />;
$[1] = t1;
} else {
t1 = $[1];
}
return t1;
}

function A(t0) {
const $ = _c(1);
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t1 = <div />;
$[0] = t1;
} else {
t1 = $[0];
}
return t1;
}

export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [],
};

```

### Eval output
(kind: ok) <div></div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow @validateRefAccessDuringRender @validatePreserveExistingMemoizationGuarantees
import {useCallback, useRef} from 'react';

component Foo() {
const ref = useRef();
Expand All @@ -7,7 +8,11 @@ component Foo() {
return ref.current;
});

return <a r={s} />;
return <A r={s} />;
}

component A(r: mixed) {
return <div />;
}

export const FIXTURE_ENTRYPOINT = {
Expand Down