Repository navigation
Conversation
Why:
`@module` and `@scope` on an `@instanceOf` type were silently ignored
(#9239), so the class check of an untagged variant read a global of the
class's name. For a class of a JavaScript library or a local file that
global does not exist, and the check threw a ReferenceError. Like an
external, the type now says where its class is found: `@module("m")`
reads it among the exports of `m`, `"default"` for the default export,
imported as a `@module` external imports it, and `@scope` puts its
properties in front of the path.
JS modified (class check of an @instanceof type):
Example:
@instanceof("Object3D") type object3D
@module("three") @instanceof("Mesh") type mesh = private object3D
@scope("THREE") @instanceof("Line") type line = private object3D
@unboxed
type shape = Mesh(mesh) | Line(line) | Other(object3D)
let kind = s =>
switch s {
| Mesh(_) => "mesh"
| Line(_) => "line"
| Other(_) => "other"
}
Before (js):
function kind(s) {
if (s instanceof Mesh) {
return "mesh";
} else if (s instanceof Line) {
return "line";
} else {
return "other";
}
}
After (js):
let Three = require("three");
function kind(s) {
if (s instanceof Three.Mesh) {
return "mesh";
} else if (s instanceof THREE.Line) {
return "line";
} else {
return "other";
}
}
Representation modified (where the class of an @instanceof type is found):
Before (representation): Ast_untagged_variants.process_runtime_annotation
@instanceof("Mesh") ↦ the global Mesh
@module("three") @instanceof("Mesh") ↦ the global Mesh
@scope("THREE") @instanceof("Mesh") ↦ the global Mesh
note: a class is identified by its path
After (representation): Ast_untagged_variants.class_of_attributes, Lam_compile_external_call.class_constructor
@instanceof("Mesh") ↦ the global Mesh
@module("three") @instanceof("Mesh") ↦ Three.Mesh, with Three the import of "three"
@module("./shape.js") @instanceof("default") ↦ the default import of "./shape.js"
@scope("THREE") @instanceof("Mesh") ↦ the global THREE.Mesh, as @instanceof("THREE.Mesh")
@module("lib") @notundefined ↦ never undefined, @module ignored as before
note: a class is identified by its path and its module
Diagnostic removed (two classes of one name in a variant):
Example:
@instanceof("Mesh") type globalMesh
@module("three") @instanceof("Mesh") type mesh
@unboxed
type t = Global(globalMesh) | Three(mesh) | Plain({"plain": bool})
let kind = x =>
switch x {
| Global(_) => "global"
| Three(_) => "three"
| Plain(_) => "plain"
}
Before (diagnostic):
This untagged variant definition is invalid: At most one case can be an instance of Mesh.
After (js):
let Three = require("three");
function kind(x) {
if (x instanceof Mesh) {
return "global";
} else if (x instanceof Three.Mesh) {
return "three";
} else {
return "plain";
}
}
Diagnostic added (an interface that states another module):
Example:
module M: {
@module("three") @instanceof("Mesh") type mesh
} = {
@instanceof("Mesh") type mesh
}
Before (diagnostic): none, both declarations stated the global Mesh
After (diagnostic):
Signature mismatch:
Actual declaration (Example.res:4:23-31):
@instanceof("Mesh") type mesh
Expected declaration (Example.res:2:40-48):
@module("three") @instanceof("Mesh") type mesh
Their runtime representations differ:
Actual declaration: instances of Mesh
Expected declaration: instances of Mesh from "three"
Diagnostic modified (an @instanceof payload that is not a string):
Example:
@module("three") @instanceof(Mesh) type mesh
Before (diagnostic):
@instanceof expects the name of a global class, such as @instanceof("Date") or @instanceof("Intl.Locale").
After (diagnostic):
@instanceof expects the name of a class that "three" exports, such as @instanceof("Mesh"), or @instanceof("default") for its default export.
Diagnostic added (a @scope that cannot lead to a class):
Example:
@scope("default") @instanceof("Mesh") type mesh
Before (diagnostic): none, @scope was ignored and the check read the global Mesh
After (diagnostic):
Without @module, the first property of @scope is a global, which cannot be named like the keyword "default".
Diagnostic added (a @module that names no module):
Example:
@module(("three", "THREE")) @instanceof("Mesh") type mesh
Before (diagnostic): none, @module was ignored and the check read the global Mesh
After (diagnostic):
@module on an @instanceof type expects the module that exports the class, such as @module("three") or @module("./shapes.js").
Review: Why are @module and @scope still ignored on a @typeof or @notundefined type? @notundefined shipped in v12, so an error there would break bindings that compile today, and without a class the attributes cannot make a check read the wrong value, which was the harm in #9239.
Review: Where is a relative path such as @module("./x.js") resolved? It is emitted as written, relative to the module that compiles the check, as for a @module external used from another module.
Review: Why does a class of a module skip the checks of global paths such as Object or Array? Those name the builtin classes only as globals; an export can be named anything, also like a keyword such as default.
Fixes: #9239
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Paul Tsnobiladzé <2479216+tsnobip@users.noreply.github.com>
Codecov's patch report on #9309 listed 10 uncovered lines in `ast_untagged_variants.ml`. Five were real gaps, now tested: the tuple form of `@scope` (the runtime test reads a class through two properties), a `@scope` with a non-string or no payload, `@module("")`, and a non-string `@instanceOf` without `@module`. The other five were covered but not measurable. The `@scope` errors went through a helper that always raises, so bisect never reached the point after the call; they now raise directly. The fallback for attributes without a location, which source code never reaches, is now one shared function instead of three copies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Paul Tsnobiladzé <2479216+tsnobip@users.noreply.github.com>
Why:
`@module` and `@scope` on a type declaration without `@instanceOf` had
no effect and were ignored without a word. The same silence on an
`@instanceOf` type is how #9239 went unnoticed. Now that the two
attributes say where the class of an `@instanceOf` type is found, they
are an error on any other type, as suggested in the review of #9309.
Diagnostic added (@module or @scope on a type without @instanceof):
Example:
@module("three") type mesh
@scope("THREE") @notundefined type line
@module("x") type record = {a: int}
Before (diagnostic): none, the attributes were ignored
After (diagnostic): one error per declaration, each compiled alone
@module on a type declaration names the module a class is imported from, so it needs @instanceof too. Add @instanceof with the name of the class, or remove @module.
@scope on a type declaration names the properties a class is read through, so it needs @instanceof, and this type has @notundefined instead. Remove @scope, or write @instanceof instead of @notundefined if its values are instances of a class.
@module on a type declaration names the module a class is imported from, so it needs @instanceof, which only abstract types and type abbreviations can have. Remove @module.
The advice depends on the declaration, since adding `@instanceOf` only
works on an abstract type or an abbreviation without another annotation.
A record with both `@typeOf` and `@module` still reports the misplaced
`@typeOf` first. The `@instanceOf` completion doc no longer calls the class
global, since it can come from a module.
Review: Does an interface compiled by an older compiler still load? Yes: the check runs in `Typedecl.transl_declaration`, where a declaration is type-checked, and `process_runtime_annotation`, which also reads declarations from .cmi files, does not raise it.
Breaking: `@module` or `@scope` on a type declaration without `@instanceOf` no longer compiles. They never had an effect there: remove them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Paul Tsnobiladzé <2479216+tsnobip@users.noreply.github.com>
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.
Diagnostic added (an external @scope whose global JavaScript cannot name):
Example:
@scope("class") @Val external b: int = "b"
@scope("foo-bar") @Val external d: int = "d"
let x = b + d
Before (js):
let x = class.b + foo-bar.d | 0;
After (diagnostic): 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`.
Diagnostic modified (an external @scope that is not strings):
Example:
@scope @Val external x: int = "x"
@scope(("window", 3)) @Val external y: int = "y"
Before (diagnostic): one error per external, each compiled alone
Illegal attributes
expect string tuple list
After (diagnostic): one error per external, each compiled alone
@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")).
Representation modified (which properties of a @scope are checked):
Before (representation): Ast_external_process.parse_external_attributes, Ast_untagged_variants.strings_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(("import", "meta")) ↦ import.meta.v
@Val @scope(("a", "foo-bar")) ↦ a["foo-bar"].v
@module("m") @scope("foo-bar") ↦ M["foo-bar"].v
@send @scope("class") ↦ o.class.f()
note: an external's properties are only checked for being strings
After (representation): Ast_payload.scopes_of_payload
@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(("a", "foo-bar")) ↦ a["foo-bar"].v
@module("m") @scope("foo-bar") ↦ M["foo-bar"].v
@send @scope("class") ↦ o.class.f()
@scope(("import", "meta")) @instanceof("C") ↦ error: a global named like a keyword
note: the first property is a global for a @Val or @new without @module, and for an @instanceof type without @module
note: a 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).
Review: 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.
Breaking: an external `@val` or `@new` without `@module` whose `@scope` starts with a string that is not an ASCII identifier, or with a JavaScript keyword other than the `import` of `import.meta`, no longer compiles. Most of these emitted invalid JavaScript. Those that did not, such as `@scope("eval")` or a global with a non-ASCII name, read the same global with "globalThis" first in `@scope`; `@scope("this")` or `@scope("true")` read no global.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Paul Tsnobiladzé <2479216+tsnobip@users.noreply.github.com>
Changelog🐛 Bug fix
💥 A commit says what it breaks ( Semantic Diff1 commit · Full page Reject an external
|
|
@tsnobip can you rebase? |
Stacked on #9309 (branch
module-instanceof), as suggested in its review: the@scopeparsing that #9309 added for@instanceOftypes is now shared with externals, which never checked the global their@scopestarts at.What changes
Ast_payload.scopes_of_payloadreads a@scopepayload for both externals and@instanceOftypes: a non-empty string or tuple of strings, whose first property, when it is a global, must be an identifier not named like a keyword. Without@module, the first property of an external@valor@newis a global that the JavaScript output prints as is, socompiled to
let x = class.b + foo-bar.d | 0;, a syntax error and a subtraction. Each external is now an error at its@scope:Following the advice gives
globalThis.class.b + globalThis["foo-bar"].d | 0. A@scopethat is not strings now says what it expects instead of "Illegal attributes" or "expect string tuple list".Decisions
E.dot→a["foo-bar"]), which is valid JavaScript, so@scope(("a", "foo-bar"))still compiles toa["foo-bar"].e. A class still needs every property to be an identifier, since its path is a dotted string.@valor@newwithout@modulestarts at a global. A@modulescope is a property of the module (M["foo-bar"]), and a@send,@get,@set,@get_indexor@set_indexscope a property of the object (o.class.f()), so those are only checked for being strings.@scope("globalThis")stays valid on externals.import.metais accepted.@scope(("import", "meta"))starts with a keyword but is valid JavaScript and a common binding (tests/docstring_tests/Node.resuses it). A class rejects it at its@scope.Js_reserved_map.is_js_keyword). It also containsthis,true,false,null,evalandarguments, so@scope("this")(this.x), which printed valid JavaScript, is now rejected too, as is a global with a non-ASCII name. The commit notes these underBreaking:, while the changelog entry is a fix since they read no global or are very rare. If you would rather keep them compiling, the check can be narrowed to the keywords that makekw.xa syntax error.The external's
@scopeis now read once the kind of external is known, since@modulemay come after it.Tests
super_errorsfixtures:external_scope_keyword,external_scope_not_identifier,external_new_scope_keyword,external_scope_not_strings, andinstance_of_scope_import_meta.bs_illegal_attribute_scopegets the new message.instance_of_invalid_scope,instance_of_scope_keyword,instance_of_scope_global_this,instance_of_scope_not_strings,instance_of_scope_without_payload,scope_without_instance_of) produce the same output.tests/tests/src/external_scope_properties.respins the JavaScript of the forms that stay valid.make testpasses except theTemporaldocstring tests (the local Node has noTemporalglobal);make test-analysispasses.🤖 Generated with Claude Code