Repository navigation
Conversation
Why:
@module on an @instanceof type was silently ignored (#9239): the class
check read a global of that name, which throws a ReferenceError when the
class is only exported by a module. @scope was ignored the same way.
A class is now identified by its path and its module. The check imports
the class as the value of a @module external is imported: a named export,
"default", or a property of an export such as "Addons.Line2".
JS modified (class check):
Example:
@instanceof("Object3D") type object3D
@module("three") @instanceof("Mesh") type mesh = private object3D
@unboxed
type shape = Mesh(mesh) | Other(object3D)
let isMesh = (s: shape) =>
switch s {
| Mesh(_) => true
| Other(_) => false
}
Before (js):
function isMesh(s) {
return s instanceof Mesh;
}
After (js):
let Three = require("three");
function isMesh(s) {
return s instanceof Three.Mesh;
}
Representation modified (class identity):
Before (representation):
@instanceof("Mesh") ↦ global Mesh
@module("three") @instanceof("Mesh") ↦ global Mesh
After (representation):
@instanceof("Mesh") ↦ global Mesh
@module("three") @instanceof("Mesh") ↦ export Mesh of "three"
@module("./Widget.js") @instanceof("default") ↦ default export of "./Widget.js"
note: the two Mesh classes are different: an untagged variant may have a case for each, and an interface must state the same module
Diagnostic added (@module without @instanceof):
Example:
@module("three") type mesh
After (diagnostic):
@module on a type declaration names the module a class is imported from, so it needs @instanceof too, such as @module("three") @instanceof("Mesh").
Diagnostic added (@scope on a type):
Example:
@scope("THREE") @instanceof("Mesh") type mesh
After (diagnostic):
@scope has no effect on a type declaration. Write the class's path in @instanceof instead: @instanceof("THREE.Mesh").
Diagnostic modified (interface with another module):
Example:
module Three: {
@instanceof("Mesh") type mesh
} = {
@module("three") @instanceof("Mesh") type mesh
}
After (diagnostic):
Their runtime representations differ:
Actual declaration: instances of Mesh from "three"
Expected declaration: instances of Mesh
Review: Why an error for @scope instead of supporting it? A dotted path in @instanceof already says the same, @instanceof("THREE.Mesh"), so one spelling is enough.
Review: How is a relative module path emitted? As written, like the module of a @module external.
Breaking: @module or @scope on a type declaration without a class was ignored and is now an error. Remove it, or for @scope write the path in @instanceof.
Fixes #9239
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGyNsDT2sgyUywYMGs
Signed-off-by: Christoph Knittel <ck@cca.io>
Changelog🐛 Bug fix
💥 A commit says what it breaks ( Semantic Diff1 commit · Full page Import the class of an
|
✅ Compiler benchmarksAllocated wordsIn total: 640,949,204 → 641,045,673 (+0.02%). Generated projects: 920,891,968 → 920,997,174 (+0.01%).
7 more changes
InstructionsIn total: 9,209,713,949 → 9,209,476,755 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn total: 25,843,406 → 25,846,280 (+0.01%).
Pull request 3a07f48 compared with base a531dc3 in this run. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9308 +/- ##
==========================================
+ Coverage 82.06% 82.07% +0.01%
==========================================
Files 455 455
Lines 65399 65434 +35
==========================================
+ Hits 53672 53708 +36
+ Misses 11727 11726 -1
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9308 |
|
Closing in favor of #9309, which does the same and covers more (@scope like externals, escaping, cross-module test). One thing from this PR that may be worth keeping: an error for |
Fixes #9239.
@modulebeside@instanceOfwas silently ignored, so the class check read a global and threw aReferenceErrorat runtime. Now the check imports the class the same way a@moduleexternal is imported:Meshfrom"three"and the globalMeshare different classes, so an untagged variant can have a case for each, and an interface has to state the same module."default"names the default export, and a dotted path such as"Addons.Line2"names a property of an export.@modulewithout@instanceOfon a type is an error, and so is@scopeon a type, with a hint to write the path instead:@instanceOf("THREE.Mesh"). Both used to be ignored.This is step 0 for the class-identity idea discussed in #9058: the coercion rules there rely on class identity including the module.
New tests:
tests/tests/src/untagged_module_classes_test.res(named, default and nested exports, plus an export and a global with the same name), and sevensuper_errorsfixtures for the new errors and messages.🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gGyNsDT2sgyUywYMGs