Repository navigation
[compiler] Type react-native PanResponder.create to fix react-hooks/refs false positive - #37970
Conversation
|
Hi @Gourav224! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Comparing: a6b912f...d91f7f3 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: (No significant changes) Generated by sizebot against d91f7f3 |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
There's a problem here when config is a previously memoized value: Could you send a separate PR first to fix how inference handles an explicitly declared Suggested prerequisite fixIn 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. |
c1bba7e to
ba155f9
Compare
|
Thanks @javache, good catch. I opened #37975 as the prerequisite (retains the declared |
…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]>
…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)
…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)
…efs false positive Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
ba155f9 to
d91f7f3
Compare
|
Rebased on main now that #37975 has landed. The diff now contains only the |
…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)
…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)
Fixes #37955
Depends on #37975 (retains declared
Freezeeffects for already-frozen values, as suggested by @javache). Until that merges this PR's diff also shows its commit.Summary
react-hooks/refsreports "Passing a ref to a function may read its value during render" for callbacks passed to React Native'sPanResponder.create({...}), because the compiler treatsPanResponderimported fromreact-nativeas 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.createfreezes its config and doesn't call it during render), but it only exists in the test-onlyshared-runtimetype provider insnap, so real code never gets it. This adds the same signature forreact-nativetodefaultModuleTypeProvider(TypeScript, and the Rust port indefault_module_type_provider.rs), so the callbacks are treated as deferred, like theIntersectionObserverfix 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 becausesnapreplaces the module type provider with its own.cd packages/eslint-plugin-react-hooks && npx jest ReactCompilerRuleTypescript→1 failed, 15 passed(the new case).yarn build:compiler && npx jestinpackages/eslint-plugin-react-hooks→4 suites passed, 5087 tests passed.yarn workspace babel-plugin-react-compiler tsc --noEmitandlint(incompiler/) pass;yarn lintat the root passes; prettier check passes on the changed TS files.cargo check -p react_compiler_hirandcargo fmt --checkpass for the Rust change (I did not run the Rust test suite).yarn snapincompiler/: identical results with and without this change (1812 passed, 20 failed; the failures are pre-existing, mostly fbt fixtures in my local setup).useMemoconfig passed toPanResponder.create); with [compiler] Retain declared Freeze effects on already-frozen values for ref validation #37975 the eslint-plugin suite passes (5088 tests).yarn flownot run: the change touches no Flow-typed code.🤖 Generated with Claude Code