Skip to content

Engine produces a misleading error when encountering certain class shapes #67180

Open

Description

@RareByteStream

Version

nodejs: v26.1.0
[Chromium 156.0.8078.47]: V8 15.6.75.19
[Brave 1.98.63]: V8 15.6.75.27

Platform

Linux <omitted-hostname> 7.0.0-38-generic #38~24.04.4-Ubuntu SMP PREEMPT_DYNAMIC Mon Sep 14 16:37:11 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

Might be "ParserBase" and "ParseClassLiteralBody" of "deps/v8/src/parsing". Still not sure though because I didn't investigate deeply and I am unfamiliar with the overall architecture of the project.

What steps will reproduce the bug?

class A {

    foo () {
      this.#_x_ = this.#_x_;
      if ( true ) {
        const y = 1;
        let y = 1;
        y = this.#_x_;
      }
      return this.#_x_;
    };

    #_x_ = 1;

}

test.js

How often does it reproduce? Is there a required condition?

For the misleading error to occur, private field has to be declared after a method that uses it. Declaring the field before the method that uses it fixes the issue. In my tests, nesting didn't matter. However, I have been coming across this issue since long ago and nuances may differ from one version or platform to another one.

What is the expected behavior? Why is that the expected behavior?

Parsing is supposed to result in redeclaration of const y error. Order of private field declaration in comparison to order of a method that uses it does not prove field being undefined at the whole class body. Therefore the behaviour specified above is a bug that misleads the developer.

What do you see instead?

Parsing results in Private field '#_x_' must be declared in an enclosing class error, which is misleading. Because the order of field declaration aborts the parsing phase, inner errors get skipped. In cases where faulty code is absent within a class body, development is even more enigmatic especially when working on big classes.

Additional information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions