From 148aa1901fec75daeebce2b003c4ca11661a6ea7 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 29 May 2025 17:30:51 -0700 Subject: [PATCH] [compiler] Receiver is mutate? for functions wo signatures [ghstack-poisoned] --- .../Inference/InferMutationAliasingEffects.ts | 17 +++++- .../new-mutability/set-add-mutate.expect.md | 54 +++++++++++++++++++ .../compiler/new-mutability/set-add-mutate.js | 11 ++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.js diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts index 475319830373..bb246671cb92 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferMutationAliasingEffects.ts @@ -519,6 +519,9 @@ function applyEffect( ) : null; if (signatureEffects != null) { + if (DEBUG) { + console.log('apply aliasing signature effects'); + } for (const signatureEffect of signatureEffects) { applyEffect( state, @@ -530,6 +533,9 @@ function applyEffect( ); } } else if (effect.signature != null) { + if (DEBUG) { + console.log('apply legacy signature effects'); + } const legacyEffects = computeEffectsForLegacySignature( state, effect.signature, @@ -548,6 +554,9 @@ function applyEffect( ); } } else { + if (DEBUG) { + console.log('default effects'); + } applyEffect( state, { @@ -567,7 +576,7 @@ function applyEffect( * - All operands are captured into (but not directly aliased as) * every other argument. */ - for (const arg of [effect.function, ...effect.args]) { + for (const arg of [effect.receiver, effect.function, ...effect.args]) { const operand = arg.kind === 'Identifier' ? arg : arg.place; if (operand !== effect.function || effect.mutatesFunction) { applyEffect( @@ -599,7 +608,11 @@ function applyEffect( aliased, effects, ); - for (const otherArg of [effect.function, ...effect.args]) { + for (const otherArg of [ + effect.receiver, + effect.function, + ...effect.args, + ]) { const other = otherArg.kind === 'Identifier' ? otherArg : otherArg.place; if (other === arg) { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md new file mode 100644 index 000000000000..955c4e070579 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function useHook({el1, el2}) { + const s = new Set(); + const arr = makeArray(el1); + s.add(arr); + // Mutate after store + arr.push(el2); + + s.add(makeArray(el2)); + return s.size; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function useHook(t0) { + const $ = _c(5); + const { el1, el2 } = t0; + let s; + if ($[0] !== el1 || $[1] !== el2) { + s = new Set(); + const arr = makeArray(el1); + s.add(arr); + + arr.push(el2); + let t1; + if ($[3] !== el2) { + t1 = makeArray(el2); + $[3] = el2; + $[4] = t1; + } else { + t1 = $[4]; + } + s.add(t1); + $[0] = el1; + $[1] = el2; + $[2] = s; + } else { + s = $[2]; + } + return s.size; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.js new file mode 100644 index 000000000000..3afbd93f84b1 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.js @@ -0,0 +1,11 @@ +// @enableNewMutationAliasingModel +function useHook({el1, el2}) { + const s = new Set(); + const arr = makeArray(el1); + s.add(arr); + // Mutate after store + arr.push(el2); + + s.add(makeArray(el2)); + return s.size; +}