Repository navigation
Read the class of an @instanceOf type from its @module and @scope - #9309
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>
Changelog💥 Breaking Change
🐛 Bug fix
Semantic Diff3 commits · Full page Read the class of an
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9309 +/- ##
==========================================
+ Coverage 82.06% 82.09% +0.02%
==========================================
Files 455 455
Lines 65399 65493 +94
==========================================
+ Hits 53672 53765 +93
- Misses 11727 11728 +1
🚀 New features to boost your workflow:
|
✅ Compiler benchmarksAllocated wordsIn total: 640,952,301 → 640,986,911 (+0.01%). Generated projects: 920,898,990 → 920,932,012 (±0.00%).
InstructionsIn total: 9,209,714,195 → 9,209,211,371 (-0.01%).
Output sizeIn total: 25,843,406 → 25,848,367 (+0.02%).
Pull request 3dcad0a compared with base a531dc3 in this run. |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9309 |
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>
|
Suggestion: make Silently ignoring these attributes is how #9239 went unnoticed. Code that breaks only has an attribute that never did anything, and the fix is to remove it. That seems fine for v13, even though it's technically breaking. #9308 had this as It could also be a small follow-up PR after this one, if you'd rather keep this PR non-breaking. |
|
Follow-up idea, separate from this PR: parse This PR checks a class's @scope("class") @val external b: int = "b"
@scope("foo-bar") @val external d: int = "d"class.b // syntax error
foo-bar.d // runs as foo - bar.dA shared parser in
For externals this is a bug fix that only breaks code already emitting broken JS, so it would get its own PR and changelog entry. |
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>
@moduleand@scopeon an@instanceOftype were silently ignored, so the class check of an untagged variant read a global of the class's name (#9239). For a class from a JavaScript library or a local file that global doesn't exist, and the check threw aReferenceError.In an
@unboxedvariant of these, the checks weres instanceof Meshands instanceof Line, with no import. They are nows instanceof Three.Mesh, withthreeimported the way a@moduleexternal imports it, ands instanceof THREE.Line.@module("m")reads the@instanceOfpath among the exports ofm, with"default"for the default export.@scopeputs its properties in front of the path, so@scope("THREE") @instanceOf("Mesh")means@instanceOf("THREE.Mesh"). The check builds its constructor withtranslate_scoped_module_val, the code@moduleexternals use, so the module that compiles the match gets the import, wherever the type is declared.Variant_runtime.Instance.class_name). A globalMeshand theMeshthat"three"exports are two cases of one variant, an interface must state the same module as its implementation, and subclass ordering compares both. Errors print such a class asMesh from "three", and printed declarations show@module("three") @instanceOf("Mesh").@moduleisn't a single string;@module, the@instanceOfpath isn't a valid export path;@scopeisn't a list of property names;@module, the first scope is a keyword orglobalThis.@moduleor@scopeon a type declaration without@instanceOf(a plain type, a record, a@typeOfor@notUndefinedtype) is now an error instead of being ignored, as suggested in review. Silently ignoring them is how @module on an @instanceOf type is silently ignored #9239 went unnoticed. Code that breaks only has an attribute that never did anything, and the migration is to remove it. This has its ownbreakingchangelog fragment. The check runs where a declaration is type-checked, so interfaces already compiled by an older compiler still load.A known limitation, shared with
@moduleexternals: a relative path such as@module("./x.js")is emitted as written, relative to the module that compiles the check.Tests:
tests/tests/src/untagged_module_class_test.resruns named, scoped and default exports, a global that has an export's name, and a type declared in another module.The docs are updated in
compiler/ml/UntaggedVariants.mdand in the@instanceOfcompletion doc.Fixes #9239
Checklist
changes/README.md), or no release notes are needed and a maintainer should applychangelog:skip.CONTRIBUTING.md).git commit -s).🤖 Generated with Claude Code