Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler/typing/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
( _,
Expand Down
35 changes: 23 additions & 12 deletions compiler/typing/typemod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions compiler/typing/typemod.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler_tests/src/ImportAttributes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions tests/compiler_tests/src/import_external.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,5 +42,7 @@ export {
f13,
relativeDefault,
f14,
relativeNullable,
f15,
}
/* f11 Not a pure module */
5 changes: 5 additions & 0 deletions tests/compiler_tests/src/import_external.res
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = "getUser"

let f15 = import(relativeNullable)
11 changes: 11 additions & 0 deletions tests/compiler_tests/src/relative_external_fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let reads = 0;

export const counter = {
get value() {
return ++reads;
},
};

export function receiverTag() {
return this === undefined;
}
43 changes: 43 additions & 0 deletions tests/compiler_tests/src/relative_external_test.mjs
Original file line number Diff line number Diff line change
@@ -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 */
20 changes: 20 additions & 0 deletions tests/compiler_tests/src/relative_external_test.res
Original file line number Diff line number Diff line change
@@ -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<int, bool> = "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}`)
})
})
4 changes: 2 additions & 2 deletions tests/compiler_tests/src/relative_path.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ function foo2(prim) {
return FileJs.foo2(prim);
}

let bar = FileJs.foo;

function nested(prim) {
return FileJs.nested(prim);
}
Expand Down Expand Up @@ -71,6 +69,8 @@ Primitive_module.update({
call: call
});

let bar = foo;

export {
foo,
foo2,
Expand Down
14 changes: 8 additions & 6 deletions tests/compiler_tests/src/tagged_template_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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'"));
Expand Down Expand Up @@ -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"]);
});
});
Expand All @@ -126,7 +128,7 @@ export {
jsonTaggedQuery,
query,
extraLength,
length,
length$1 as length,
makeSql,
prefixedSql,
factoryQuery,
Expand Down
6 changes: 4 additions & 2 deletions tests/runtime_tests/docstrings/DocTest.res.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading