diff --git a/changes/inline-unloadable-module.fix.md b/changes/inline-unloadable-module.fix.md new file mode 100644 index 0000000000..648854d901 --- /dev/null +++ b/changes/inline-unloadable-module.fix.md @@ -0,0 +1 @@ +- When inlining functions from other modules (until now only with `-bs-cross-module-opt`), call a function from another package instead of inlining it when its body names a module of a package the caller doesn't depend on. The build used to fail with "not found, it means either the module does not exist or it is a namespace". diff --git a/compiler/backend/lam_compile_env.ml b/compiler/backend/lam_compile_env.ml index b791ebdc64..667afcefb5 100644 --- a/compiler/backend/lam_compile_env.ml +++ b/compiler/backend/lam_compile_env.ml @@ -48,6 +48,13 @@ let cached_tbl_key = let cached_tbl () = Domain.DLS.get cached_tbl_key +(* Whether this compile found a module's .cmj on the load path, by module + name, so each one is looked up once. *) +let loadable_tbl_key = + Request_local.new_key (fun () : (string, bool) Hashtbl.t -> Hashtbl.create 7) + +let loadable_tbl () = Domain.DLS.get loadable_tbl_key + let ( +> ) id value = Lam_module_ident.Hash.add (cached_tbl ()) id value (* For each compilation we need reset to make it re-entrant *) @@ -56,7 +63,8 @@ let reset () = (* This is needed in the playground since one no_export can make it true In the payground, it seems we need reset more states *) - Lam_module_ident.Hash.clear (cached_tbl ()) + Lam_module_ident.Hash.clear (cached_tbl ()); + Hashtbl.clear (loadable_tbl ()) (** We should not provide "#moduleid" as output since when we print it in the end, it will @@ -101,9 +109,38 @@ let cmj_table_of_module_id ~dynamic_import (module_id : Ident.t) = let register_ml_module ~dynamic_import (module_id : Ident.t) : unit = ignore (cmj_table_of_module_id ~dynamic_import module_id) +(* Whether this compile can load a module that a stored body names: it is + cached already, or its .cmj is on the load path. Looking it up must not + register it, since registered modules with side effects get imported. *) +let can_load (id : Ident.t) = + Lam_module_ident.Hash.mem (cached_tbl ()) + (Lam_module_ident.of_ml ~dynamic_import:false id) + || + match Hashtbl.find_opt (loadable_tbl ()) id.name with + | Some loadable -> loadable + | None -> + let loadable = + Config_util.find_opt (id.name ^ Literals.suffix_cmj) <> None + in + Hashtbl.replace (loadable_tbl ()) id.name loadable; + loadable + +let rec names_unloadable_module (lam : Lambda.t) = + (match lam with + | Lglobal_module id -> not (can_load id) + | _ -> false) + || Lambda_traverse.shallow_exists names_unloadable_module lam + +(* A stored body can name modules of packages this one does not depend on, + which are not on its load path. Inlining it would fail, so the caller + calls the value instead, as without a stored body. *) let query_external_id_info (module_id : Ident.t) (name : string) : ident_info = let cmj_table = cmj_table_of_module_id ~dynamic_import:false module_id in - Js_cmj_format.query_by_name cmj_table name + let info = Js_cmj_format.query_by_name cmj_table name in + match info.persistent_closed_lambda with + | Some lam when names_unloadable_module lam -> + {info with persistent_closed_lambda = None} + | Some _ | None -> info let get_package_info_from_cmj (id : Lam_module_ident.t) : Js_packages_info.t * Ext_js_file_kind.case = diff --git a/tests/build_tests/projects/cross_module_unloadable_module/.gitignore b/tests/build_tests/projects/cross_module_unloadable_module/.gitignore new file mode 100644 index 0000000000..8108338454 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/.gitignore @@ -0,0 +1,3 @@ +lib +**/*.mjs +!node_modules diff --git a/tests/build_tests/projects/cross_module_unloadable_module/app/package.json b/tests/build_tests/projects/cross_module_unloadable_module/app/package.json new file mode 100644 index 0000000000..3f61577746 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/app/package.json @@ -0,0 +1,4 @@ +{ + "name": "app", + "version": "0.0.1" +} diff --git a/tests/build_tests/projects/cross_module_unloadable_module/app/rescript.json b/tests/build_tests/projects/cross_module_unloadable_module/app/rescript.json new file mode 100644 index 0000000000..0559a407d9 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/app/rescript.json @@ -0,0 +1,8 @@ +{ + "name": "app", + "sources": { "dir": "src" }, + "dependencies": ["p"], + "package-specs": { "module": "esmodule", "in-source": true }, + "suffix": ".mjs", + "compiler-flags": ["-bs-cross-module-opt"] +} diff --git a/tests/build_tests/projects/cross_module_unloadable_module/app/src/App.res b/tests/build_tests/projects/cross_module_unloadable_module/app/src/App.res new file mode 100644 index 0000000000..8008451490 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/app/src/App.res @@ -0,0 +1,4 @@ +// PMod.f names QMod, from a package app does not depend on: called. +let viaQ = PMod.f(2) +// PMod.k names no other module: inlined. +let local = PMod.k(2) diff --git a/tests/build_tests/projects/cross_module_unloadable_module/input.js b/tests/build_tests/projects/cross_module_unloadable_module/input.js new file mode 100644 index 0000000000..293de67103 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/input.js @@ -0,0 +1,28 @@ +// @ts-check + +// With -bs-cross-module-opt, a stored body that names a module of a package +// the caller does not depend on cannot be inlined there: that module is not +// on the caller's load path. The caller calls the value instead. + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuildOrThrow, execClean } = setup( + path.join(import.meta.dirname, "app"), +); + +await execClean(); +await execBuildOrThrow(); + +const app = await fs.readFile( + path.join(import.meta.dirname, "app", "src", "App.mjs"), + "utf8", +); + +assert.match(app, /PMod\.f\(2\)/, "a body naming QMod is called"); +assert.doesNotMatch(app, /QMod/, "App does not refer to QMod"); +assert.match(app, /let local = 4;/, "a body naming no module is inlined"); + +await execClean(); diff --git a/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/package.json b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/package.json new file mode 100644 index 0000000000..b7b558cba0 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/package.json @@ -0,0 +1,4 @@ +{ + "name": "p", + "version": "0.0.1" +} diff --git a/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/rescript.json b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/rescript.json new file mode 100644 index 0000000000..01b930e878 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/rescript.json @@ -0,0 +1,8 @@ +{ + "name": "p", + "sources": { "dir": "src" }, + "dependencies": ["q"], + "package-specs": { "module": "esmodule", "in-source": true }, + "suffix": ".mjs", + "compiler-flags": ["-bs-cross-module-opt"] +} diff --git a/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/src/PMod.res b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/src/PMod.res new file mode 100644 index 0000000000..0c0a1b27f0 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/p/src/PMod.res @@ -0,0 +1,2 @@ +@inline let f = x => QMod.g(x) + 1 +@inline let k = x => x * 2 diff --git a/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/package.json b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/package.json new file mode 100644 index 0000000000..b377a6a794 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/package.json @@ -0,0 +1,4 @@ +{ + "name": "q", + "version": "0.0.1" +} diff --git a/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/rescript.json b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/rescript.json new file mode 100644 index 0000000000..198c78354f --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/rescript.json @@ -0,0 +1,7 @@ +{ + "name": "q", + "sources": { "dir": "src" }, + "package-specs": { "module": "esmodule", "in-source": true }, + "suffix": ".mjs", + "compiler-flags": ["-bs-cross-module-opt"] +} diff --git a/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/src/QMod.res b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/src/QMod.res new file mode 100644 index 0000000000..93e756cac1 --- /dev/null +++ b/tests/build_tests/projects/cross_module_unloadable_module/node_modules/q/src/QMod.res @@ -0,0 +1 @@ +let g = x => x * 3