NEW275145
Incorrect source location for TDZ error in modules when thrown before other statements
https://bugs.webkit.org/show_bug.cgi?id=275145
Summary Incorrect source location for TDZ error in modules when thrown before other s...
Jarred Sumner
Reported 2024-06-05 01:33:41 PDT
For the following input code: ``` // A statement which doesn't declare any variables Intl; const a = a; ``` When running `jsc` (correct, not a module): ``` jsc error.js Exception: ReferenceError: Cannot access uninitialized variable. global code@error.js:11:12 ``` When running `jsc -m` (incorrect, is a module): ``` jsc -m error.js Exception: ReferenceError: Cannot access uninitialized variable. module code@error.js:2:5 ``` It's pointing to the first statement in the function body instead of the location where it happened. Relevant issue: https://github.com/oven-sh/bun/issues/6824
Attachments
Jarred Sumner
Comment 1 2024-06-05 01:58:52 PDT
One way to fix it: call `emitExpressionInfo` before the `emitTDZCheckIfNecessary` call in `ResolveNode::emitBytecode`: https://github.com/WebKit/WebKit/blob/d7759dc9cbb90b3fda92a54521c132c18cc5a0df/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp#L263 ``` JSTextPosition divot = m_start + m_ident.length(); generator.emitExpressionInfo(divot, m_start, divot); ``` I'm guessing this is hot code though. There's probably a way to do this only when no expression info has been emitted before
Jarred Sumner
Comment 2 2024-06-05 02:54:40 PDT
Similar behavior occurs for `emitReadOnlyExceptionIfNeeded` The following input: ``` const a = 1; print(a); a = 2; ``` Emits the following error: ``` ❯ jsc -m /Users/jarred/Code/bun/src/bun.js/WebKit/a.js 1 Exception: TypeError: Attempted to assign to readonly property. module code@/Users/jarred/Code/bun/src/bun.js/WebKit/a.js:2:6 ``` The reported line number should be 3, which it is when not a module.
Radar WebKit Bug Importer
Comment 3 2024-06-05 20:14:36 PDT
Ahmad Saleem
Comment 4 2024-06-06 07:17:24 PDT
@Jarred - is it regression by any chance (if you know)?
Jarred Sumner
Comment 5 2024-06-06 20:09:23 PDT
@Ahmad I don't think this ever worked (for modules). We figured it was a bug in Bun because we had various bugs with sourcemaps, but we fixed the sourcemaps bug and this reproduces in the jsc shell.
Note You need to log in before you can comment on or make changes to this bug.