Semantic Diff

#9422 Negate floats as -0 - x and parenthesize ** under prefix operators

2 commits

6a7e3b8dbabcd2aa54643d86044d2f8ec2d6f804..pr-9422

c889611b5 · illusionalsagacity · Nov 25, 2026, 19:30 UTC

Negate floats as -0 - x and parenthesize ** under prefix operators

Why

The JS IR represents a negation as a subtraction from zero, and the printer printed every 0 - x as -x. That is wrong for a float subtraction: 0. -. x is 0 when x is 0., but -x is -0. A float negation is now a subtraction from -0., which equals -x for every x, and only that is printed as - x. An int negation stays 0 - x; it is printed as -x only as the operand of a bitwise operator, such as the | 0 of every int subtraction, which drops the sign of a zero. Prefix operators also printed their operand at the level of **, so a power had no parentheses there, which JavaScript rejects. This concerned a float negation, ~ on a bigint, typeof and await; the operands of all prefix operators now go through unary_operand. Where 0 - x is printed as -x, an x that prints with a leading minus is kept apart by a space, as found by prints_with_leading_minus; of the subtractions, only -0 - x prints with a leading minus now.

modifiedJS subtraction from zero

let fromLiteral = x => 0. -. xlet fromInt = x => Float.fromInt(0) -. xlet negate = x => -.xlet negateInt = (x: int) => -x
js
Before
After
function fromLiteral(x) {function fromLiteral(x) { return - x; return 0 - x;}} function fromInt(x) {function fromInt(x) { return -x; return 0 - x;}} function negate(x) {function negate(x) { return - x; return - x;}} function negateInt(x) {function negateInt(x) { return -x | 0; return -x | 0;}}

modifiedJS subtraction from a negative zero literal

let f = x => -0. -. x
js
Before
After
return -0 - x;return - x;

modifiedJS power under a prefix operator

let negate = (a: float, b: float) => -.(a ** b)let bitNot = (a: bigint, b: bigint) => ~~~(a ** b)let typeOf = (a: float, b: float) => typeof(a ** b)let wait = async (a: float, b: float) => await Obj.magic(a ** b)
js
Before a syntax error
After
function negate(a, b) {function negate(a, b) { return - a ** b; return - (a ** b);}} function bitNot(a, b) {function bitNot(a, b) { return ~a ** b; return ~(a ** b);}} function typeOf(a, b) {function typeOf(a, b) { return typeof a ** b; return typeof (a ** b);}} async function wait(a, b) {async function wait(a, b) { return await a ** b; return await (a ** b);}}

Review

  • Why decide the int case by the enclosing operator? Inlining can put an int literal into a float subtraction, so a left operand 0 does not tell an int from a float, but the sign of a zero never survives a bitwise operator.
  • Which checked-in outputs change? Only float_add_fold_test.mjs: a subtraction from a zero int such as - -5 now prints as 0 - -5. Its case for a negative bigint, which this would turn into 0 - -5n and a "Cannot mix BigInt" error, negates the bigint with -. instead.
bb6811d61 · illusionalsagacity · Nov 27, 2026, 16:03 UTC

Test int negation of operands printed with a leading minus

Why

Under a bitwise operator, 0 - x is printed as -x, and an x that prints with a leading minus is kept apart by a space. The tests only covered int and float literals, so they now also negate raw JavaScript, a raw literal in an identity Some, a negative zero, a float negation and a negative bigint, whose - -5n | 0 parses and then throws a TypeError. is_pow lists every expression kind instead of ending in a wildcard, so a new kind has to be decided there, and a test case that appeared twice is gone.

Review

  • Does any output change? No compiler output does; only the tests of unary_minus_test.res are added or moved.