Repository navigation
Conversation
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.
JS added (class check in another directory):
Example:
// src/shapes/Shapes.res
@module("./shapes.js") @instanceof("Circle")
type circle
// src/app/App.res
@unboxed
type value =
| Circle(Shapes.circle)
| Plain({"plain": bool})
let kind = value =>
switch value {
| Circle(_) => "circle"
| Plain(_) => "plain"
}
Before (js): 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";
}
}
After (text): node src/app/App.mjs
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '…/src/app/shapes.js' imported from …/src/app/App.mjs
After (text): node input.js, the test
AssertionError [ERR_ASSERTION]: imports of src/app/App.mjs
actual: [ './shapes.js' ],
expected: [ '../shapes/shapes.js' ],
Signed-off-by: Jono Prest <jjprest@gmail.com>
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.
JS modified (class check in another directory):
Example:
// src/shapes/Shapes.res
@module("./shapes.js") @instanceof("Circle")
type circle
// src/app/App.res
@unboxed
type value =
| Circle(Shapes.circle)
| Plain({"plain": bool})
let kind = value =>
switch value {
| Circle(_) => "circle"
| Plain(_) => "plain"
}
Before (js): src/app/App.mjs
import * as ShapesJs from "./shapes.js";
function kind(value) {
if (value instanceof ShapesJs.Circle) {
return "circle";
} else {
return "plain";
}
}
After (js): src/app/App.mjs
import * as ShapesJs from "../shapes/shapes.js";
function kind(value) {
if (value instanceof ShapesJs.Circle) {
return "circle";
} else {
return "plain";
}
}
JS modified (class check in another package):
Example:
// node_modules/shapes-dep/src/hex/Dep.res, namespace ShapesDep
@module("./dep_shapes.js") @instanceof("Hexagon")
type hexagon
// src/app/Classes.res
@unboxed
type value =
| Hexagon(ShapesDep.Dep.hexagon)
| Plain({"plain": bool})
Before (js): src/app/Classes.mjs, and lib/js/src/app/Classes.cjs
import * as Dep_shapesJs from "./dep_shapes.js";
let Dep_shapesJs = require("./dep_shapes.js");
After (js): src/app/Classes.mjs, and lib/js/src/app/Classes.cjs
import * as Dep_shapesJs from "shapes-dep/src/hex/dep_shapes.js";
let Dep_shapesJs = require("shapes-dep/lib/js/src/hex/dep_shapes.js");
Representation modified (class of an @instanceof type):
Before (representation):
@module("./shapes.js") @instanceof("Circle") ↦ {module_ = Some "./shapes.js"; path = "Circle"}
@module("three") @instanceof("Mesh") ↦ {module_ = Some "three"; path = "Mesh"}
After (representation): in Shapes.res
@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"}
Diagnostic modified (the same relative path in two files):
Example:
// Class_types.res
module type S = {
@module("./shapes.js") @instanceof("Circle")
type t
}
// Class_use.res
module M: Class_types.S = {
@module("./shapes.js") @instanceof("Circle")
type t
}
Before (diagnostic): none
After (diagnostic):
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.
Review: 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.
Review: 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.
Review: 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.
Review: 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.
Review: 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.
Review: 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.
Signed-off-by: Jono Prest <jjprest@gmail.com>
Changelog🐛 Bug fix
Semantic Diff2 commits · Full page Test an
|
|
@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. |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. 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". |
✅ Compiler benchmarksAllocated wordsIn total: 653,429,548 → 653,437,035 (±0.00%). Generated projects: 627,286,493 → 627,286,606 (±0.00%). None of the 265 benchmarks changed by 0.1% or more. InstructionsIn total: 8,967,192,662 → 8,965,894,329 (-0.01%).
5 more changes
Output sizeIn total: 27,460,200 → 27,461,721 (+0.01%). None of the 13 benchmarks changed by 0.1% or more. Pull request 22b65dd compared with base e489471 in this run. |
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-9523 |
|
Thanks, this fixes #9520. We built the PR's compiler and All the main points come from one design choice: a class is identified by the file that declares it plus the path string. 1. One JS class declared in two files gives a match that silently takes the wrong arm. @module("./shapes.js") @instanceOf("Circle")
type circle@unboxed
type v = A(Shapes.circle) | B(Shapes2.circle)
let kind = v => switch v { | A(_) => "A" | B(_) => "B" }This compiles, with two imports of 2. Another file can't hide such a type behind an abstract type. Say module type S = { @module("../moved/shapes.js") @instanceOf("Circle") type t }
module M: S = { type t = Shapes.circle }This is rejected. The two declarations are 3. Messages show internal module names. In a namespaced package the errors say Simplifications
Small points
This review was done by Claude (Opus 5.5 and Fable 5.2, independently). |
Fixes #9520
@module("./shapes.js") @instanceOf("Circle") type circlenames 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 match in another directory or package imported a file beside itself.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 the same way as an import of the unit itself, per output format:
../shapes/shapes.jsfromsrc/app,shapes-dep/src/hex/dep_shapes.jsfrom another package. Checks in the declaring file, package specifiers and globals are written as before.Things worth a look:
-bs-package-output). So the same"./shapes.js"annotation in two files is two classes, also in one directory: a signature with such a type is not matched from another file, and a variant can have both as cases. An annotation that repeats the abbreviated type's still names that type's class. The@moduleon@instanceOffeature is unreleased, so no released program changes.UntaggedVariants.mdspells this out..cmj. A package that does not depend on that unit's package gets an error asking for the dependency, and a function with such a check is not inlined into it. Without a package (plainbsc) the path stays as written when the.cmjor JS file is missing.External_ffi_types.is_package_relative_pathand leavesprimitive.ml,translcore.mlandtypemod.mlalone. Both are about relative@modulepaths across modules, but that one is about externals andimport(), this one about@instanceOftypes.Tests: the first commit adds the project test alone and fails on master (
src/app/App.mjsimports./shapes.js); the second fixes it and extends it.tests/project_tests/instance_of_relative_modulebuilds with the build system and runs the ES module output for consumers in the same, a sibling, a deeper and a shallower directory, named/default/scoped exports, the same relative string in two directories, a namespaced dependency package, a class of a transitive dependency, an inlined check, interface files, abbreviations, restated annotations, module aliases and includes. The CommonJS out-of-source output and the unloadable-dependency error are checked as text only. The plainbscfallback I checked by hand, not in a test.make testandmake checkformatpass locally.The commits are not signed off.
Checklist
changes/README.md), or no release notes are needed and a maintainer should applychangelog:skip.CONTRIBUTING.md).git commit -s).Replaces #9521, which was opened from my fork.