Skip to content

Conversation

@mayborn-05

This comment was marked as off-topic.

@CodiumAI-Agent

This comment was marked as off-topic.

@mayborn-05

This comment was marked as off-topic.

@CodiumAI-Agent

This comment was marked as off-topic.

@mayborn-05

This comment was marked as off-topic.

@CodiumAI-Agent

This comment was marked as off-topic.

@jorge-cab jorge-cab force-pushed the pr34580 branch 4 times, most recently from b80f0f7 to a174bc2 Compare September 24, 2025 21:11
@jorge-cab jorge-cab force-pushed the pr34580 branch 4 times, most recently from 835b3fb to 8c4e4f6 Compare October 3, 2025 21:37
@jorge-cab jorge-cab force-pushed the pr34580 branch 2 times, most recently from cb5b804 to 9e2fae8 Compare October 17, 2025 18:00
case Effect.ConditionallyMutateIterator:
case Effect.Mutate: {
if (isMutable(instr, operand)) {
context.derivationCache.addDerivationEntry(
Copy link
Contributor

@mofeiZ mofeiZ Oct 17, 2025

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:
Copy link
Contributor

@mofeiZ mofeiZ Oct 17, 2025

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 props
const x = {} ;
const arr = [x, props.foo];
write(arr);
read(x);   // <- might be from state or props

Comment on lines 43 to 44
readonly setStateCache: Map<string | undefined | null, Array<Place>>;
readonly effectSetStateCache: Map<string | undefined | null, Array<Place>>;
Copy link
Contributor

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();

@jorge-cab jorge-cab force-pushed the pr34580 branch 2 times, most recently from 1816279 to be75143 Compare October 20, 2025 22:30
Copy link
Contributor

@mofeiZ mofeiZ left a 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;
Copy link
Contributor

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
@jorge-cab jorge-cab merged commit 09056ab into main Oct 23, 2025
20 of 29 checks passed
github-actions bot pushed a commit that referenced this pull request Oct 23, 2025
…#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)
github-actions bot pushed a commit that referenced this pull request Oct 23, 2025
…#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)
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.

5 participants