[compiler][poc] Improve impurity/ref tracking #225
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Mirror of facebook/react#35298
Original author: josephsavona
We currently model impure functions like
Date.now()with anImpureeffect, but the way this works is that the effect is considered an error if it's called at all. But we really want to reflect the fact that the value is impure, and is okay to be used outside of render. For example,useRef(Date.now())should be fine.So this PR changes the Impure effect to describe that an impure value flows
into: Place. We flow impurity through the data flow graph during InferMutationAliasingRanges, including propagating this to the function's effects. So if a function expression returns a transitively impure value, that's expressed in our inferred signature for that function. Calling it propagates the impurity via our effect system.We stop this propagation when reaching a ref, allowing
useRef(Date.now())oruseRef(localFunctionThatReturnsDateNow()).This lets us also model accessing a ref as an impure value - we just emit an
Impureevent for PropertyLoad/ComputedLoad off of a ref, and the above tracking kicks in.A final piece is that we can also use our existing machinery to disallow writing to global values during render to handle writing to refs - except that we need to allow the lazy init pattern. So it probably makes sense to keep the ref validation pass, but scope it back to just handling writes and not reads.
This definitely needs more polish, the error messages would ideally be something like:
Ie show you both the local site (where impurity flows into render) and the ultiamte source of the impure value.