Semantic Diff

#9310 Reject an external @scope whose global JavaScript cannot name

1 commit

3dcad0a272a9c55432cb55bbd084fe7eb8c28c58..pr-9310

1605436dd · tsnobip + Claude · Nov 16, 2026, 13:40 UTC

Reject an external @scope whose global JavaScript cannot name

Why

An external's @scope was only checked for being strings, and without @module the first one names a global, printed as is: @scope("class") and @scope("foo-bar") on a @val emitted class.b, a syntax error, and foo-bar.d, a subtraction. #9309 checks the @scope of an @instanceOf type, and its review suggested sharing that parsing with externals. Ast_payload.scopes_of_payload now reads the payload for both and checks the global the properties start at.

addedDiagnostic an external @scope whose global JavaScript cannot name

@scope("class") @val external b: int = "b"@scope("foo-bar") @val external d: int = "d"let x = b + d
js
Before
let x = class.b + foo-bar.d | 0;
diagnostic
After one error per external, each compiled alone
Without @module, the first property of @scope is a global, which cannot be named like the keyword "class". To read a property of the global object of that name, put "globalThis" first in @scope.Without @module, the first property of @scope is a global, so it must be an identifier, and "foo-bar" is not one. To read a property of the global object of that name, put "globalThis" first in @scope.

Following the advice, @scope(("globalThis", "class")) and @scope(("globalThis", "foo-bar")) compile to globalThis.class.b + globalThis["foo-bar"].d | 0.

modifiedDiagnostic an external @scope that is not strings

@scope @val external x: int = "x"@scope(("window", 3)) @val external y: int = "y"
diagnostic
Before one error per external, each compiled alone
After one error per external, each compiled alone
Illegal attributesexpect string tuple list@scope on an external expects the properties its value is read through, such as @scope("document") or @scope(("window", "location")).@scope on an external expects the properties its value is read through, such as @scope("document") or @scope(("window", "location")).

modifiedRepresentation which properties of a @scope are checked

representation
Before Ast_external_process.parse_external_attributes, Ast_untagged_variants.strings_of_payload
After Ast_payload.scopes_of_payload
@val @scope("class") ↦ class.v, a syntax error@val @scope("foo-bar") ↦ foo-bar.v, a subtraction@new @scope("default") ↦ new (default.K)(), a syntax error@val @scope("class") ↦ error: a global named like a keyword@val @scope("foo-bar") ↦ error: a global that is not an identifier@new @scope("default") ↦ error: a global named like a keyword@val @scope(("import", "meta")) ↦ import.meta.v@val @scope(("import", "meta")) ↦ import.meta.v@val @scope(("a", "foo-bar")) ↦ a["foo-bar"].v@val @scope(("a", "foo-bar")) ↦ a["foo-bar"].v@module("m") @scope("foo-bar") ↦ M["foo-bar"].v@module("m") @scope("foo-bar") ↦ M["foo-bar"].v@send @scope("class") ↦ o.class.f()@send @scope("class") ↦ o.class.f()
notean external's properties are only checked for being strings
@scope(("import", "meta")) @instanceOf("C") ↦ error: a global named like a keyword
notethe first property is a global for a @val or @new without @module, and for an @instanceOf type without @module
notea class also needs every property to be an identifier, and no "globalThis" first

The properties after the global may be any string for an external, as before: they are read with a computed access where they need one, as in a["foo-bar"]. A class keeps its own rules: its path is a string of identifiers and dots, and globalThis in front would give one class two identities. @scope("globalThis") stays valid on an external. Class errors are unchanged, except that @scope(("import", "meta")), which an external may use, is reported at the @scope of a class rather than as an invalid @instanceOf. The external's @scope is now read once the kind of external is known, since @module can follow it.

Review

  • Why accept import as the first property of an external's @scope? @scope(("import", "meta")) reads import.meta, which is valid JavaScript and how bindings read import.meta.url or import.meta.env (tests/docstring_tests/Node.res does).
  • Why is the first property of a @send, @get or @set not checked? It is a property of the object the external is called on, as in o.class.f(), never a global.