Semantic Diff

#9523 Import an @instanceOf class of a relative module from the declaring file

2 commits

fd365b88a475b2410ac1507ef947dab8f0f4cacb..pr-9523

239f06695 · JonoPrest · Dec 04, 2026, 08:08 UTC

Test an @instanceOf class of a relative module from other directories

Why

A type can name its class with a module path relative to its own file, @module("./shapes.js") @instanceOf("Circle"). An untagged variant switch in a module of another directory writes that path into its own import, where it names another file or none.

This commit only adds the project test, which fails on its first module of another directory: src/app/App.mjs imports ./shapes.js, not ../shapes/shapes.js. The next commit fixes the import.

addedJS class check in another directory

// src/shapes/Shapes.res@module("./shapes.js") @instanceOf("Circle")type circle // src/app/App.res@unboxedtype value =  | Circle(Shapes.circle)  | Plain({"plain": bool}) let kind = value =>  switch value {  | Circle(_) => "circle"  | Plain(_) => "plain"  }
js
Before src/app/App.mjs, as the compiler writes it today
import * as ShapesJs from "./shapes.js"; function kind(value) {  if (value instanceof ShapesJs.Circle) {    return "circle";  } else {    return "plain";  }}
text
After node src/app/App.mjs
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '…/src/app/shapes.js' imported from …/src/app/App.mjs
text
After node input.js, the test
AssertionError [ERR_ASSERTION]: imports of src/app/App.mjs  actual: [ './shapes.js' ],  expected: [ '../shapes/shapes.js' ],
22b65ddd2 · JonoPrest · Dec 04, 2026, 08:14 UTC

Import an @instanceOf class of a relative module from the declaring file

Why

@module("./shapes.js") @instanceOf("Circle") type circle names a file beside the module that declares the type. The module that compiles a class check adds the import, and it wrote the path as it is, so a check in another directory or package imported a file beside itself: another file, or none.

A class with a relative module now records the compilation unit that declares it. A check in another unit imports the path from the JavaScript file of that unit, which is found as an import of the unit is, for each output format. Externals avoid the same problem by calling through the declaring module's export; a type has no export to go through, so the path is rewritten instead.

modifiedJS class check in another directory

// src/shapes/Shapes.res@module("./shapes.js") @instanceOf("Circle")type circle // src/app/App.res@unboxedtype value =  | Circle(Shapes.circle)  | Plain({"plain": bool}) let kind = value =>  switch value {  | Circle(_) => "circle"  | Plain(_) => "plain"  }
js
Before src/app/App.mjs
After src/app/App.mjs
import * as ShapesJs from "./shapes.js";import * as ShapesJs from "../shapes/shapes.js"; function kind(value) {function kind(value) { if (value instanceof ShapesJs.Circle) { if (value instanceof ShapesJs.Circle) { return "circle"; return "circle"; } else { } else { return "plain"; return "plain"; } }}}

modifiedJS class check in another package

// node_modules/shapes-dep/src/hex/Dep.res, namespace ShapesDep@module("./dep_shapes.js") @instanceOf("Hexagon")type hexagon // src/app/Classes.res@unboxedtype value =  | Hexagon(ShapesDep.Dep.hexagon)  | Plain({"plain": bool})
js
Before src/app/Classes.mjs, and lib/js/src/app/Classes.cjs
After src/app/Classes.mjs, and lib/js/src/app/Classes.cjs
import * as Dep_shapesJs from "./dep_shapes.js";import * as Dep_shapesJs from "shapes-dep/src/hex/dep_shapes.js"; let Dep_shapesJs = require("./dep_shapes.js");let Dep_shapesJs = require("shapes-dep/lib/js/src/hex/dep_shapes.js");

modifiedRepresentation class of an @instanceOf type

representation
Before
After in Shapes.res
@module("./shapes.js") @instanceOf("Circle") ↦ {module_ = Some "./shapes.js"; path = "Circle"}@module("three") @instanceOf("Mesh") ↦ {module_ = Some "three"; path = "Mesh"}@module("./shapes.js") @instanceOf("Circle") ↦ {module_ = Some "./shapes.js"; relative_to = Some "Shapes"; path = "Circle"}@module("three") @instanceOf("Mesh") ↦ {module_ = Some "three"; relative_to = None; path = "Mesh"}

modifiedDiagnostic the same relative path in two files

// Class_types.resmodule type S = {  @module("./shapes.js") @instanceOf("Circle")  type t} // Class_use.resmodule M: Class_types.S = {  @module("./shapes.js") @instanceOf("Circle")  type t}
diagnostic
Before
After
none Signature mismatch:…The declarations read the same, but a type they mention differs: Actual declaration: instances of Circle from "./shapes.js" relative to Class_use Expected declaration: instances of Circle from "./shapes.js" relative to Class_types

Review

  • Is a check in the declaring file itself changed? No. There the unit is the one being compiled, the path is written as it is, and the import is shared with the file's externals of the same path, as before; untagged_module_class_test.mjs is unchanged.
  • What does an annotation mean that repeats the abbreviated type's, in another file? It names the abbreviated type's class, with the path from that type's file, so an interface that restates @module("./shapes.js") @instanceOf("Circle") type circle = Shapes.circle still compiles in any directory.
  • Where does the import go when the output is not beside the sources? Beside the output of the declaring module, in each format: the path is read from the generated JavaScript file, as the path of an external is, so the file has to be there, as for an external.
  • Are two declarations of one file's class one class? No. A relative module is identified with the unit that declares the type, since the type checker does not know which directory a unit is in: the same "./shapes.js" declared in two files is two classes, also in one directory. A signature with such a type is not matched from another file, as the diagnostic above shows, and a variant can have both types as cases, each with its own import of the file. The feature is not released yet (changes/instance-of-module.fix.md), so no released program changes meaning.
  • What if the declaring unit cannot be loaded? Its .cmj gives the path, and a package that does not depend on the unit's package has none. A check there is an error that asks for the dependency (Cannot import the class Triangle from "./hidden.js" relative to Hidden …), and a function with such a check is not inlined into that package, like a body that names a module it cannot load. Without a package (plain bsc), where the unit's .cmj or JavaScript file may not exist, the path stays as written, as before.
  • Can every package import the file? Only if the declaring package lets it: a package that limits its exports has to export the file, as it has to export its compiled modules.
  • Does this touch the externals that #9516 changes? No. It reads External_ffi_types.is_package_relative_path and leaves primitive.ml, translcore.ml and typemod.ml alone.