Skip to content

feat(compiler): implement constant folding for unary minus #33140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025

Conversation

nikeee
Copy link
Contributor

@nikeee nikeee commented May 7, 2025

Summary

-constant is represented as a UnaryExpression node that is currently not part of constant folding. If the operand is a constant number, the node is folded to constant * -1. This also coerces -0 to 0, resulting in 0 === -0 being folded to true.

How did you test this change?

See attached tests

) {
const result: Primitive = {
kind: 'Primitive',
value: operand.value * -1,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're only operating on numbers here. Part in the spec:
https://tc39.es/ecma262/2024/#sec-unary-minus-operator-runtime-semantics-evaluation

@josephsavona josephsavona merged commit 946da51 into facebook:main May 7, 2025
20 checks passed
@nikeee nikeee deleted the unary-minus branch May 7, 2025 17:16
github-actions bot pushed a commit that referenced this pull request May 7, 2025
## Summary
`-constant` is represented as a `UnaryExpression` node that is currently
not part of constant folding. If the operand is a constant number, the
node is folded to `constant * -1`. This also coerces `-0` to `0`,
resulting in `0 === -0` being folded to `true`.

## How did you test this change?
See attached tests

DiffTrain build for [946da51](946da51)
github-actions bot pushed a commit that referenced this pull request May 7, 2025
## Summary
`-constant` is represented as a `UnaryExpression` node that is currently
not part of constant folding. If the operand is a constant number, the
node is folded to `constant * -1`. This also coerces `-0` to `0`,
resulting in `0 === -0` being folded to `true`.

## How did you test this change?
See attached tests

DiffTrain build for [946da51](946da51)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants