Skip to content

[compiler] Retain declared Freeze effects on already-frozen values for ref validation - #37975

Merged
javache merged 1 commit into
react:mainfrom
Gourav224:compiler/preserve-freeze-effect-for-ref-validation
Oct 23, 2026
Merged

javache merged 1 commit into
react:mainfrom
Gourav224:compiler/preserve-freeze-effect-for-ref-validation

Conversation

@Gourav224

Copy link
Copy Markdown
Contributor

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:

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

@javache
javache merged commit a6b912f into react:main Oct 23, 2026
25 checks passed
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 deleted the compiler/preserve-freeze-effect-for-ref-validation branch October 26, 2026 04:10
javache 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]>
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)
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.

3 participants