-
Notifications
You must be signed in to change notification settings - Fork 49.7k
[Compiler] Improve error for calculate in render useEffect validation #34580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
b80f0f7 to
a174bc2
Compare
835b3fb to
8c4e4f6
Compare
cb5b804 to
9e2fae8
Compare
| case Effect.ConditionallyMutateIterator: | ||
| case Effect.Mutate: { | ||
| if (isMutable(instr, operand)) { | ||
| context.derivationCache.addDerivationEntry( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is currently overwriting the operand's derivationEntry. Instead, we want to merge the operand's existing entry with the current instruction's typeOfValue
| const setStateLocations: Array<SourceLocation> = []; | ||
| for (const operand of eachInstructionOperand(instr)) { | ||
| switch (operand.effect) { | ||
| case Effect.Capture: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where alias maps would come in handy. We technically don't need to mark captured values as 'fromProps' or 'fromState' unless there is a later mutation. This is a bit complex to model though, so we can skip for now
const x = {} ;
const arr = [x, props.foo];
read(x); // <- known to be not from state or propsconst x = {} ;
const arr = [x, props.foo];
write(arr);
read(x); // <- might be from state or props| readonly setStateCache: Map<string | undefined | null, Array<Place>>; | ||
| readonly effectSetStateCache: Map<string | undefined | null, Array<Place>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this a Map<Identifier, Array<Place>> or . We shouldn't rely on loc.identifierName being present, and we don't want places with a non-existent loc.identiferName to all map to the same entry.
In this example, this logic works fine since setS is named and used explicitly (e.g. called right after a LoadLocal)
const [s, setS] = useState(...);
setS();1816279 to
be75143
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shipit!
In a followup, let's change to use IdentifierId instead of source locations to key state setters.
| } | ||
|
|
||
| if (errors.hasAnyErrors()) { | ||
| throw errors; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a follow-up / when we decide to land this, let's also return and handle (log or throw) inside of the run function (in Pipeline) instead of throwing here
Summary: This creates the test cases we expect this first iteration of calculate in render to catch The goal is to have tests that will be in a good state once we have the first iteration of the calculate in render validation working, which should be pretty limited in what its capturing. Test Plan: Test cases
…k prop and local state derived values variables and add extra tests Summary: Biggest change of the stack, we track how values prop and local state values are derived throughout the entire component. We are iterating over instructions instead of effects since some mutations can not be caught otherwise. For every derivation we track the type of value its coming from (props or local state) and also the top most relevant sources (These would be the ones that are actually named instead of promoted like t0) We propagate these relevant sources to each derivation. This allows us to catch more complex useEffects though right now we are overcapturing some more complex cases which will be refined further up the stack. This PR also adds a couple tests we will work towards fixing Test Plan: Added: ref-conditional-in-effect-no-error effect-contains-prop-function-call-no-error derived-state-from-ref-and-state-no-error
… effect Summary: Using refs in an effect signify we are synchronizing with external state so to avoid overcapturing we just bail when we encounter one
…ction call in the effect Summary: Global function calls can introduce unexpected side effects, for this first iteration we are bailing out the validation when we encounter one. Local function calls remain
…ed outside of the effect Summary: If the setter is used both inside and outside the effect then usually the solution is more complex and requires hoisting state up to a parent component since we can't just remove the local state. To do this, we now have 2 caches that track setState usages (not just calls) since if the effect is passed as an argument or called outside the effect the solution gets more complex which we are trying to avoid for now
Summary: Change error and update snapshots The error now mentions what values are causing the issue which should provide better context on how to fix the issue
…#34580) Summary: Change error and update snapshots The error now mentions what values are causing the issue which should provide better context on how to fix the issue --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34580). * __->__ #34580 * #34579 * #34578 * #34577 * #34575 * #34574 DiffTrain build for [09056ab](09056ab)
…#34580) Summary: Change error and update snapshots The error now mentions what values are causing the issue which should provide better context on how to fix the issue --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34580). * __->__ #34580 * #34579 * #34578 * #34577 * #34575 * #34574 DiffTrain build for [09056ab](09056ab)
Summary:
Change error and update snapshots
The error now mentions what values are causing the issue which should provide better context on how to fix the issue
Stack created with Sapling. Best reviewed with ReviewStack.