Skip to content

Commit 2b19aec

Browse files
authored
[rust-compiler] Fix exponentiation to match js semantics (react#37587)
## Summary The Rust port's constant propagation folds `**` with `f64::powf`, which follows IEEE 754. IEEE `pow` returns `1` for `pow(1, NaN)` and `pow(±1, ±∞)`, but ECMAScript's [`Number::exponentiate`](https://tc39.es/ecma262/#sec-numeric-types-number-exponentiate) returns `NaN` in those cases. The TS compiler folds with native `lhs ** rhs`, so the two backends disagree, and the Rust-compiled output changes runtime behavior: ```js const x = 1 ** (1 / 0); // JS: NaN, Rust-compiled: 1 ``` This adds `js_exponentiate`, which returns `NaN` when `|base| == 1` and the exponent is not finite, and otherwise defers to `powf`. `**=` lowers to the same `BinaryOperator::Exponent`, so it is covered too. To check the guard is neither too narrow nor too broad, I compared `f64::powf` against JS `**` over all 225 pairs drawn from {NaN, ±Infinity, ±0, ±1, ±2, ±0.5, ±3, ±1e308}. The only mismatches were `1 ** NaN`, `1 ** ±Infinity` and `(-1) ** ±Infinity`. The condition also matches `(-1) ** NaN`, where `powf` already returns `NaN`. While in this file I noticed two places where `js_to_number` diverges from `ToNumber`. I left them out to keep this PR focused, and can send a follow-up if that's useful: - `trimmed.parse::<f64>()` accepts `"inf"`, `"infinity"` and `"INFINITY"`, so `"inf" == 1 / 0` folds to `true` (JS: `false`). - The `0x`/`0o`/`0b` branches use `u64::from_str_radix`, which fails past 64 bits (`"0xFFFFFFFFFFFFFFFFF" == 295147905179352830000` folds to `false`, JS: `true`) and accepts a sign after the prefix (`"0x+10" == 16` folds to `true`, JS: `false`). ## How did you test this change? Added `constant-propagation-exponent-non-finite.js` with the five mismatching cases plus five controls: `(-1) ** NaN`, `NaN ** 0`, `2 ** Infinity`, `0.5 ** Infinity` and `2 ** 10`. The snapshot was generated with the TS compiler. - Before the fix, `yarn snap --rust -p constant-propagation-exponent-non-finite` fails on eval output: expected `[null,null,null,null,null,null,1,null,0,1024]`, got `[1,1,1,1,1,null,1,null,0,1024]`. - With a narrower guard (`exponent.is_infinite()` instead of `!exponent.is_finite()`) it still fails, only on `1 ** NaN`. - With the fix, it passes on both backends. I also ran the steps from `compiler_rust.yml` locally: - `cargo check`, `cargo build`, `cargo test --workspace` (49 passed) - `bash scripts/test-babel-ast.sh` - `bash scripts/test-rust-port.sh`: 1816 passed, 0 failed (1815 on `main`, before the new fixture) - `yarn snap --rust` and `yarn snap`: 1817 passed, 0 failed
1 parent 98cec2e commit 2b19aec

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

‎compiler/crates/react_compiler_optimization/src/constant_propagation.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ fn evaluate_binary_op(
919919
},
920920
BinaryOperator::Exponent => match (lhs, rhs) {
921921
(PrimitiveValue::Number(l), PrimitiveValue::Number(r)) => Some(PrimitiveValue::Number(
922-
FloatValue::new(l.value().powf(r.value())),
922+
FloatValue::new(js_exponentiate(l.value(), r.value())),
923923
)),
924924
_ => None,
925925
},
@@ -1107,6 +1107,15 @@ fn js_abstract_equal(lhs: &PrimitiveValue, rhs: &PrimitiveValue) -> bool {
11071107
// JavaScript Number.toString() approximation
11081108
// =============================================================================
11091109

1110+
/// ECMAScript Number::exponentiate (`**`). `f64::powf` follows IEEE 754, which
1111+
/// returns 1 for `1 ** NaN` and `(±1) ** ±Infinity`; JavaScript returns NaN.
1112+
fn js_exponentiate(base: f64, exponent: f64) -> f64 {
1113+
if base.abs() == 1.0 && !exponent.is_finite() {
1114+
return f64::NAN;
1115+
}
1116+
base.powf(exponent)
1117+
}
1118+
11101119
/// ECMAScript ToInt32: convert f64 to i32 with modular (wrapping) semantics.
11111120
fn js_to_int32(n: f64) -> i32 {
11121121
if n.is_nan() || n.is_infinite() || n == 0.0 {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {Stringify} from 'shared-runtime';
6+
7+
function foo() {
8+
return (
9+
<Stringify
10+
value={[
11+
1 ** (0 / 0),
12+
1 ** (1 / 0),
13+
1 ** (-1 / 0),
14+
(-1) ** (1 / 0),
15+
(-1) ** (-1 / 0),
16+
(-1) ** (0 / 0),
17+
(0 / 0) ** 0,
18+
2 ** (1 / 0),
19+
0.5 ** (1 / 0),
20+
2 ** 10,
21+
]}
22+
/>
23+
);
24+
}
25+
26+
export const FIXTURE_ENTRYPOINT = {
27+
fn: foo,
28+
params: [],
29+
isComponent: false,
30+
};
31+
32+
```
33+
34+
## Code
35+
36+
```javascript
37+
import { c as _c } from "react/compiler-runtime";
38+
import { Stringify } from "shared-runtime";
39+
40+
function foo() {
41+
const $ = _c(1);
42+
let t0;
43+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
44+
t0 = (
45+
<Stringify value={[NaN, NaN, NaN, NaN, NaN, NaN, 1, Infinity, 0, 1024]} />
46+
);
47+
$[0] = t0;
48+
} else {
49+
t0 = $[0];
50+
}
51+
return t0;
52+
}
53+
54+
export const FIXTURE_ENTRYPOINT = {
55+
fn: foo,
56+
params: [],
57+
isComponent: false,
58+
};
59+
60+
```
61+
62+
### Eval output
63+
(kind: ok) <div>{"value":[null,null,null,null,null,null,1,null,0,1024]}</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Stringify} from 'shared-runtime';
2+
3+
function foo() {
4+
return (
5+
<Stringify
6+
value={[
7+
1 ** (0 / 0),
8+
1 ** (1 / 0),
9+
1 ** (-1 / 0),
10+
(-1) ** (1 / 0),
11+
(-1) ** (-1 / 0),
12+
(-1) ** (0 / 0),
13+
(0 / 0) ** 0,
14+
2 ** (1 / 0),
15+
0.5 ** (1 / 0),
16+
2 ** 10,
17+
]}
18+
/>
19+
);
20+
}
21+
22+
export const FIXTURE_ENTRYPOINT = {
23+
fn: foo,
24+
params: [],
25+
isComponent: false,
26+
};

0 commit comments

Comments
 (0)