Skip to content

Commit a91d0de

Browse files
cknittclaude
andcommitted
Check flat export names only against exported bindings
A flat `A$B$make` export now only clashes with another export. A binding hidden by the interface may have the same JS name: the printer renames the local and exports the alias under the flat name. Example (rescript): // Lib.resi exposes A.B.make and both, but not \"A$B$make" module A = { module B = { let make = () => 1 } } let \"A$B$make" = () => Math.random() > 2. ? 0 : 2 let both = () => A.B.make() + \"A$B$make"() After (js): function A$B$make() { ... } let A$B$make$1 = make; export { A, both, A$B$make$1 as A$B$make } Source: tests/tests/src/flat_make_export_hidden.mjs in this diff; the previous commit rejected this file. Review: Codex finding on #8904, lam_compile_main.ml:186. Area: lambda tests Output: changed tests/tests/src/flat_make_export_*.mjs only, for the new test Formatting: unchanged Formats: none PPX: unchanged Editor: unchanged Tests: +tests/tests/src/flat_make_export_hidden.res ~tests/tests/src/flat_make_export_test.res ~tests/build_tests/super_errors/expected/flat_make_export_collision.res.expected PR: #8904 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013GUg1NbeoJMi6t6V89yBWh Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 10d7001 commit a91d0de

9 files changed

Lines changed: 76 additions & 11 deletions

‎compiler/core/lam_compile_main.ml‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,10 @@ let flat_make_aliases (export_ids : Ident.t list)
162162
Ext_list.fold_left export_ids Map_string.empty (fun modules id ->
163163
Map_string.add id.Ident.name id modules)
164164
in
165+
(* Only export names can clash: the printer renames other bindings. *)
165166
let occupied_names =
166-
Ext_list.fold_left groups Set_string.empty (fun occupied group ->
167-
match group with
168-
| Single (_, id, _) ->
169-
Set_string.add (Ext_ident.convert id.Ident.name) occupied
170-
| Recursive bindings ->
171-
Ext_list.fold_left bindings occupied (fun occupied (id, _) ->
172-
Set_string.add (Ext_ident.convert id.Ident.name) occupied)
173-
| Nop _ -> occupied)
167+
Ext_list.fold_left export_ids Set_string.empty (fun occupied id ->
168+
Set_string.add (Ext_ident.convert id.Ident.name) occupied)
174169
in
175170
let loc = Location.in_file (Location.get_input_name ()) in
176171
List.rev @@ fst
@@ -181,8 +176,8 @@ let flat_make_aliases (export_ids : Ident.t list)
181176
let js_name = Ext_ident.convert name in
182177
if Set_string.mem js_name occupied_names then
183178
Location.raise_errorf ~loc
184-
"Cannot export `%s` as `%s` because that name is already used \
185-
by a top-level binding."
179+
"Cannot export `%s` as `%s` because that name is already \
180+
exported."
186181
(String.concat "." names) name;
187182
match path with
188183
| (_, top) :: fields -> (

‎tests/build_tests/super_errors/expected/flat_make_export_collision.res.expected‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
We've found a bug for you!
33
/.../fixtures/flat_make_export_collision.res
44

5-
Cannot export `A.B.make` as `A$B$make` because that name is already used by a top-level binding.
5+
Cannot export `A.B.make` as `A$B$make` because that name is already exported.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
function make() {
5+
return 1;
6+
}
7+
8+
let B = {
9+
make: make
10+
};
11+
12+
let A = {
13+
B: B
14+
};
15+
16+
function A$B$make() {
17+
if (Math.random() > 2) {
18+
return 0;
19+
} else {
20+
return 2;
21+
}
22+
}
23+
24+
function both() {
25+
return 1 + A$B$make() | 0;
26+
}
27+
28+
let A$B$make$1 = make;
29+
30+
export {
31+
A,
32+
both,
33+
A$B$make$1 as A$B$make,
34+
}
35+
/* No side effect */
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module A = {
2+
module B = {
3+
let make = () => 1
4+
}
5+
}
6+
7+
// Hidden by the interface, so it does not clash with the flat export.
8+
let \"A$B$make" = () => Math.random() > 2. ? 0 : 2
9+
10+
let both = () => A.B.make() + \"A$B$make"()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module A: {
2+
module B: {
3+
let make: unit => int
4+
}
5+
}
6+
7+
let both: unit => int

‎tests/tests/src/flat_make_export_test.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Mocha.describe("Flat_make_export_test", () => {
2727
Test_utils.eq("File \"flat_make_export_test.res\", line 25, characters 7-14", Flat_make_export_use.included, "one");
2828
Test_utils.eq("File \"flat_make_export_test.res\", line 26, characters 7-14", Flat_make_export_use.aliased, "one");
2929
});
30+
Mocha.test("a binding hidden by the interface does not clash", () => {
31+
Test_utils.eq("File \"flat_make_export_test.res\", line 30, characters 7-14", Flat_make_export_use.hidden, 1);
32+
Test_utils.eq("File \"flat_make_export_test.res\", line 31, characters 7-14", Flat_make_export_use.hiddenBoth, 3);
33+
});
3034
});
3135

3236
/* Not a pure module */

‎tests/tests/src/flat_make_export_test.res‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ describe(__MODULE__, () => {
2525
eq(__LOC__, Flat_make_export_use.included, "one")
2626
eq(__LOC__, Flat_make_export_use.aliased, "one")
2727
})
28+
29+
test("a binding hidden by the interface does not clash", () => {
30+
eq(__LOC__, Flat_make_export_use.hidden, 1)
31+
eq(__LOC__, Flat_make_export_use.hiddenBoth, 3)
32+
})
2833
})

‎tests/tests/src/flat_make_export_use.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as Flat_make_export from "./flat_make_export.mjs";
4+
import * as Flat_make_export_hidden from "./flat_make_export_hidden.mjs";
45

56
let top = Flat_make_export.make();
67

@@ -32,6 +33,10 @@ let included = Flat_make_export.Included$make();
3233

3334
let aliased = Flat_make_export.One$make();
3435

36+
let hidden = Flat_make_export_hidden.A$B$make();
37+
38+
let hiddenBoth = Flat_make_export_hidden.both();
39+
3540
export {
3641
top,
3742
one,
@@ -48,5 +53,7 @@ export {
4853
applied,
4954
included,
5055
aliased,
56+
hidden,
57+
hiddenBoth,
5158
}
5259
/* top Not a pure module */

‎tests/tests/src/flat_make_export_use.res‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ let rebound = Flat_make_export.Rebound.make
1717
let applied = Flat_make_export.Applied.make()
1818
let included = Flat_make_export.Included.make()
1919
let aliased = Flat_make_export.Aliased.make()
20+
let hidden = Flat_make_export_hidden.A.B.make()
21+
let hiddenBoth = Flat_make_export_hidden.both()

0 commit comments

Comments
 (0)