Commit Graph
2105 Commits
Author SHA1 Message Date
Mike Vitousek dad908790d [compiler][wip] Validate against useEffect 2024-08-05 13:42:32 -07:00
Mike Vitousek 3af905d954 [compiler] Fix issue with macro arguments being outlined
Summary:
Fixes issue documented by #30435. We change the pipeline order so that outlining comes after tracking macro operands, and any function that is referenced in a macro will now not be outlined.

ghstack-source-id: f731ad65c8
Pull Request resolved: https://github.com/facebook/react/pull/30587
2024-08-02 14:55:55 -07:00
Mike Vitousek 47337a842a [compiler] Allow global mutation effects in arguments passed to hooks and in return values
ghstack-source-id: f9ea675ead
Pull Request resolved: https://github.com/facebook/react/pull/30576
2024-08-02 12:24:41 -07:00
Sathya Gunsasekaran bae18b4dfe [compiler] Add flag for lowering context access
*This is only for internal profiling, not intended to ship.*

ghstack-source-id: e48998b7be
Pull Request resolved: https://github.com/facebook/react/pull/30547
2024-08-02 18:13:51 +01:00
Sathya Gunsasekaran f5f9899bee [compiler] Add typing for useContext hook
In the future, we can use this to identify useContext calls.

ghstack-source-id: 01d7b0941c
Pull Request resolved: https://github.com/facebook/react/pull/30546
2024-08-02 18:13:51 +01:00
Sathya Gunsasekaran ce6078521e [compiler] Refactor makeTemporary outside HIRBuilder
This is a useful utility function similar to the existing
`makeInstructionId` and `makeIdentifierId` functions.

This PR moves it outside the HIRBuilder so we can use this in passes
that don't have access to the builder instance.

ghstack-source-id: 1ac0839e6c
Pull Request resolved: https://github.com/facebook/react/pull/30545
2024-08-02 18:13:51 +01:00
Sathya Gunsasekaran a5a58164ea [compiler] Simplify FunctionExpression node
Rather than storing the entire babel node, store only the required
information which is the node type.

This will be useful for when we synthesize new functions that don't have
a corresponding babel node.

ghstack-source-id: 9098cbdbc4
Pull Request resolved: https://github.com/facebook/react/pull/30544
2024-08-02 18:13:51 +01:00
Joe Savona 1db4d6c415 [compiler] Validate against setState in useMemo (resubmit of #30552)
ghstack failed to land #30552 properly, resubmitting

Developers sometimes use `useMemo()` as a way to conditionally execute code, including conditionally calling setState. However, the compiler may remove existing useMemo calls if they are not necessary, which _should_ always be a safe optimization. If the useMemo has side effects (eg sets state), then this isn't safe.

This PR improves ValidateNoSetStateInRender to disallow any setState in useMemo (even if it's conditional), expanding on the previous check for unconditional setState in render. Note that the approach uses the StartMemoize/FinishMemoize instructions added in DropManualMemo to know whether a particular setState call is within a useMemo or not. This means enabling the validation in DropManualMemo when the setState validation is enabled, but that's fine since that validation is on everywhere by default (_except_ for in fixtures, which we have a todo for)

ghstack-source-id: 65bb3289c3
Pull Request resolved: https://github.com/facebook/react/pull/30583
2024-08-02 10:06:08 -07:00
Mike Vitousek 56dbd58feb [compiler] More complete validation against locals being reassigned after render
Summary:
This diff extends the existing work on validating against locals being reassigned after render, by propagating the reassignment "effect" into the lvalues of instructions when the rvalue operands include values known to cause reassignments. In particular, this "closes the loop" for function definitions and function calls: a function that returns a function that reassigns will be considered to also perform reassignments, but previous to this we didn't consider the result of a `Call` of a function that reassigns to itself be a value that reassigns.

This causes a number of new bailouts in test cases, all of which appear to me to be legit.

ghstack-source-id: 770bf02d07
Pull Request resolved: https://github.com/facebook/react/pull/30540
2024-07-31 11:11:07 -07:00
Lauren Tan d06196c1cd [compiler] Visit nested scopes in pruned scopes in PromoteUsedTemporaries
While debugging #30536 I happened to notice that the bug only reproduced
when there was interleaving scopes, and observed that an unpruned scope
nested inside of a pruned one was not being visited by
CollectPromotableTemporaries, which keeps track of which identifiers
should be promoted later. Therefore when actually promoting temporaries
we were skipping over the identifiers in children of pruned scopes

ghstack-source-id: d805f62f22
Pull Request resolved: https://github.com/facebook/react/pull/30537
2024-07-30 17:05:23 -04:00
Mofei Zhang edfaa99f01 [compiler][repro] JSX tag local variable is named lowercase
ghstack-source-id: f9ac4641e1
Pull Request resolved: https://github.com/facebook/react/pull/30536
2024-07-30 16:32:09 -04:00
Mofei Zhang 212d5ae8cb [compiler][repro] fixtures for fbt plural and macro bugs
ghstack-source-id: 8ccf49bb40
Pull Request resolved: https://github.com/facebook/react/pull/30535
2024-07-30 16:32:09 -04:00
Mofei Zhang 704c34e21f [compiler] Bail out on local variables named 'fbt'
ghstack-source-id: c4e2b802a0
Pull Request resolved: https://github.com/facebook/react/pull/30524
2024-07-30 16:32:09 -04:00
Mofei Zhang 7c8734c41c [compiler][repro] Fbt local var incompatibility repro
ghstack-source-id: ec256a365b
Pull Request resolved: https://github.com/facebook/react/pull/30523
2024-07-30 16:32:09 -04:00
Jan KassensandGitHub 41ecbada0e Enable prettier for some blocklisted fixtures with invalid GraphQL (#30425)
The invalid GraphQL in these fixtures somehow causes an unhandled
promise rejection error when running `yarn prettier-all`. This fixes
that issue by making the GraphQL valid.
2024-07-30 14:23:43 -04:00
Mofei Zhang a8f5c4e16d [compiler] patch: retain UpdateExpression for globals
ghstack-source-id: aff6606f85
Pull Request resolved: https://github.com/facebook/react/pull/30471
2024-07-29 13:18:45 -04:00
Mofei Zhang 2a9274a73e [compiler] Repro: updates to globals should be retained
ghstack-source-id: 7ee813c0d7
Pull Request resolved: https://github.com/facebook/react/pull/30470
2024-07-29 13:18:45 -04:00
Mofei Zhang e215aa1160 [compiler] Fix FBT whitespace handling again (again (again))
ghstack-source-id: 00a86e41cf
Pull Request resolved: https://github.com/facebook/react/pull/30451
2024-07-29 13:18:45 -04:00
Mofei Zhang e0acd77aab [compiler] Todo for fbt with multiple pronoun/plural
ghstack-source-id: 77b6980c2b
Pull Request resolved: https://github.com/facebook/react/pull/30437
2024-07-29 13:18:45 -04:00
Mofei Zhang a15e8d7cdc [compiler][repro] Test fixture for fbt plural bug
ghstack-source-id: 222e7312c0
Pull Request resolved: https://github.com/facebook/react/pull/30432
2024-07-29 13:18:45 -04:00
Ahmed AbdelbasetandGitHub f7ee804c22 [compiler] Add git info to package.json files in compiler packages (#30475)
This PR adds the repository field to `compiler/packages/*/package.json`

| eslint-plugin-react-compiler | eslint-plugin-react-hooks |
| --- | --- |
|
![image](https://github.com/user-attachments/assets/3392a496-1ff1-4f36-ab96-cfbe1ed88693)
|
![image](https://github.com/user-attachments/assets/b0605dba-eef7-44fe-9484-979b4814d9fb)
|
2024-07-26 12:56:39 -04:00
Lauren Tan a0e7ecfc6a Bump version to 0.0.0-experimental-ab3118d-20240725 2024-07-25 15:46:47 -04:00
Lauren Tan 83035ee299 Bump version to 0.0.0-experimental-9ed098e-20240725 2024-07-25 15:46:46 -04:00
Lauren Tan 789c257d79 Bump version to 0.0.0-experimental-334f00b-20240725 2024-07-25 15:46:45 -04:00
Lauren Tan 9f3bbb05ab [compiler] Make inserting outlined functions more resilient
To handle more cases, always append the synthetic outlined function as a
new child of the module rather than make assumptions about the original
function. This should handle whatever case where the original function
expression may be a child of a variety of parents

ghstack-source-id: 8581edb8be
Pull Request resolved: https://github.com/facebook/react/pull/30466
2024-07-25 15:38:19 -04:00
Lauren Tan d529a43212 [compiler] Add repro for outlining in non VarDecls
ghstack-source-id: 7688987b02
Pull Request resolved: https://github.com/facebook/react/pull/30465
2024-07-25 15:38:19 -04:00
Lauren Tan 88bf4e197a [compiler] Always emit FuncDecl for outlined functions
Addresses follow up feedback from #30446. Since the outlined function is
guaranteed to have a module-scoped unique identifier name, we can
simplify the insertion logic for the outlined function to always emit a
function declaration rather than switch based on the original function
type. This is fine because the outlined function is synthetic anyway.

ghstack-source-id: 0a4d1f7b0a
Pull Request resolved: https://github.com/facebook/react/pull/30464
2024-07-25 15:38:18 -04:00
Sathya Gunsasekaran c5fa460784 [compiler] Add tests for incorrect global mutation detection
If a function expression that mutates a global is passed as a prop,
we don't throw an error as we assume it's not called in render.

But if this function expression is captured in an object and passed down
as prop, we throw an error.

ghstack-source-id: 74cacee09f
Pull Request resolved: https://github.com/facebook/react/pull/30456
2024-07-25 17:47:20 +01:00
Lauren Tan 66968535c6 Bump version to 0.0.0-experimental-176c31a-20240725 2024-07-25 12:27:06 -04:00
Lauren Tan 9f04494b8e Bump version to 0.0.0-experimental-47e15aa-20240725 2024-07-25 12:27:05 -04:00
Lauren Tan 2e800a81d3 Bump version to 0.0.0-experimental-f1f288c-20240725 2024-07-25 12:27:04 -04:00
Lauren Tan a6b7e438ca [compiler] Correctly insert (Arrow)FunctionExpressions
Previously we would insert new (Arrow)FunctionExpressions as a sibling
of the original function. However this would break in the outlining case
as it would cause the original function expression's parent to become a
SequenceExpression, breaking a bunch of assumptions in the babel plugin.

To get around this, we synthesize a new VariableDeclaration to contain
the newly inserted function expression and therefore insert it as a true
sibling to the original function.

Yeah, it's kinda gross

ghstack-source-id: df13e3b439
Pull Request resolved: https://github.com/facebook/react/pull/30446
2024-07-24 16:02:29 -04:00
Lauren Tan 91e4f0712e [compiler] Repro for outlining bug in funcexprs
I discovered this compiler crash while trying to do an internal sync of
the compiler. Any kind of outlining appears to crash the babel plugin
when the component is a function expression.

ghstack-source-id: 4f717674af
Pull Request resolved: https://github.com/facebook/react/pull/30443
2024-07-24 16:02:28 -04:00
Jan KassensandGitHub ab2135c708 [BE] enable prettier for flow fixtures (#30426)
Since switching to `hermes-parser`, we can parse all flow syntax and no
longer need to exclude these fixtures from prettier.
2024-07-24 10:59:40 -04:00
Joe Savona b943feba35 [compiler] Stop relying on identifier mutable ranges after constructing scopes
Addresses discussion at https://github.com/facebook/react/pull/30399#discussion_r1684693021. Once we've constructed scopes it's invalid to use identifier mutable ranges. The only places we can do this which i can find are ValidateMemoizedEffectDeps (which is already flawed and disabled by default) and ValidatePreservedManualMemoization. I added a todo to the former, and fixed up the latter.

The idea of the fix is that for StartMemo dependencies, if they needed to be memoized (identifier.scope != null) then that scope should exist and should have already completed. If they didn't need a scope or can't have one created (eg their range spans a hook), then their scope would be pruned. So if the scope is set, not pruned, and not completed, then it's an error.

For declarations (FinishMemo) the existing logic applies unchanged.

ghstack-source-id: af5bfd8855
Pull Request resolved: https://github.com/facebook/react/pull/30428
2024-07-24 09:22:16 +09:00
Joe Savona c08b516007 [compiler] repro of false positive for ValidatePreserveManualMemo
ghstack-source-id: 235c5bae08
Pull Request resolved: https://github.com/facebook/react/pull/30431
2024-07-24 09:22:16 +09:00
Joe Savona f987f1a057 [compiler] Maintain RPO and unique instruction ids when constructing scope terminals
Later passes may rely on HIR invariants such as blocks being in RPO or instructions having unique, ascending InstructionIds. However, BuildReactiveScopeTerminalsHIR doesn't currently gurantee this.

This PR updates that pass to first restore RPO, fixup predecessors (the previous logic tried to do this but failed on unreachable blocks, where `markPredecessors()` handles that case), and renumber instructions. Then it walks instructions and scopes to update identifier and scope ranges given the new instruction ids.

ghstack-source-id: 2a99df02ac
Pull Request resolved: https://github.com/facebook/react/pull/30399
2024-07-24 09:22:16 +09:00
Joe Savona ca5fef0f4a [compiler] Flatten scopes based on fallthrough, not scope range
Once we create scopes, we should prefer to use the block structure to identify active scope ranges rather than the scope range. They _should_ always be in sync, but ultimately the block structure determine the active range (ie the id of the 'scope' terminal and the terminal's fallthrough block).

ghstack-source-id: 730b6d1cfa
Pull Request resolved: https://github.com/facebook/react/pull/30398
2024-07-24 09:22:16 +09:00
Joe Savona 7c7087811e [compiler] printTerminal always prints instruction id
Doing some debugging I noticed that a few of the newer terminals kinds weren't printing the instruction id.

ghstack-source-id: e0e4c96aee
Pull Request resolved: https://github.com/facebook/react/pull/30397
2024-07-24 09:22:16 +09:00
Lauren Tan 942eb80381 [compiler] Repro for interaction b/w outlining and idx
babel-plugin-idx enforces that its 2nd argument is an arrow function, so
outlining needs to skip over lambdas that are args to idx. This PR adds
a small repro highlighting the issue.

ghstack-source-id: b4627ec552
Pull Request resolved: https://github.com/facebook/react/pull/30435
2024-07-23 15:56:56 -04:00
Lauren Tan 08b4ee83cc [compiler] Add babel-plugin-idx to snap
To surface any potential conflicts with this plugin, let's install it
into snap so we can surface any runtime errors after compilation

ghstack-source-id: 545eee6fb7
Pull Request resolved: https://github.com/facebook/react/pull/30434
2024-07-23 15:56:56 -04:00
Mofei Zhang b34b750729 [compiler][eslint] remove compilationMode override; report bailouts on first line
ghstack-source-id: 1870c7aa09
Pull Request resolved: https://github.com/facebook/react/pull/30423
2024-07-23 14:24:36 -04:00
Mofei Zhang 50640a1b88 [compiler][repro] Test fixture for fbt whitespace bug
ghstack-source-id: 749feb2cb8
Pull Request resolved: https://github.com/facebook/react/pull/30394
2024-07-23 14:24:36 -04:00
Jan KassensandGitHub 70484844bf [BE] switch to hermes parser for prettier (#30421)
This will allow us to parse new flow syntax since the `flow` parser is
no longer updated.

I had to exclude some files and have them fall back to `flow` parser
since they contain invalid graphql syntax that makes the plugin crash.
2024-07-22 19:16:13 -04:00
Jan KassensandGitHub b7e7f1a3fa [BE] upgrade prettier to 3.3.3 (#30420)
Mostly just changes in ternary formatting.
2024-07-22 16:09:01 -04:00
Jan KassensandGitHub fd2b3e13d3 Compiler: unfork prettier config (#30205)
Updates the prettier config to format all `.ts` and `.tsx` files in the
repo using the existing defaults and removing overrides.

The first commit in this PR contains the config changes, the second is
just the result of running `yarn prettier-all`.
2024-07-18 17:00:24 -04:00
Mofei Zhang b2ec0445b4 [compiler][patch] Don't wrap non-ascii fbt operands in JSXExpressionContainer
ghstack-source-id: 4b5505aa5e
Pull Request resolved: https://github.com/facebook/react/pull/30389
2024-07-18 16:18:36 -04:00
Mofei Zhang 378ab81522 [compiler][fixtures] Repro for fbt + non-ascii strings
ghstack-source-id: 0521198056
Pull Request resolved: https://github.com/facebook/react/pull/30386
2024-07-18 16:18:36 -04:00
Mofei Zhang e15c5b15c6 [compiler][patch] Fix jsx non-ascii regex pattern
ghstack-source-id: 97ed491124
Pull Request resolved: https://github.com/facebook/react/pull/30382
2024-07-18 16:18:36 -04:00
Joe Savona 163365a078 [compiler] Show outlined functions in logging, playground
ghstack-source-id: abda15e874
Pull Request resolved: https://github.com/facebook/react/pull/30344
2024-07-17 10:22:08 +09:00