Skip to content

[compiler] Don't treat comparing a ref-reading function as a ref access - #37688

Merged
javache merged 3 commits into
react:mainfrom
Hmache:fix/eslint-refs-false-positive-on-function-comparison
Oct 12, 2026
Merged

javache merged 3 commits into
react:mainfrom
Hmache:fix/eslint-refs-false-positive-on-function-comparison

Conversation

@Hmache

@Hmache Hmache commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes a false positive in the react-hooks/refs validation (ValidateNoRefAccessInRender). Fixes #37521.

Comparing a function with === / !== was reported as "Cannot access refs during render" whenever that function reads a ref when called, for example a useCallback that closes over ref.current:

const sampleFunc = useCallback(() => {
  ref.current = /* ... */;
}, []);
return sampleFunc !== other ? 'different' : 'same';

A binary operator never invokes its operands, so comparing function identity cannot read .current. The BinaryExpression case validated its operands with validateNoRefValueAccess, which treats a Structure type carrying fn.readRefEffect the same as an actual ref value. This switches it to validateNoDirectRefValueAccess, which only flags a direct RefValue operand, so ref.current === x still errors while fn !== otherFn no longer does.

How did you test this change?

  • Added the regression fixture allow-comparing-ref-accessing-function-identity (input + .expect.md) reproducing the pattern from the issue; it now compiles without error and the eval result is "different".
  • Direct ref.current comparisons continue to error (existing fixtures unchanged).
  • Ran the full compiler snapshot suite with yarn snap: 1828 tests, no regressions.
  • Ported the same change to the Rust compiler (validate_no_ref_access_in_render.rs): yarn snap --rust passes all 1828 fixtures against the Rust backend, and scripts/test-rust-port.sh ValidateNoRefAccessInRender reports no TS/Rust divergence.

@meta-cla

meta-cla Bot commented Sep 24, 2026

Copy link
Copy Markdown

Hi @Hmache!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@github-actions

Copy link
Copy Markdown

A size report will appear here when the build finishes.

Generated by sizebot against dc230a9

@Hmache
Hmache force-pushed the fix/eslint-refs-false-positive-on-function-comparison branch from dc230a9 to d1c61f7 Compare September 24, 2026 14:37
@meta-cla meta-cla Bot added the CLA Signed label Sep 24, 2026
Refs react#37521

The `react-hooks/refs` validation (ValidateNoRefAccessInRender) flagged
`fn !== otherFn` as "Cannot access refs during render" whenever `fn` was a
function that reads a ref when called (e.g. a useCallback closing over
ref.current). A `===`/`!==` comparison never invokes its operands, so
merely comparing function identity cannot read `.current`.

The BinaryExpression case validated its operands with
validateNoRefValueAccess, which treats a Structure type with
fn.readRefEffect the same as an actual ref value. Switch to
validateNoDirectRefValueAccess, which only flags a direct RefValue
operand (e.g. `ref.current === x`), matching the fact that a binary
operator cannot call a function operand.

Added a regression fixture
(allow-comparing-ref-accessing-function-identity) confirming the
pattern from the issue now compiles without error, while direct
ref.current comparisons continue to correctly error. Ran the full
`yarn snap` suite (1828 tests) with no regressions.
@Hmache
Hmache force-pushed the fix/eslint-refs-false-positive-on-function-comparison branch from d1c61f7 to 111fdfd Compare September 24, 2026 14:43
@Hmache Hmache changed the title Fix/eslint refs false positive on function comparison [compiler] Don't treat comparing a ref-reading function as a ref access Sep 25, 2026
@javache

javache commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Can you make sure the Rust version of the compiler has the same semantics?

Hmache and others added 2 commits October 8, 2026 22:22
Port the ValidateNoRefAccessInRender change from the TypeScript compiler to
the Rust port. The BinaryExpression case validated its operands with
validate_no_ref_value_access, which treats a Structure type with
read_ref_effect the same as an actual ref value, so `fn !== otherFn` was
flagged as "Cannot access refs during render" whenever `fn` reads a ref when
called. A `===`/`!==` comparison never invokes its operands, so switch to
validate_no_direct_ref_value_access, which only flags a direct RefValue
operand (e.g. `ref.current === x`).

Without this, the allow-comparing-ref-accessing-function-identity fixture
fails under `yarn snap --rust`. With it, `yarn snap --rust` passes all 1828
fixtures and `scripts/test-rust-port.sh ValidateNoRefAccessInRender` reports
no TS/Rust divergence.
@Hmache

Hmache commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Can you make sure the Rust version of the compiler has the same semantics?

ah right, forgot the Rust port runs the same fixture set, pushed a50760f with the same change in validate_no_ref_access_in_render.rs, the BinaryExpression arm now uses the direct ref value check like the TS side

@javache
javache merged commit 2f5370a into react:main Oct 12, 2026
19 checks passed
eps1lon added a commit to vercel/next.js that referenced this pull request Oct 14, 2026
[diff
react/react@b618bbb4...07dde64d](react/react@b618bbb...07dde64)

<details>
<summary>React upstream changes</summary>

- react/react#37710
- react/react#37814
- react/react#37778
- react/react#37688
- react/react#37796

</details>

---------

Co-authored-by: next-js-bot[bot] <279046576+next-js-bot[bot]@users.noreply.github.com>
Co-authored-by: Sebastian Sebbie Silbermann <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: ESLint flags just refering (not calling) functions that use refs as accessing refs (react-hooks/refs)

2 participants