Skip to content

Allow impure ref initialization in render#35992

Open
angular-bootstrap wants to merge 1 commit intofacebook:mainfrom
angular-bootstrap:react/ref-init-impure-purity
Open

Allow impure ref initialization in render#35992
angular-bootstrap wants to merge 1 commit intofacebook:mainfrom
angular-bootstrap:react/ref-init-impure-purity

Conversation

@angular-bootstrap
Copy link

@angular-bootstrap angular-bootstrap commented Mar 10, 2026

Summary

Allows the narrow safe case of initializing ref.current from an impure call during render when it is guarded by the standard null-check initialization pattern.

This updates the compiler to recognize:

if (ref.current == null) {
  ref.current = Date.now();
}

as a valid one-time ref initialization flow, while continuing to reject other impure render-time calls.

Changes

Add shared compiler analysis to detect allowed impure ref initializers.
Use that analysis in ValidateNoImpureFunctionsInRender.
Use the same analysis in InferMutationAliasingRanges so the inference path does not still surface the call as an Impure error.
Add compiler fixture coverage for the allowed case in both fixture trees.

Test Plan

yarn snap -p allow-impure-ref-initialization
yarn snap -p error.invalid-impure-functions-in-render

Both targeted fixture runs reported the expected pass/fail behavior locally. The snap runner in this environment still exits non-zero afterward because of its existing worker shutdown bug (Farm is ended, no more calls can be done to it).

Issue

Closes #35973

@meta-cla meta-cla bot added the CLA Signed label Mar 10, 2026
@angular-bootstrap
Copy link
Author

@josephsavona

Following up on your earlier comment, I reworked this as a compiler-side change with compiler fixture tests.

This fix allows the narrow safe case of null-guarded ref.current initialization from an impure call, while leaving the existing invalid impure render cases unchanged.

I verified it with targeted snap fixture runs locally. The fixture results were as expected, although the snap runner in my environment still exits non-zero afterward because of an existing worker shutdown issue (Farm is ended, no more calls can be done to it).

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]: ref initialization using === null doesn't work with impure functions

1 participant