diff --git a/compiler/typing/translcore.ml b/compiler/typing/translcore.ml index b50eb03221..c98afd5c8e 100644 --- a/compiler/typing/translcore.ml +++ b/compiler/typing/translcore.ml @@ -663,6 +663,20 @@ let transl_adapted_external_import loc env loc let transl_dynamic_import loc (arg : Typedtree.expression) : Lambda.t = + (* Reuse both external import paths without exposing the primitive to + translation of ordinary references to the binding. *) + let arg = + match arg.exp_desc with + | Texp_ident ((Pident id as path), lid, vd) -> ( + match Typemod.relative_external_primitive id with + | Some prim -> + { + arg with + exp_desc = Texp_ident (path, lid, {vd with val_kind = Val_prim prim}); + } + | None -> arg) + | _ -> arg + in match arg.exp_desc with | Texp_ident ( _, diff --git a/compiler/typing/typemod.ml b/compiler/typing/typemod.ml index 68b15ece32..4351337782 100644 --- a/compiler/typing/typemod.ml +++ b/compiler/typing/typemod.ml @@ -1254,6 +1254,20 @@ let wrap_constraint env arg mty explicit = (* Type a module value expression *) +(* Relative externals are bound as regular values (see [Pstr_primitive] in + [type_structure]). Only [import(x)] needs the original primitive, keyed by + the local binding, never by a qualified reference through a signature. *) +let relative_external_primitives = + Request_local.new_key (fun () : Primitive.description Ident.tbl ref -> + ref Ident.empty) + +let relative_external_primitive id = + match + Ident.find_same id !(Request_local.get relative_external_primitives) + with + | primitive -> Some primitive + | exception Not_found -> None + let rec type_module ?(alias = false) sttn funct_body anchor env smod = Builtin_attributes.warning_scope smod.pmod_attributes (fun () -> type_module_aux ~alias sttn funct_body anchor env smod) @@ -1560,18 +1574,15 @@ and type_structure funct_body anchor env sstr = incl_loc = loc; } in - (* Only the exported signature hides the primitive. The rest of - this file, where the relative path is valid, still sees the - external, so [import(x)] can import its module directly. *) - let local_sg = - List.map - (function - | Sig_value (id, vd) -> - Sig_value (id, {vd with val_kind = desc.val_val.val_kind}) - | item -> item) - sg - in - (Tstr_include incl, sg, Env.add_signature local_sg env) + (* Use the captured value in ordinary expressions. Translation + can still recover the relative external for [import(x)]. *) + let table = Request_local.get relative_external_primitives in + List.iter + (function + | Sig_value (id, _) -> table := Ident.add id prim !table + | _ -> ()) + sg; + (Tstr_include incl, sg, Env.add_signature sg env) | _ -> (Tstr_primitive desc, [item], newenv)) | Pstr_type (rec_flag, sdecls) -> List.iter (fun decl -> check_name check_type names decl.ptype_name) sdecls; diff --git a/compiler/typing/typemod.mli b/compiler/typing/typemod.mli index 97603fdfd0..5a150e9353 100644 --- a/compiler/typing/typemod.mli +++ b/compiler/typing/typemod.mli @@ -21,6 +21,10 @@ open Format val type_structure : Env.t -> Parsetree.structure -> Typedtree.structure * Types.signature * Env.t +val relative_external_primitive : Ident.t -> Primitive.description option +(** The primitive of a same-file relative external, which is otherwise bound + as a regular value. Used to translate [import(x)]. *) + val rescript_hide : Typedtree.structure_item_desc -> bool val type_implementation_more : diff --git a/tests/compiler_tests/src/ImportAttributes.mjs b/tests/compiler_tests/src/ImportAttributes.mjs index 3b22b6f65f..3489b09547 100644 --- a/tests/compiler_tests/src/ImportAttributes.mjs +++ b/tests/compiler_tests/src/ImportAttributes.mjs @@ -5,11 +5,11 @@ import MyJsonJson from "./myJson.json" with {"type": "json", "some-identifier": let myJson = MyJsonJson; -console.log(MyJsonJson); +console.log(myJson); let buttonCss = MyCssCss.button; -console.log(MyCssCss.button); +console.log(buttonCss); export { myJson, diff --git a/tests/compiler_tests/src/import_external.mjs b/tests/compiler_tests/src/import_external.mjs index 902be116fb..3910533943 100644 --- a/tests/compiler_tests/src/import_external.mjs +++ b/tests/compiler_tests/src/import_external.mjs @@ -26,6 +26,12 @@ function relativeDefault(prim) { let f14 = import("./m.js").then(m => m.default); +function relativeNullable(prim) { + return Primitive_option.fromNullable(MJs.getUser(prim)); +} + +let f15 = import("./m.js").then(m => (prim0 => Primitive_option.fromNullable(m.getUser(prim0)))); + export { f8, f9, @@ -36,5 +42,7 @@ export { f13, relativeDefault, f14, + relativeNullable, + f15, } /* f11 Not a pure module */ diff --git a/tests/compiler_tests/src/import_external.res b/tests/compiler_tests/src/import_external.res index b8a6db423b..cfebb80cdb 100644 --- a/tests/compiler_tests/src/import_external.res +++ b/tests/compiler_tests/src/import_external.res @@ -27,3 +27,8 @@ let f13 = import(relativeScoped) @module("./m.js") external relativeDefault: float => float = "default" let f14 = import(relativeDefault) + +@module("./m.js") @return(nullable) +external relativeNullable: string => option = "getUser" + +let f15 = import(relativeNullable) diff --git a/tests/compiler_tests/src/relative_external_fixture.js b/tests/compiler_tests/src/relative_external_fixture.js new file mode 100644 index 0000000000..3518c9878a --- /dev/null +++ b/tests/compiler_tests/src/relative_external_fixture.js @@ -0,0 +1,11 @@ +let reads = 0; + +export const counter = { + get value() { + return ++reads; + }, +}; + +export function receiverTag() { + return this === undefined; +} diff --git a/tests/compiler_tests/src/relative_external_test.mjs b/tests/compiler_tests/src/relative_external_test.mjs new file mode 100644 index 0000000000..3ed0e23d4c --- /dev/null +++ b/tests/compiler_tests/src/relative_external_test.mjs @@ -0,0 +1,43 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; +import * as Relative_external_fixtureJs from "./relative_external_fixture.js"; + +let value = Relative_external_fixtureJs.counter.value; + +let receiverTag = Relative_external_fixtureJs.receiverTag; + +function read() { + return [ + value, + value + ]; +} + +Mocha.describe("same-file relative externals", () => { + Mocha.test("ordinary reads keep the captured value", () => { + Test_utils.eq("File \"relative_external_test.res\", line 14, characters 7-14", [ + value, + value + ], [ + 1, + 1 + ]); + Test_utils.eq("File \"relative_external_test.res\", line 15, characters 7-14", [ + value, + value + ], [ + 1, + 1 + ]); + }); + Mocha.test("a relative external tag does not gain a receiver", () => Test_utils.ok("File \"relative_external_test.res\", line 18, characters 7-14", receiverTag`value: ${1}`)); +}); + +export { + value, + receiverTag, + read, +} +/* value Not a pure module */ diff --git a/tests/compiler_tests/src/relative_external_test.res b/tests/compiler_tests/src/relative_external_test.res new file mode 100644 index 0000000000..28b4f25952 --- /dev/null +++ b/tests/compiler_tests/src/relative_external_test.res @@ -0,0 +1,20 @@ +open Mocha +open Test_utils + +@module("./relative_external_fixture.js") @scope("counter") +external value: int = "value" + +@module("./relative_external_fixture.js") +external receiverTag: taggedTemplate = "receiverTag" + +let read = () => (value, value) + +describe("same-file relative externals", () => { + test("ordinary reads keep the captured value", () => { + eq(__LOC__, read(), (1, 1)) + eq(__LOC__, read(), (1, 1)) + }) + test("a relative external tag does not gain a receiver", () => { + ok(__LOC__, receiverTag`value: ${1}`) + }) +}) diff --git a/tests/compiler_tests/src/relative_path.mjs b/tests/compiler_tests/src/relative_path.mjs index 6b15b23c75..f7a211aa39 100644 --- a/tests/compiler_tests/src/relative_path.mjs +++ b/tests/compiler_tests/src/relative_path.mjs @@ -9,8 +9,6 @@ function foo2(prim) { return FileJs.foo2(prim); } -let bar = FileJs.foo; - function nested(prim) { return FileJs.nested(prim); } @@ -71,6 +69,8 @@ Primitive_module.update({ call: call }); +let bar = foo; + export { foo, foo2, diff --git a/tests/compiler_tests/src/tagged_template_test.mjs b/tests/compiler_tests/src/tagged_template_test.mjs index 72553aa446..e3ee5ef4e2 100644 --- a/tests/compiler_tests/src/tagged_template_test.mjs +++ b/tests/compiler_tests/src/tagged_template_test.mjs @@ -21,12 +21,14 @@ let queryWithModule = sql`SELECT * FROM ${table} WHERE id = ${id}`; let json = Tagged_template_libJs.sql; -let jsonTaggedQuery = Tagged_template_libJs.sql`SELECT * FROM ${table}`; +let jsonTaggedQuery = json`SELECT * FROM ${table}`; let query = sql` " SELECT * FROM ${table} WHERE id = ${id}`; -let length = Tagged_template_libJs.length`hello ${10} what's the total length? Is it ${3}?`; +let length = Tagged_template_libJs.length; + +let length$1 = length`hello ${10} what's the total length? Is it ${3}?`; function makeSql(prim) { return Tagged_template_libJs.makeSql(prim); @@ -65,14 +67,14 @@ let crossModuleQuery = Tagged_template_binding.sql`SELECT * FROM ${table}`; let rawTag = Tagged_template_libJs.rawTag; -let rawResult = Tagged_template_libJs.rawTag`a ${1} b ${2} c`; +let rawResult = rawTag`a ${1} b ${2} c`; Mocha.describe("tagged templates", () => { Mocha.test("with externals, it should return a string with the correct interpolations", () => Test_utils.eq("File \"tagged_template_test.res\", line 100, characters 6-13", query, ` " SELECT * FROM 'users' WHERE id = '5'`)); Mocha.test("with module scoped externals, it should also return a string with the correct interpolations", () => Test_utils.eq("File \"tagged_template_test.res\", line 109, characters 13-20", queryWithModule, "SELECT * FROM 'users' WHERE id = '5'")); Mocha.test("a tag named json remains an ordinary tagged template", () => Test_utils.eq("File \"tagged_template_test.res\", line 113, characters 7-14", jsonTaggedQuery, "SELECT * FROM 'users'")); - Mocha.test("with externals, it should return the result of the function", () => Test_utils.eq("File \"tagged_template_test.res\", line 116, characters 79-86", length, 52)); + Mocha.test("with externals, it should return the result of the function", () => Test_utils.eq("File \"tagged_template_test.res\", line 116, characters 79-86", length$1, 52)); Mocha.test("with a runtime-constructed tag (factory), it should emit tagged-template syntax", () => Test_utils.eq("File \"tagged_template_test.res\", line 119, characters 7-14", factoryQuery, "PREFIX SELECT * FROM 'users'")); Mocha.test("with a tag from a bare package import, it should emit tagged-template syntax", () => Test_utils.eq("File \"tagged_template_test.res\", line 125, characters 7-14", bareImportQuery, "PG: SELECT * FROM 'users'")); Mocha.test("with a tag passed as a function argument, it should emit tagged-template syntax", () => Test_utils.eq("File \"tagged_template_test.res\", line 129, characters 7-14", paramQuery, "SELECT id = '5'")); @@ -110,7 +112,7 @@ Mocha.describe("tagged templates", () => { ]); }); Mocha.test("invalid escapes remain valid in tagged-template segments", () => { - let result = Tagged_template_libJs.rawTag`\unicode`; + let result = rawTag`\unicode`; Test_utils.eq("File \"tagged_template_test.res\", line 165, characters 7-14", result.raw, ["\\unicode"]); }); }); @@ -126,7 +128,7 @@ export { jsonTaggedQuery, query, extraLength, - length, + length$1 as length, makeSql, prefixedSql, factoryQuery, diff --git a/tests/runtime_tests/docstrings/DocTest.res.js b/tests/runtime_tests/docstrings/DocTest.res.js index ca8f4b9c8e..46d65f046b 100644 --- a/tests/runtime_tests/docstrings/DocTest.res.js +++ b/tests/runtime_tests/docstrings/DocTest.res.js @@ -15,6 +15,8 @@ import * as Promises from "node:fs/promises"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.mjs"; import * as BinsJs from "../../../cli/common/bins.js"; +let rescript_tools_exe = BinsJs.rescript_tools_exe; + let nodeVersion = Stdlib_Option.getOrThrow(Stdlib_Int.fromString(Stdlib_Option.getOrThrow(process.version.replace("v", "").split(".")[0], "Failed to find major version of Node")), "Failed to convert node version to Int"); let ignoreRuntimeTests = [ @@ -45,7 +47,7 @@ function getOutput(buffer) { } async function extractDocFromFile(file) { - let match = await SpawnAsync.run(BinsJs.rescript_tools_exe, [ + let match = await SpawnAsync.run(rescript_tools_exe, [ "extract-codeblocks", file, "--transform-assert-equal" @@ -167,4 +169,4 @@ ${output.join("\n")}`; await main(); -/* Not a pure module */ +/* rescript_tools_exe Not a pure module */