Closed Bug 1770609 Opened 2 years ago Closed 2 years ago

Invalid use of declared private field reports incorrect line number

Categories

(Core :: JavaScript Engine, defect, P3)

Firefox 100
defect

Tracking

()

RESOLVED FIXED
102 Branch
Tracking Status
firefox102 --- fixed

People

(Reporter: blmatthews, Assigned: mgaudet)

References

Details

Attachments

(2 files)

Attached file private.html

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:100.0) Gecko/20100101 Firefox/100.0

Steps to reproduce:

Save the attached file and open it in Firefox with Tool > Browser Tools > Web Developer Tools open and on the Console (or Debugger) tab. Then edit the file and uncomment line 15.

Actual results:

With the file as attached (line 15 commented out), the console reports, as expected:

Uncaught SyntaxError: reference to undeclared private field or method #undeclaredPrivateField private.html:16:8

However, with line 15 uncommented, the console reports:

Uncaught SyntaxError: reference to undeclared private field or method #declaredPrivateField private.html:7:3

A couple of problems. First, the field isn't undeclared (although I suppose it's undeclared in the scope it's being used in), and second, and more problematic, the line number is the line number it's declared on, not where it's being used.

Expected results:

With line 15 uncommented the error should reference line 16.

Note that although m isn't (explicitly) declared and doesn't have a value, that doesn't matter, anywhere you attempt to invalidly reference a declared private field the error reports the line as the line the field is declared on, not where it's being referenced as one would expect. Private methods have the same problem.

Oops, of course the error should reference line 15, not 16.

The Bugbug bot thinks this bug should belong to the 'Core::JavaScript Engine' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → JavaScript Engine
Product: Firefox → Core
Severity: -- → S3
Priority: -- → P3

I can also reproduce this in the shell:

class Mainclass {
    #declaredPrivateField = 42;	// Line 2
}

m.#declaredPrivateField = 23;	// Line 5
m.#undeclaredPrivateField = 23;	// Line 6

With line 5 commented out we get

/Users/mgaudet/tmp/t.js:6:2 SyntaxError: reference to undeclared private field or method #undeclaredPrivateField:
/Users/mgaudet/tmp/t.js:6:2 m.#undeclaredPrivateField = 23;     // Line 6
/Users/mgaudet/tmp/t.js:6:2 ..^

With line 5 un-commented, we get:

/Users/mgaudet/tmp/t.js:2:4 SyntaxError: reference to undeclared private field or method #declaredPrivateField

My recollection was that we stored information in the used names tracker to handle these sorts of error message problems, but this is definitely a huge paper cut.

Status: UNCONFIRMED → NEW
Ever confirmed: true

ni? myself to look at this when I've got some spare time.

Flags: needinfo?(mgaudet)

A bit of investigation:

Both errors are issued from the same place, as we have found undeclared private names in the provided scope.

The reason we're getting the wrong debug info for this when the private name is declared elsewhere is that in the UsedNameTracker, we only track the first use position. Which made sense at one point, but this bug shows pretty clearly that this can result in very confusing error messages.

I'll bet this can be rectified relatively easily: If the vector of uses is empty when adding a new one, we should reset the firstUsePosition...

Flags: needinfo?(mgaudet)
Assignee: nobody → mgaudet
Status: NEW → ASSIGNED
Pushed by mgaudet@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/74a8cc31925f
Update private name token position when starting processing a new scope to ensure good diagnostics r=arai
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 102 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: