Skip to content

Import the class of an @instanceOf type that has @module - #9308

Closed
cknitt wants to merge 1 commit into
masterfrom
instance-of-module-9239
Closed

cknitt wants to merge 1 commit into
masterfrom
instance-of-module-9239

Conversation

@cknitt

@cknitt cknitt commented Nov 15, 2026

Copy link
Copy Markdown
Member

Fixes #9239.

@module beside @instanceOf was silently ignored, so the class check read a global and threw a ReferenceError at runtime. Now the check imports the class the same way a @module external is imported:

@module("three") @instanceOf("Mesh") type mesh = private object3D
let Three = require("three");
// …
return s instanceof Three.Mesh;
  • A class is now identified by its path and its module. Mesh from "three" and the global Mesh are 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.
  • @module without @instanceOf on a type is an error, and so is @scope on a type, with a hint to write the path instead: @instanceOf("THREE.Mesh"). Both used to be ignored.
  • A relative module path is emitted as written, as it is for externals.

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 seven super_errors fixtures for the new errors and messages.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGyNsDT2sgyUywYMGs

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>
@github-actions

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • @module beside @instanceOf now imports the class from that module, instead of being ignored. @scope on a type is an error: write the path in @instanceOf, such as @instanceOf("THREE.Mesh").

💥 A commit says what it breaks (Breaking:), but no changelog entry is a breaking change.

Semantic Diff

1 commit · Full page

Import the class of an @instanceOf type that has @module

3a07f48 · cknitt + Claude · Nov 15, 2026, 16:31 UTC

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)

@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
  }

js

+let Three = require("three");
+
 function isMesh(s) {
-  return s instanceof Mesh;
+  return s instanceof Three.Mesh;
 }

Representation modified (class identity)

representation

 @instanceOf("Mesh")                           ↦ global Mesh
-@module("three") @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)

@module("three") type mesh

diagnostic · After

@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)

@scope("THREE") @instanceOf("Mesh") type mesh

diagnostic · After

@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)

module Three: {
  @instanceOf("Mesh") type mesh
} = {
  @module("three") @instanceOf("Mesh") type mesh
}

diagnostic · After

Their runtime representations differ:
  Actual declaration: instances of Mesh from "three"
  Expected declaration: instances of Mesh

Fixes #9239

⚠️ 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.

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.
  • How is a relative module path emitted? As written, like the module of a @module external.

@github-actions

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 640,949,204 → 641,045,673 (+0.02%).

Generated projects: 920,891,968 → 920,997,174 (+0.01%).

Benchmark Base Head Change
packages/@rescript/runtime/Dom.res 1,068,580 1,077,647 +0.85%
packages/@rescript/runtime/Primitive_js_extern.res 239,405 240,317 +0.38%
packages/@rescript/runtime/Stdlib_Temporal_Common.res 365,984 367,001 +0.28%
packages/@rescript/belt/src/Belt_Id.resi 259,765 260,462 +0.27%
packages/@rescript/belt/src/Belt_Id.res 289,988 290,635 +0.22%
packages/@rescript/runtime/Stdlib_Intl_DateTimeFormat.res 388,733 389,572 +0.22%
packages/@rescript/runtime/Stdlib_Intl_RelativeTimeFormat.res 179,823 180,168 +0.19%
packages/@rescript/runtime/Stdlib_Intl_NumberFormat.res 403,726 404,491 +0.19%
packages/@rescript/runtime/Stdlib_Intl_ListFormat.res 169,182 169,493 +0.18%
packages/@rescript/runtime/JsxEvent.res 1,088,126 1,089,894 +0.16%
7 more changes
Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Intl_Segmenter.res 153,928 154,167 +0.16%
packages/@rescript/runtime/Stdlib_Intl_Collator.res 178,061 178,337 +0.16%
packages/@rescript/runtime/Stdlib_Intl_Common.res 153,945 154,157 +0.14%
packages/@rescript/runtime/Stdlib_Intl_PluralRules.res 201,187 201,463 +0.14%
packages/@rescript/runtime/RescriptTools_Docgen.resi 273,198 273,550 +0.13%
packages/@rescript/runtime/RescriptTools_Docgen.res 283,381 283,733 +0.12%
packages/@rescript/runtime/Stdlib_Type.resi 135,558 135,700 +0.10%

Instructions

In total: 9,209,713,949 → 9,209,476,755 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 25,843,406 → 25,846,280 (+0.01%).

Benchmark Base Head Change
output/playground/compiler.js 1,889,883 1,892,358 +0.13%

Pull request 3a07f48 compared with base a531dc3 in this run.

@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.07%. Comparing base (a531dc3) to head (3a07f48).

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     
Files with missing lines Coverage Δ
compiler/core/js_exp_make.ml 66.47% <ø> (-0.30%) ⬇️
compiler/core/lam_compile.ml 90.42% <100.00%> (+0.02%) ⬆️
compiler/core/lam_compile_external_call.ml 93.18% <100.00%> (+0.19%) ⬆️
compiler/ml/ast_untagged_variants.ml 86.76% <100.00%> (+2.49%) ⬆️
compiler/ml/predef.ml 100.00% <ø> (ø)
compiler/ml/typedecl.ml 83.58% <100.00%> (+0.04%) ⬆️
compiler/ml/variant_runtime.ml 93.44% <100.00%> (+0.33%) ⬆️
tests/ounit_tests/ounit_string_literal_tests.ml 90.75% <100.00%> (+0.02%) ⬆️
tests/ounit_tests/ounit_untagged_dispatch_tests.ml 94.44% <100.00%> (+0.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 15, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9308

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9308

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9308

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9308

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9308

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9308

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9308

commit: 3a07f48

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

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 @module or @scope on a type without @instanceOf. There they are still ignored silently, which is how #9239 went unnoticed. It's technically breaking, so it could also be a separate follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@module on an @instanceOf type is silently ignored

1 participant