Closed Bug 1599163 Opened 5 years ago Closed 5 years ago

Nullish coalescing operator returns undefined for void expressions

Categories

(Core :: JavaScript Engine, defect, P1)

72 Branch
defect

Tracking

()

RESOLVED FIXED
mozilla72
Tracking Status
firefox-esr68 --- unaffected
firefox70 --- unaffected
firefox71 --- unaffected
firefox72 + fixed

People

(Reporter: wartmanm, Assigned: yulia)

References

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0

Steps to reproduce:

I was trying out new language features, as one does, and typed in (void 0) ?? "test" in the console.

Actual results:

The resulting value was undefined.

Expected results:

I think the correct result is "test". Assigning void 0 to a temporary value produces that result, as does the original expression in Chromium.

I'm not sure when an expression like this would ever come up in practice, but I wanted to make a note of it just in case.

Component: Untriaged → JavaScript Engine
Product: Firefox → Core

It looks like FoldAndOrCoalesce just needs to handle ParseNodeKind::VoidExpr.

Blocks: 1566141

[Tracking Requested - why for this release]:
New feature.

Status: UNCONFIRMED → NEW
Ever confirmed: true
Priority: -- → P1
Assignee: nobody → ystartsev
Status: NEW → ASSIGNED

mattheww, great catch. Thank you so much for the bug report. I think you saved us some real embarrassment.

The bug is fun. It happens in a part of the JS engine where we examine source code and try to simplify it a bit, "algebraically", before running it, an optimization called constant folding. So for example, if you have a function that does let x = 60 * 60;, we simplify that to let x = 3600; before anything runs -- the function will run faster if we multiply once, up front, rather than every time the function is called. Constant folding also throws away most if (false) { ... } statements, and so on.

Unfortunately, in this case we were erroneously assuming that the only constant expressions that evaluate to null or undefined were null and undefined. As you found, there's one more we need to account for.

Pushed by ystartsev@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/9dbf3a2c8c37 fix nullish coalescing operator for (void 0) case; r=jorendorff
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla72
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: