Repository navigation
[compiler] Fix refs false positive for IntersectionObserver callbacks - #37764
Conversation
|
Comparing: 278794d...3af4393 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 3af4393 |
|
Changes look reasonable, but can you reduce the fixtures in a smaller set which repro the core issue? |
|
@javache Reduced the seven compiler fixtures to one positive reproduction and one negative shadowed-constructor case in bcbc5f1, while retaining the two ESLint integration tests Also reproduced the Rust CI failure and aligned the Rust globals and ref validation with TypeScript Local validation passes: 1829 fixtures for each backend, 1828 Rust/TypeScript parity cases, 44 compiler unit tests, 49 Rust tests, and 5084 ESLint tests Strict Clippy still hits the same pre-existing MSRV errors on the original commit, with zero new warnings in the full baseline comparison The new GitHub CI runs still need to execute |
|
Looks good, the CI failures seem unrelated. Two things I'd like resolved before this lands, both about behaviour outside IntersectionObserver:
Smaller, non-blocking:
|
|
Addressed the follow-up in 3af4393
Local validation passes for all 1831 fixtures in each backend, 1830 TS/Rust parity fixtures, 5086 ESLint tests, compiler Jest, Cargo checks/tests, Babel AST checks, formatting, lint, and typechecks Strict Clippy still reports existing diagnostics in The six workflows for the new revision require maintainer approval before they can run |
[diff react/react@07dde64d...99c106c4](react/react@07dde64...99c106c) <details> <summary>React upstream changes</summary> - react/react#37852 - react/react#37850 - react/react#37846 - react/react#37764 </details> --------- Co-authored-by: next-js-bot[bot] <279046576+next-js-bot[bot]@users.noreply.github.com> Co-authored-by: Josh Story <[email protected]>
…backs
Both constructors only retain the callback and invoke it after render, so
reading a ref inside the callback is safe. Without a modeled signature they
fall back to the default effects for unknown constructors, which report
Cannot access refs during render
for the common `new ResizeObserver(() => ref.current)` pattern. That is the
same false positive that react#37764 fixed for `IntersectionObserver`.
Model both observers in the TypeScript and Rust compilers. Each takes a
single constructor argument — unlike `IntersectionObserver`, whose options
object is a second constructor parameter, `ResizeObserver` and
`MutationObserver` receive their options in `observe(target, options)` — so
both are modeled with a single `Freeze`d callback.
The fixtures cover the accepted patterns (including `observe` options) plus
one negative case asserting that mutating a hook value inside the callback
is still rejected.
…backs
Both constructors only retain the callback and invoke it after render, so
reading a ref inside the callback is safe. Without a modeled signature they
fall back to the default effects for unknown constructors, which report
Cannot access refs during render
for the common `new ResizeObserver(() => ref.current)` pattern. That is the
same false positive that react#37764 fixed for `IntersectionObserver`.
Model both observers in the TypeScript and Rust compilers. Each takes a
single constructor argument — unlike `IntersectionObserver`, whose options
object is a second constructor parameter, `ResizeObserver` and
`MutationObserver` receive their options in `observe(target, options)` — so
both are modeled with a single `Freeze`d callback.
The fixtures cover the accepted patterns (including `observe` options) plus
one negative case asserting that mutating a hook value inside the callback
is still rejected.
…backs (#37895) ## Summary `new ResizeObserver(() => ref.current)` and `new MutationObserver(() => ref.current)` currently report a false positive: ``` Error: Cannot access refs during render > 6 | return new ResizeObserver(() => ref.current); | ^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously after the observation fires — never during render — so reading a ref inside the callback is safe. The same reasoning was applied to `IntersectionObserver` in #37764; the remaining two DOM observer constructors were still unmodeled and fell back to the default effects for unknown constructors. This PR models both the same way in the TypeScript and Rust compilers: - `ResizeObserver(callback)`: single `Freeze`d callback. (Unlike `IntersectionObserver`, whose options object is a second constructor argument, the `{box}` option of `ResizeObserver` belongs to `observe(target, options)`.) - `MutationObserver(callback)`: single `Freeze`d callback. ## Validation - `allow-ref-access-in-resize-observer.ts`: plain callback compiles, and `observe(target, {box})` options do not trip the validation. - `allow-ref-access-in-mutation-observer.ts`: callback compiles. - `error.invalid-mutate-hook-value-in-resize-observer.js`: mutating a `useMemo` value inside the callback is **still** rejected (`This value cannot be modified`), i.e. the `Freeze` effect is preserved and only the ref false positive is removed. - Unknown constructors are unaffected: `new Observer(() => ref.current)` still errors. - Full fixture suite: 1835 fixtures, 25 changed / 3 failed — identical to the pre-existing baseline in this environment (`fbt/*`, `debugger*`, `conflict-codegen-instrument-forget`), so no new snapshot deltas. - `tsc --noEmit` and `prettier check-changed` pass. ## Notes The observers' instance methods (`observe`/`unobserve`/`disconnect`) are intentionally left unmodeled, matching `IntersectionObserver`. Co-authored-by: zhoubiao188 <[email protected]>
…backs (#37895) ## Summary `new ResizeObserver(() => ref.current)` and `new MutationObserver(() => ref.current)` currently report a false positive: ``` Error: Cannot access refs during render > 6 | return new ResizeObserver(() => ref.current); | ^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously after the observation fires — never during render — so reading a ref inside the callback is safe. The same reasoning was applied to `IntersectionObserver` in #37764; the remaining two DOM observer constructors were still unmodeled and fell back to the default effects for unknown constructors. This PR models both the same way in the TypeScript and Rust compilers: - `ResizeObserver(callback)`: single `Freeze`d callback. (Unlike `IntersectionObserver`, whose options object is a second constructor argument, the `{box}` option of `ResizeObserver` belongs to `observe(target, options)`.) - `MutationObserver(callback)`: single `Freeze`d callback. ## Validation - `allow-ref-access-in-resize-observer.ts`: plain callback compiles, and `observe(target, {box})` options do not trip the validation. - `allow-ref-access-in-mutation-observer.ts`: callback compiles. - `error.invalid-mutate-hook-value-in-resize-observer.js`: mutating a `useMemo` value inside the callback is **still** rejected (`This value cannot be modified`), i.e. the `Freeze` effect is preserved and only the ref false positive is removed. - Unknown constructors are unaffected: `new Observer(() => ref.current)` still errors. - Full fixture suite: 1835 fixtures, 25 changed / 3 failed — identical to the pre-existing baseline in this environment (`fbt/*`, `debugger*`, `conflict-codegen-instrument-forget`), so no new snapshot deltas. - `tsc --noEmit` and `prettier check-changed` pass. ## Notes The observers' instance methods (`observe`/`unobserve`/`disconnect`) are intentionally left unmodeled, matching `IntersectionObserver`. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [23cd68d](23cd68d)
…backs (#37895) ## Summary `new ResizeObserver(() => ref.current)` and `new MutationObserver(() => ref.current)` currently report a false positive: ``` Error: Cannot access refs during render > 6 | return new ResizeObserver(() => ref.current); | ^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously after the observation fires — never during render — so reading a ref inside the callback is safe. The same reasoning was applied to `IntersectionObserver` in #37764; the remaining two DOM observer constructors were still unmodeled and fell back to the default effects for unknown constructors. This PR models both the same way in the TypeScript and Rust compilers: - `ResizeObserver(callback)`: single `Freeze`d callback. (Unlike `IntersectionObserver`, whose options object is a second constructor argument, the `{box}` option of `ResizeObserver` belongs to `observe(target, options)`.) - `MutationObserver(callback)`: single `Freeze`d callback. ## Validation - `allow-ref-access-in-resize-observer.ts`: plain callback compiles, and `observe(target, {box})` options do not trip the validation. - `allow-ref-access-in-mutation-observer.ts`: callback compiles. - `error.invalid-mutate-hook-value-in-resize-observer.js`: mutating a `useMemo` value inside the callback is **still** rejected (`This value cannot be modified`), i.e. the `Freeze` effect is preserved and only the ref false positive is removed. - Unknown constructors are unaffected: `new Observer(() => ref.current)` still errors. - Full fixture suite: 1835 fixtures, 25 changed / 3 failed — identical to the pre-existing baseline in this environment (`fbt/*`, `debugger*`, `conflict-codegen-instrument-forget`), so no new snapshot deltas. - `tsc --noEmit` and `prettier check-changed` pass. ## Notes The observers' instance methods (`observe`/`unobserve`/`disconnect`) are intentionally left unmodeled, matching `IntersectionObserver`. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [23cd68d](23cd68d)
…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]>
…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)
…r callbacks (#37996) ## Summary `new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are compiled with a false positive when the callback touches a ref: ``` Error: Cannot access refs during render 4 | return new PerformanceObserver(() => { | ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe. `IntersectionObserver` (#37764) and `ResizeObserver` / `MutationObserver` (#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`. This is the same class of fix as #37895, which is now on `main`. ## Fix Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals` (Rust) using the same signature shape as the existing observers: | Global | Constructor signature | | --- | --- | | `PerformanceObserver` | `(callback)` → `[Freeze]` | | `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` | `ReportingObserver`'s second argument is a genuine constructor argument (unlike the observers whose options belong to `observe()`), so it is read rather than retained. Both backends are updated so TS and Rust stay in parity. ## Tests Four new fixtures: - `allow-ref-access-in-performance-observer.ts` - `allow-ref-access-in-reporting-observer.ts` Both fail before the change with `Cannot access refs during render` and compile to memoized constructors after it. - `error.invalid-mutate-hook-value-in-performance-observer.js` - `error.invalid-mutate-hook-value-in-reporting-observer.js` Negative controls proving `Freeze` is not weakened: mutating a `useMemo` value inside the callback still reports `` `entries` cannot be modified ``. Validation run locally: - Full fixture suite (1840 fixtures): `25 changed / 3 failed` — identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new diffs. - `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean. - Rust side mirrors the TS change 1:1 but was not compiled locally (no toolchain in this environment); relying on the `babel-plugin-react-compiler-rust` CI job. Co-authored-by: zhoubiao188 <[email protected]>
…r callbacks (#37996) ## Summary `new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are compiled with a false positive when the callback touches a ref: ``` Error: Cannot access refs during render 4 | return new PerformanceObserver(() => { | ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe. `IntersectionObserver` (#37764) and `ResizeObserver` / `MutationObserver` (#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`. This is the same class of fix as #37895, which is now on `main`. ## Fix Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals` (Rust) using the same signature shape as the existing observers: | Global | Constructor signature | | --- | --- | | `PerformanceObserver` | `(callback)` → `[Freeze]` | | `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` | `ReportingObserver`'s second argument is a genuine constructor argument (unlike the observers whose options belong to `observe()`), so it is read rather than retained. Both backends are updated so TS and Rust stay in parity. ## Tests Four new fixtures: - `allow-ref-access-in-performance-observer.ts` - `allow-ref-access-in-reporting-observer.ts` Both fail before the change with `Cannot access refs during render` and compile to memoized constructors after it. - `error.invalid-mutate-hook-value-in-performance-observer.js` - `error.invalid-mutate-hook-value-in-reporting-observer.js` Negative controls proving `Freeze` is not weakened: mutating a `useMemo` value inside the callback still reports `` `entries` cannot be modified ``. Validation run locally: - Full fixture suite (1840 fixtures): `25 changed / 3 failed` — identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new diffs. - `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean. - Rust side mirrors the TS change 1:1 but was not compiled locally (no toolchain in this environment); relying on the `babel-plugin-react-compiler-rust` CI job. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [c050dbd](c050dbd)
…r callbacks (#37996) ## Summary `new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are compiled with a false positive when the callback touches a ref: ``` Error: Cannot access refs during render 4 | return new PerformanceObserver(() => { | ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe. `IntersectionObserver` (#37764) and `ResizeObserver` / `MutationObserver` (#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`. This is the same class of fix as #37895, which is now on `main`. ## Fix Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals` (Rust) using the same signature shape as the existing observers: | Global | Constructor signature | | --- | --- | | `PerformanceObserver` | `(callback)` → `[Freeze]` | | `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` | `ReportingObserver`'s second argument is a genuine constructor argument (unlike the observers whose options belong to `observe()`), so it is read rather than retained. Both backends are updated so TS and Rust stay in parity. ## Tests Four new fixtures: - `allow-ref-access-in-performance-observer.ts` - `allow-ref-access-in-reporting-observer.ts` Both fail before the change with `Cannot access refs during render` and compile to memoized constructors after it. - `error.invalid-mutate-hook-value-in-performance-observer.js` - `error.invalid-mutate-hook-value-in-reporting-observer.js` Negative controls proving `Freeze` is not weakened: mutating a `useMemo` value inside the callback still reports `` `entries` cannot be modified ``. Validation run locally: - Full fixture suite (1840 fixtures): `25 changed / 3 failed` — identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new diffs. - `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean. - Rust side mirrors the TS change 1:1 but was not compiled locally (no toolchain in this environment); relying on the `babel-plugin-react-compiler-rust` CI job. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [c050dbd](c050dbd)
Summary
Fixes #35982
This pull request resolves a false positive ref access error during render when passing callbacks to the global
IntersectionObserverconstructorCumulative implementation:
IntersectionObserverconstructor with a deferred frozen callback (Effect.Freeze), read-only options (Effect.Read), and a mutable non-aliasing return valueNewExpressioninstructions reuse standard call-effect ref validation rather than branching to a separate path, ensuring unknown constructors receiving refs directly or via callbacks are rejected during renderReview follow-up updates:
BuiltInObjectshape toPoly(Type::Polyin Rust and{kind: 'Poly'}in TypeScript)compiler/crates/react_compiler_hir/src/globals.rsallow-ref-access-in-intersection-observer.tsto two minimal hooks covering construction with and without optionsuseCallback,useMemo, and options reproduction as an ESLint integration test inpackages/eslint-plugin-react-hooks/__tests__/ReactCompilerRuleTypescript-test.tserror.invalid-ref-in-unknown-constructor.jsfor passing a direct ref argument to an unknown constructorerror.invalid-mutate-hook-value-in-intersection-observer.jsfor mutating auseMemo-returned array inside an observer callbackHow this was tested
Local verification completed across compiler and root suites:
cargo check --workspace --all-targetsandcargo test --workspace(49 tests)yarn linc, Prettier check on changed files, andrustfmton changed Rust filesrustfmtpassedQualification
cargo clippy -p react_compiler_hir -p react_compiler_validation --all-targets -- -D warningsfails on pre-existingimplicit_saturating_subandincompatible_msrvdiagnostics incompiler/crates/react_compiler_diagnostics/src/code_frame.rsa32292dunder the same toolchain