Skip to content

[JSC] Import and export declarations should not redeclare a var in module code - #77373

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/import-and-export-declarations-should-not-redeclare-a-var-in-module-code
Oct 20, 2026
Merged

webkit-commit-queue merged 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/import-and-export-declarations-should-not-redeclare-a-var-in-module-code

Conversation

@sosukesuzuki

@sosukesuzuki sosukesuzuki commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

f88678d

[JSC] Import and export declarations should not redeclare a var in module code
https://bugs.webkit.org/show_bug.cgi?id=327927

Reviewed by Yusuke Suzuki.

It is a Syntax Error if a LexicallyDeclaredName of a module also occurs in its
VarDeclaredNames [1], but JSC accepted the following when the var came first.

    var x; import { y as x } from "./m.js";
    var x; export let x;
    var x; export class x { }

Debug builds then hit ASSERT(symbolTableEntry.isNull()) in
BytecodeGenerator::instantiateLexicalVariables.

Parser::declareVariable compares a lexical name with vars only when
m_statementDepth is 1, and import and export declarations were parsed at depth 0.
This patch parses them at depth 1, the depth of top-level statements, by
incrementing it in parseImportDeclaration and parseExportDeclaration as
parseStatementListItem does. Setting the depth to 1 only for exported function
declarations is no longer needed.

[1]: https://tc39.es/ecma262/#sec-module-semantics-static-semantics-early-errors

Tests: JSTests/stress/bigint-to-string-divide-and-conquer-large.js
       JSTests/stress/modules-syntax-error-import-and-export-declarations-after-var.js

* JSTests/stress/bigint-to-string-divide-and-conquer-large.js: Added.
(shouldBe):
* JSTests/stress/modules-syntax-error-import-and-export-declarations-after-var.js: Added.
(shouldThrowModuleSyntaxError):
(export.x):
(export.async x):
(export.default.x):
(export.default.async x):
(const.declaration.of.declarations.const.statement.of.f):
(export.default):
(export.C):
(export.let.y):
* JSTests/stress/modules-syntax-error-with-names.js:
* Source/JavaScriptCore/parser/Parser.cpp:
(JSC::Parser<LexerType>::parseImportDeclaration):
(JSC::Parser<LexerType>::parseExportDeclaration):

Canonical link: https://commits.webkit.org/324223@main

b2b479c

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win
✅ 🛠 ios-sim ⏳ 🛠 mac-cmake-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe
✅ 🧪 ios-wk2-wpt ⏳ 🧪 api-mac-cmake-debug ✅ 🧪 jsc-wpe
✅ 🧪 jsc-x86-64 ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk3-gcc
✅ 🛠 🧪 jsc-debug-arm64 ✅ 🛠 ios-safer-cpp ⏳ 🧪 mac-cmake-debug-wk2 ✅ 🛠 gtk
✅ 🛠 vision ✅ 🧪 gtk-wk2
✅ 🛠 🧪 merge ✅ 🛠 vision-sim ✅ 🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🛠 mac-safer-cpp ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@sosukesuzuki
sosukesuzuki requested a review from a team as a code owner October 17, 2026 12:47
@sosukesuzuki sosukesuzuki self-assigned this Oct 17, 2026
@sosukesuzuki sosukesuzuki added the JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues. label Oct 17, 2026
robobun added a commit to oven-sh/WebKit that referenced this pull request Oct 20, 2026
…dule code

The Parser.cpp change is the one of WebKit#77373 (b2b479c,
https://bugs.webkit.org/show_bug.cgi?id=327927), unchanged. The test is new.

It is a Syntax Error if a LexicallyDeclaredName of a module also occurs in its
VarDeclaredNames, but JSC accepted a module when the var came first:

    var x; import { y as x } from "./m.js";
    var x; export let x;
    var x; export class x { }

Builds with assertions then hit ASSERT(symbolTableEntry.isNull()) in
BytecodeGenerator::instantiateLexicalVariables. Release builds ran the module
with the slot of the var. This module crashed them on a read of the empty value:

    if (globalThis.c) var p = 2; import * as p from "./m.js"; let t = typeof p;

Parser::declareVariable compares a lexical name with the vars only when
m_statementDepth is 1, and import and export declarations were parsed at
depth 0. They are now parsed at depth 1, the depth of a top-level statement.
The four places that set the depth to 1 for an exported function declaration
are no longer needed.

* JSTests/stress/modules-syntax-error-var-then-import-or-export.js: Added.
* Source/JavaScriptCore/parser/Parser.cpp:
(JSC::Parser<LexerType>::parseImportDeclaration):
(JSC::Parser<LexerType>::parseExportDeclaration):

Co-authored-by: Sosuke Suzuki <[email protected]>
@sosukesuzuki sosukesuzuki added the merge-queue Applied to send a pull request to merge-queue label Oct 20, 2026
…dule code

https://bugs.webkit.org/show_bug.cgi?id=327927

Reviewed by Yusuke Suzuki.

It is a Syntax Error if a LexicallyDeclaredName of a module also occurs in its
VarDeclaredNames [1], but JSC accepted the following when the var came first.

    var x; import { y as x } from "./m.js";
    var x; export let x;
    var x; export class x { }

Debug builds then hit ASSERT(symbolTableEntry.isNull()) in
BytecodeGenerator::instantiateLexicalVariables.

Parser::declareVariable compares a lexical name with vars only when
m_statementDepth is 1, and import and export declarations were parsed at depth 0.
This patch parses them at depth 1, the depth of top-level statements, by
incrementing it in parseImportDeclaration and parseExportDeclaration as
parseStatementListItem does. Setting the depth to 1 only for exported function
declarations is no longer needed.

[1]: https://tc39.es/ecma262/#sec-module-semantics-static-semantics-early-errors

Tests: JSTests/stress/bigint-to-string-divide-and-conquer-large.js
       JSTests/stress/modules-syntax-error-import-and-export-declarations-after-var.js

* JSTests/stress/bigint-to-string-divide-and-conquer-large.js: Added.
(shouldBe):
* JSTests/stress/modules-syntax-error-import-and-export-declarations-after-var.js: Added.
(shouldThrowModuleSyntaxError):
(export.x):
(export.async x):
(export.default.x):
(export.default.async x):
(const.declaration.of.declarations.const.statement.of.f):
(export.default):
(export.C):
(export.let.y):
* JSTests/stress/modules-syntax-error-with-names.js:
* Source/JavaScriptCore/parser/Parser.cpp:
(JSC::Parser<LexerType>::parseImportDeclaration):
(JSC::Parser<LexerType>::parseExportDeclaration):

Canonical link: https://commits.webkit.org/324223@main
@webkit-commit-queue
webkit-commit-queue force-pushed the eng/import-and-export-declarations-should-not-redeclare-a-var-in-module-code branch from b2b479c to f88678d Compare October 20, 2026 11:04
@webkit-commit-queue

Copy link
Copy Markdown
Collaborator

Committed 324223@main (f88678d): https://commits.webkit.org/324223@main

Reviewed commits have been landed. Closing PR #77373 and removing active labels.

@webkit-commit-queue
webkit-commit-queue merged commit f88678d into WebKit:main Oct 20, 2026
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants