Skip to content

[compiler] Type react-native PanResponder.create to fix react-hooks/refs false positive - #37970

Merged
javache merged 2 commits into
react:mainfrom
Gourav224:fix/refs-callback-passed-to-call
Oct 26, 2026
Merged

javache merged 2 commits into
react:mainfrom
Gourav224:fix/refs-callback-passed-to-call

Conversation

@Gourav224

@Gourav224 Gourav224 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #37955

Depends on #37975 (retains declared Freeze effects for already-frozen values, as suggested by @javache). Until that merges this PR's diff also shows its commit.

Summary

react-hooks/refs reports "Passing a ref to a function may read its value during render" for callbacks passed to React Native's PanResponder.create({...}), because the compiler treats PanResponder imported from react-native as an unknown function that may call its arguments during render. PanResponder.create() only stores the handlers and invokes them later from gesture events.

The compiler already has a signature for exactly this (PanResponder.create freezes its config and doesn't call it during render), but it only exists in the test-only shared-runtime type provider in snap, so real code never gets it. This adds the same signature for react-native to defaultModuleTypeProvider (TypeScript, and the Rust port in default_module_type_provider.rs), so the callbacks are treated as deferred, like the IntersectionObserver fix in #37764.

Other unknown functions still report errors when given ref-accessing callbacks.

How did you test this change?

Added a valid case to packages/eslint-plugin-react-hooks/__tests__/ReactCompilerRuleTypescript-test.ts (the repro shape from the issue). Snap fixtures can't cover this because snap replaces the module type provider with its own.

  • Without the fix: cd packages/eslint-plugin-react-hooks && npx jest ReactCompilerRuleTypescript → 1 failed, 15 passed (the new case).
  • With the fix: yarn build:compiler && npx jest in packages/eslint-plugin-react-hooks → 4 suites passed, 5087 tests passed.
  • yarn workspace babel-plugin-react-compiler tsc --noEmit and lint (in compiler/) pass; yarn lint at the root passes; prettier check passes on the changed TS files.
  • cargo check -p react_compiler_hir and cargo fmt --check pass for the Rust change (I did not run the Rust test suite).
  • yarn snap in compiler/: identical results with and without this change (1812 passed, 20 failed; the failures are pre-existing, mostly fbt fixtures in my local setup).
  • Also added a memoized-config case (useMemo config passed to PanResponder.create); with [compiler] Retain declared Freeze effects on already-frozen values for ref validation #37975 the eslint-plugin suite passes (5088 tests).
  • yarn flow not run: the change touches no Flow-typed code.

🤖 Generated with Claude Code

@meta-cla

meta-cla Bot commented Oct 23, 2026

Copy link
Copy Markdown

Hi @Gourav224!

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

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Comparing: a6b912f...d91f7f3

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 7.35 kB 7.35 kB +0.05% 1.96 kB 1.96 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 626.13 kB 626.13 kB = 110.64 kB 110.64 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 7.36 kB 7.36 kB = 1.96 kB 1.96 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 697.35 kB 697.35 kB = 122.08 kB 122.08 kB
facebook-www/ReactDOM-prod.classic.js = 715.38 kB 715.38 kB = 125.15 kB 125.15 kB
facebook-www/ReactDOM-prod.modern.js = 705.70 kB 705.70 kB = 123.58 kB 123.58 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by sizebot against d91f7f3

@meta-cla

meta-cla Bot commented Oct 23, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Oct 23, 2026
@meta-cla

meta-cla Bot commented Oct 23, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@javache

javache commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

There's a problem here when config is a previously memoized value:

const ref = useRef(0);
const config = useMemo(
  () => ({
    onPanResponderRelease: () => {
      ref.current = 0;
    },
  }),
  [],
);

const responder = PanResponder.create(config);

Could you send a separate PR first to fix how inference handles an explicitly declared Freeze effect when the argument is already frozen? The PanResponder change can then build on that fix.

Suggested prerequisite fix

In compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts, preserve the declared effect independently of whether it changes abstract state:

 case 'Freeze': {
-  const didFreeze = state.freeze(effect.value, effect.reason);
-  if (didFreeze) {
-    effects.push(effect);
-  }
+  state.freeze(effect.value, effect.reason);
+  // Ref validation needs the declared effect even if already frozen.
+  effects.push(effect);
   break;
 }

Apply the equivalent change in the Rust implementation.

This preserves validation metadata; it does not introduce runtime freezing or weaken the immutable-config contract.

@Gourav224

Copy link
Copy Markdown
Contributor Author

Thanks @javache, good catch. I opened #37975 as the prerequisite (retains the declared Freeze effect for already-frozen values, in TS and Rust). Applying the diff verbatim regressed 11 fixtures, because the retained effect also sets operand.effect === Freeze, which ValidateNoFreezingKnownMutableFunctions reads (e.g. the valid allow-mutate-global-in-effect-fixpoint), so #37975 marks it alreadyFrozen and only ref validation sees it; details are in that PR. This PR is now rebased on it and has your useMemo example as a test.

javache pushed a commit that referenced this pull request Oct 23, 2026
…r ref validation (#37975)

## Summary

When an operand is already frozen (e.g. a memoized value), `applyEffect`
drops a declared `Freeze` effect because nothing new gets frozen. Ref
validation reads the declared `Freeze` effects of known signatures, so
it misses them in this case. For example, with `PanResponder.create`'s
signature:

```js
const ref = useRef(0);
const config = useMemo(() => ({onPanResponderRelease: () => { ref.current = 0; }}), []);
const responder = PanResponder.create(config); // false positive: ref access reported
```

This keeps the declared effect on the instruction when the value is
already frozen, as suggested by @javache in #37970. This is metadata
only: nothing extra is frozen.

**Note:** the literal diff from review (always `effects.push(effect)`)
regressed 11 snapshot fixtures. A retained `Freeze` sets `operand.effect
=== Effect.Freeze` in `InferMutationAliasingRanges`, which
`ValidateNoFreezingKnownMutableFunctions` reads, so it started reporting
an extra "Cannot modify local variables after render completes" error on
already-frozen operands. It also made the valid
`allow-mutate-global-in-effect-fixpoint` fixture fail, and added a
duplicate error to e.g. `error.assign-ref-in-effect-hint`,
`error.invalid-mutate-hook-value-in-intersection-observer` and
`error.todo-reassign-const`.

So this PR keeps the effect for ref validation only: the retained effect
is marked `alreadyFrozen`, and `InferMutationAliasingRanges` skips it
when computing operand effects. All other passes see what they saw
before. The same change is made in the Rust port (`already_frozen`).

## How did you test this change?

- Added `panresponder-ref-in-memoized-config.js` (the `useMemo` example
above, using the `shared-runtime` `PanResponder.create` signature). It
fails with "Cannot access refs during render" without this change and
passes with it.
- `yarn snap` in `compiler/`: the failure list is identical to `main`
(the same 20 pre-existing failures, mostly fbt fixtures in my local
setup); no snapshots changed other than the new fixture.
- `yarn workspace babel-plugin-react-compiler tsc --noEmit` and `lint`,
prettier: pass.
- `cargo fmt --all`, `cargo check --workspace`, and `cargo test` for
`react_compiler_hir`, `react_compiler_inference` and
`react_compiler_validation`: pass. I did not run the Rust fixture
comparison against snap.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
github-actions Bot pushed a commit that referenced this pull request Oct 23, 2026
…r ref validation (#37975)

## Summary

When an operand is already frozen (e.g. a memoized value), `applyEffect`
drops a declared `Freeze` effect because nothing new gets frozen. Ref
validation reads the declared `Freeze` effects of known signatures, so
it misses them in this case. For example, with `PanResponder.create`'s
signature:

```js
const ref = useRef(0);
const config = useMemo(() => ({onPanResponderRelease: () => { ref.current = 0; }}), []);
const responder = PanResponder.create(config); // false positive: ref access reported
```

This keeps the declared effect on the instruction when the value is
already frozen, as suggested by @javache in #37970. This is metadata
only: nothing extra is frozen.

**Note:** the literal diff from review (always `effects.push(effect)`)
regressed 11 snapshot fixtures. A retained `Freeze` sets `operand.effect
=== Effect.Freeze` in `InferMutationAliasingRanges`, which
`ValidateNoFreezingKnownMutableFunctions` reads, so it started reporting
an extra "Cannot modify local variables after render completes" error on
already-frozen operands. It also made the valid
`allow-mutate-global-in-effect-fixpoint` fixture fail, and added a
duplicate error to e.g. `error.assign-ref-in-effect-hint`,
`error.invalid-mutate-hook-value-in-intersection-observer` and
`error.todo-reassign-const`.

So this PR keeps the effect for ref validation only: the retained effect
is marked `alreadyFrozen`, and `InferMutationAliasingRanges` skips it
when computing operand effects. All other passes see what they saw
before. The same change is made in the Rust port (`already_frozen`).

## How did you test this change?

- Added `panresponder-ref-in-memoized-config.js` (the `useMemo` example
above, using the `shared-runtime` `PanResponder.create` signature). It
fails with "Cannot access refs during render" without this change and
passes with it.
- `yarn snap` in `compiler/`: the failure list is identical to `main`
(the same 20 pre-existing failures, mostly fbt fixtures in my local
setup); no snapshots changed other than the new fixture.
- `yarn workspace babel-plugin-react-compiler tsc --noEmit` and `lint`,
prettier: pass.
- `cargo fmt --all`, `cargo check --workspace`, and `cargo test` for
`react_compiler_hir`, `react_compiler_inference` and
`react_compiler_validation`: pass. I did not run the Rust fixture
comparison against snap.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Sonnet 5.5 <[email protected]>

DiffTrain build for [a6b912f](a6b912f)
github-actions Bot pushed a commit that referenced this pull request Oct 23, 2026
…r ref validation (#37975)

## Summary

When an operand is already frozen (e.g. a memoized value), `applyEffect`
drops a declared `Freeze` effect because nothing new gets frozen. Ref
validation reads the declared `Freeze` effects of known signatures, so
it misses them in this case. For example, with `PanResponder.create`'s
signature:

```js
const ref = useRef(0);
const config = useMemo(() => ({onPanResponderRelease: () => { ref.current = 0; }}), []);
const responder = PanResponder.create(config); // false positive: ref access reported
```

This keeps the declared effect on the instruction when the value is
already frozen, as suggested by @javache in #37970. This is metadata
only: nothing extra is frozen.

**Note:** the literal diff from review (always `effects.push(effect)`)
regressed 11 snapshot fixtures. A retained `Freeze` sets `operand.effect
=== Effect.Freeze` in `InferMutationAliasingRanges`, which
`ValidateNoFreezingKnownMutableFunctions` reads, so it started reporting
an extra "Cannot modify local variables after render completes" error on
already-frozen operands. It also made the valid
`allow-mutate-global-in-effect-fixpoint` fixture fail, and added a
duplicate error to e.g. `error.assign-ref-in-effect-hint`,
`error.invalid-mutate-hook-value-in-intersection-observer` and
`error.todo-reassign-const`.

So this PR keeps the effect for ref validation only: the retained effect
is marked `alreadyFrozen`, and `InferMutationAliasingRanges` skips it
when computing operand effects. All other passes see what they saw
before. The same change is made in the Rust port (`already_frozen`).

## How did you test this change?

- Added `panresponder-ref-in-memoized-config.js` (the `useMemo` example
above, using the `shared-runtime` `PanResponder.create` signature). It
fails with "Cannot access refs during render" without this change and
passes with it.
- `yarn snap` in `compiler/`: the failure list is identical to `main`
(the same 20 pre-existing failures, mostly fbt fixtures in my local
setup); no snapshots changed other than the new fixture.
- `yarn workspace babel-plugin-react-compiler tsc --noEmit` and `lint`,
prettier: pass.
- `cargo fmt --all`, `cargo check --workspace`, and `cargo test` for
`react_compiler_hir`, `react_compiler_inference` and
`react_compiler_validation`: pass. I did not run the Rust fixture
comparison against snap.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Sonnet 5.5 <[email protected]>

DiffTrain build for [a6b912f](a6b912f)
@Gourav224
Gourav224 force-pushed the fix/refs-callback-passed-to-call branch from ba155f9 to d91f7f3 Compare October 24, 2026 04:11
@Gourav224

Copy link
Copy Markdown
Contributor Author

Rebased on main now that #37975 has landed. The diff now contains only the PanResponder.create typing (TS and Rust) and two eslint-plugin cases, including your memoized-config example. With the new typing that example compiles cleanly; without it, it fails with "Cannot access refs during render". Reading ref.current during render is still reported.

@javache
javache merged commit 9ca7a27 into react:main Oct 26, 2026
253 checks passed
github-actions Bot pushed a commit that referenced this pull request Oct 26, 2026
…efs false positive (#37970)

Fixes #37955

**Depends on #37975** (retains declared `Freeze` effects for
already-frozen values, as suggested by @javache). Until that merges this
PR's diff also shows its commit.

## Summary

`react-hooks/refs` reports "Passing a ref to a function may read its
value during render" for callbacks passed to React Native's
`PanResponder.create({...})`, because the compiler treats `PanResponder`
imported from `react-native` as an unknown function that may call its
arguments during render. `PanResponder.create()` only stores the
handlers and invokes them later from gesture events.

The compiler already has a signature for exactly this
(`PanResponder.create` freezes its config and doesn't call it during
render), but it only exists in the test-only `shared-runtime` type
provider in `snap`, so real code never gets it. This adds the same
signature for `react-native` to `defaultModuleTypeProvider` (TypeScript,
and the Rust port in `default_module_type_provider.rs`), so the
callbacks are treated as deferred, like the `IntersectionObserver` fix
in #37764.

Other unknown functions still report errors when given ref-accessing
callbacks.

## How did you test this change?

Added a valid case to
`packages/eslint-plugin-react-hooks/__tests__/ReactCompilerRuleTypescript-test.ts`
(the repro shape from the issue). Snap fixtures can't cover this because
`snap` replaces the module type provider with its own.

- Without the fix: `cd packages/eslint-plugin-react-hooks && npx jest
ReactCompilerRuleTypescript` → `1 failed, 15 passed` (the new case).
- With the fix: `yarn build:compiler && npx jest` in
`packages/eslint-plugin-react-hooks` → `4 suites passed, 5087 tests
passed`.
- `yarn workspace babel-plugin-react-compiler tsc --noEmit` and `lint`
(in `compiler/`) pass; `yarn lint` at the root passes; prettier check
passes on the changed TS files.
- `cargo check -p react_compiler_hir` and `cargo fmt --check` pass for
the Rust change (I did not run the Rust test suite).
- `yarn snap` in `compiler/`: identical results with and without this
change (1812 passed, 20 failed; the failures are pre-existing, mostly
fbt fixtures in my local setup).
- Also added a memoized-config case (`useMemo` config passed to
`PanResponder.create`); with #37975 the eslint-plugin suite passes (5088
tests).
- `yarn flow` not run: the change touches no Flow-typed code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>

DiffTrain build for [9ca7a27](9ca7a27)
github-actions Bot pushed a commit that referenced this pull request Oct 26, 2026
…efs false positive (#37970)

Fixes #37955

**Depends on #37975** (retains declared `Freeze` effects for
already-frozen values, as suggested by @javache). Until that merges this
PR's diff also shows its commit.

## Summary

`react-hooks/refs` reports "Passing a ref to a function may read its
value during render" for callbacks passed to React Native's
`PanResponder.create({...})`, because the compiler treats `PanResponder`
imported from `react-native` as an unknown function that may call its
arguments during render. `PanResponder.create()` only stores the
handlers and invokes them later from gesture events.

The compiler already has a signature for exactly this
(`PanResponder.create` freezes its config and doesn't call it during
render), but it only exists in the test-only `shared-runtime` type
provider in `snap`, so real code never gets it. This adds the same
signature for `react-native` to `defaultModuleTypeProvider` (TypeScript,
and the Rust port in `default_module_type_provider.rs`), so the
callbacks are treated as deferred, like the `IntersectionObserver` fix
in #37764.

Other unknown functions still report errors when given ref-accessing
callbacks.

## How did you test this change?

Added a valid case to
`packages/eslint-plugin-react-hooks/__tests__/ReactCompilerRuleTypescript-test.ts`
(the repro shape from the issue). Snap fixtures can't cover this because
`snap` replaces the module type provider with its own.

- Without the fix: `cd packages/eslint-plugin-react-hooks && npx jest
ReactCompilerRuleTypescript` → `1 failed, 15 passed` (the new case).
- With the fix: `yarn build:compiler && npx jest` in
`packages/eslint-plugin-react-hooks` → `4 suites passed, 5087 tests
passed`.
- `yarn workspace babel-plugin-react-compiler tsc --noEmit` and `lint`
(in `compiler/`) pass; `yarn lint` at the root passes; prettier check
passes on the changed TS files.
- `cargo check -p react_compiler_hir` and `cargo fmt --check` pass for
the Rust change (I did not run the Rust test suite).
- `yarn snap` in `compiler/`: identical results with and without this
change (1812 passed, 20 failed; the failures are pre-existing, mostly
fbt fixtures in my local setup).
- Also added a memoized-config case (`useMemo` config passed to
`PanResponder.create`); with #37975 the eslint-plugin suite passes (5088
tests).
- `yarn flow` not run: the change touches no Flow-typed code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>

DiffTrain build for [9ca7a27](9ca7a27)
@Gourav224
Gourav224 deleted the fix/refs-callback-passed-to-call branch October 26, 2026 16:27
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.

[Compiler Bug]: react-hooks/refs false positive when using PanResponder from React Native

2 participants