diff --git a/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts b/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts index e2116c9d94..fd19369c24 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts @@ -369,6 +369,58 @@ function evaluateInstruction( } break; } + case "|": { + if (typeof lhs === "number" && typeof rhs === "number") { + result = { kind: "Primitive", value: lhs | rhs, loc: value.loc }; + } + break; + } + case "&": { + if (typeof lhs === "number" && typeof rhs === "number") { + result = { kind: "Primitive", value: lhs & rhs, loc: value.loc }; + } + break; + } + case "^": { + if (typeof lhs === "number" && typeof rhs === "number") { + result = { kind: "Primitive", value: lhs ^ rhs, loc: value.loc }; + } + break; + } + case "<<": { + if (typeof lhs === "number" && typeof rhs === "number") { + result = { kind: "Primitive", value: lhs << rhs, loc: value.loc }; + } + break; + } + case ">>": { + if (typeof lhs === "number" && typeof rhs === "number") { + result = { kind: "Primitive", value: lhs >> rhs, loc: value.loc }; + } + break; + } + case ">>>": { + if (typeof lhs === "number" && typeof rhs === "number") { + result = { + kind: "Primitive", + value: lhs >>> rhs, + loc: value.loc, + }; + } + break; + } + case "%": { + if (typeof lhs === "number" && typeof rhs === "number") { + result = { kind: "Primitive", value: lhs % rhs, loc: value.loc }; + } + break; + } + case "**": { + if (typeof lhs === "number" && typeof rhs === "number") { + result = { kind: "Primitive", value: lhs ** rhs, loc: value.loc }; + } + break; + } case "<": { if (typeof lhs === "number" && typeof rhs === "number") { result = { kind: "Primitive", value: lhs < rhs, loc: value.loc }; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/assignment-variations.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/assignment-variations.expect.md index 746fed4056..880601fbc6 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/assignment-variations.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/assignment-variations.expect.md @@ -22,10 +22,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript function f() { - let x; - - x = 3 >>> 1; - return x; + return 1; } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.expect.md new file mode 100644 index 0000000000..99c5d71e78 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.expect.md @@ -0,0 +1,78 @@ + +## Input + +```javascript +import { Stringify } from "shared-runtime"; + +function foo() { + return ( + > 0, + 123 >>> 0, + 123.45 | 1, + 123.45 & 1, + 123.45 ^ 1, + 123 << 1, + 123 >> 1, + 123 >>> 1, + 3 ** 2, + 3 ** 2.5, + 3.5 ** 2, + 2 ** (3 ** 0.5), + 4 % 2, + 4 % 2.5, + 4 % 3, + 4.5 % 2, + ]} + /> + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( + + ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok)
{"value":[123,0,123,123,123,123,123,1,122,246,61,61,9,15.588457268119896,12.25,3.3219970854839125,0,1.5,1,0.5]}
\ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.js new file mode 100644 index 0000000000..967d48c209 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.js @@ -0,0 +1,36 @@ +import { Stringify } from "shared-runtime"; + +function foo() { + return ( + > 0, + 123 >>> 0, + 123.45 | 1, + 123.45 & 1, + 123.45 ^ 1, + 123 << 1, + 123 >> 1, + 123 >>> 1, + 3 ** 2, + 3 ** 2.5, + 3.5 ** 2, + 2 ** (3 ** 0.5), + 4 % 2, + 4 % 2.5, + 4 % 3, + 4.5 % 2, + ]} + /> + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +};