-
Notifications
You must be signed in to change notification settings - Fork 47.9k
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
[compiler] Patch array and argument spread mutability #32521
base: main
Are you sure you want to change the base?
Conversation
const items = makeArray(0, 1, 2, null, 4, false, 6); | ||
return useIdentity(...items.values()); |
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 the hook call bailout case. This new bailout is good (better correctness, as we were previously memoizing the items.values()
iterable)
Ideally we can rewrite this to 1) create an intermediate array to consume the iterable and (2) capture and spread the array at the callsite.
const items = makeArray(0, 1, 2, null, 4, false, 6);
const tmp = [...items.values()]; // this spread has conditionallyMutate effects
return useIdentity([...tmp]); // but this spread has freeze effects
…ors (#32520) See newly added test fixtures. Repros fixed in later prs of this stack --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/32520). * #32522 * #32521 * __->__ #32520
Array and argument spreads may mutate stateful iterables. Spread sites should have `ConditionallyMutate` effects (e.g. mutate if the ValueKind is mutable, otherwise read). See - [ecma spec (13.2.4.1 Runtime Semantics: ArrayAccumulation. SpreadElement : ... AssignmentExpression)](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-runtime-semantics-arrayaccumulation). - [ecma spec 13.3.8.1 Runtime Semantics: ArgumentListEvaluation](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-runtime-semantics-argumentlistevaluation) Note that - Object and JSX Attribute spreads do not evaluate iterables (srcs [mozilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#description), [ecma](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-runtime-semantics-propertydefinitionevaluation)) - An ideal mutability inference system could model known collections (i.e. Arrays or Sets) as a "mutated collection of non-mutable objects" (see `todo-granular-iterator-semantics`), but this is not what we do today. As such, an array / argument spread will always extend the range of built-in arrays, sets, etc - Due to HIR limitations, call expressions with argument spreads may cause unnecessary bailouts and/or scope merging when we know the call itself has `freeze`, `capture`, or `read` semantics (e.g. `useHook(...mutableValue)`) We can deal with this by rewriting these call instructions to (1) create an intermediate array to consume the iterator and (2) capture and spread the array at the callsite
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 great! See comments for a couple concrete things to take a look at before landing, up to you if you want to address them here or a follow-up.
state.referenceAndRecordEffects( | ||
freezeActions, | ||
element.place, | ||
Effect.ConditionallyMutate, | ||
ValueReason.Other, | ||
); |
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.
I wonder if we could address most of the negative consequences of this change by just special-casing array here. Basically isBuiltInArrayType(element) ? Effect.Capture : Effect.ConditionallyMutate
as the effect. Because spreading doesn't mutate arrays, it only mutates if you have an iterator.
@@ -1330,7 +1344,8 @@ function inferBlock( | |||
state.referenceAndRecordEffects( | |||
freezeActions, | |||
place, | |||
Effect.Read, | |||
// see call-spread-argument-mutable-iterator test fixture | |||
arg.kind === 'Spread' ? Effect.ConditionallyMutate : Effect.Read, |
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.
hmm this doesn't seem like it should be necessary - we're inside an if (areArgumentsImmutableAndNonMutating())
check
Array and argument spreads may mutate stateful iterables. Spread sites should have
ConditionallyMutate
effects (e.g. mutate if the ValueKind is mutable, otherwise read).See
Note that
todo-granular-iterator-semantics
), but this is not what we do today. As such, an array / argument spread will always extend the range of built-in arrays, sets, etcfreeze
,capture
, orread
semantics (e.g.useHook(...mutableValue)
)We can deal with this by rewriting these call instructions to (1) create an intermediate array to consume the iterator and (2) capture and spread the array at the callsite
Stack created with Sapling. Best reviewed with ReviewStack.