Closed Bug 1887677 Opened 2 months ago Closed 2 months ago

Wrong private fields scope in class computed keys that contain another class

Categories

(Core :: JavaScript Engine, task)

task

Tracking

()

RESOLVED FIXED
126 Branch
Tracking Status
firefox126 --- fixed

People

(Reporter: nicolo.ribaudo, Assigned: anba)

Details

Attachments

(1 file)

Code:

let capturedPrivateAccess;
class A {
  static #x = 42;
  static [(class {}, capturedPrivateAccess = () => A.#x)];
}
console.log(capturedPrivateAccess());

This should log 42, but it throws SyntaxError: reference to undeclared private field or method #x.

We noticed it because Babel generates a similar pattern when transpiling decorators, and the generated code doesn't work in Firefox (https://github.com/babel/babel/issues/16379).

As a workaround, we are probably going to wrap the computed key in an arrow function, as this code works:

let capturedPrivateAccess;
class A {
  static #x = 42;
  static [(() => (class {}, capturedPrivateAccess = () => A.#x))()];
}
console.log(capturedPrivateAccess());

I took a quick peek at this. Here's an even more reduced version:

class A {
  static #x = 42;
  static [class {}];
}

I'm not an expert here, but it looks like there's something going wrong with used name tracking and nested classes. When we parse the inner class, isInClass is false for some reason.

Looks like Matt is taking the week off. Arai, do you know how this is supposed to work, or should we just wait for Matt to get back?

Flags: needinfo?(arai.unmht)

SharedContext::inClass() returns whether the current SharedContext is within
a class definition. A class definition itself doesn't create a new SharedContext,
so an inner nested class definition within a computed property key of an outer
class definition was treated as if it wasn't nested within a class definition. This
led to reporting a SyntaxError when checking for unbound private names.

Calling ParseContext::findInnermostStatement() in addition to SharedContext::inClass()
fixes this issue. (We need to keep SharedContext::inClass() to correctly handle
lazily parsed classes.)

Assignee: nobody → andrebargull
Status: NEW → ASSIGNED
Pushed by andre.bargull@gmail.com:
https://hg.mozilla.org/integration/autoland/rev/ec85604517f3
Correctly compute inClass predicate when validating unbound private names. r=dminor
Flags: needinfo?(arai.unmht)
Status: ASSIGNED → RESOLVED
Closed: 2 months ago
Resolution: --- → FIXED
Target Milestone: --- → 126 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: