Skip to content

Commit d0dba1f

Browse files
cknittclaude
authored andcommitted
Reject bindings of a module that get the same JavaScript name (rescript-lang#8982)
Why: `Ext_ident.convert` maps both `delete` and `$$delete` to `$$delete`, and `\"a-b"` like `a$b`, so two bindings could share an export name or a module object key: the ES export list repeated a name, which Node rejects, and CommonJS output and nested modules lost a binding. The type checker now rejects such a module, at the second binding. This replaces Lam_coercion's "is exported twice" check, which only covered equal source names and had no location. Diagnostic added (JavaScript name clash): Example: let delete = 1 let $$delete = 2 Before (js): let $$delete = 1; let $$delete$1 = 2; export { $$delete, $$delete$1 as $$delete, } After (diagnostic): This value has the same JavaScript name $$delete as the value at A.res:1:5-10. Rename one of them. Diagnostic modified (exception and module of one name): Example: exception M(int) module M = { let x = 3 } Before (diagnostic): M is exported twice After (diagnostic): This module has the same JavaScript name M as the extension constructor at A.res:1:1-16. Rename one of them. Review: Why not rename one of the two exports? Importers derive an export's name from the source name alone (`js_export_name`), so a renamed export would not be found by them; the clash has to be resolved in the source. Review: Which signatures are checked? Each structure's signature and each written signature (module types and `.resi` files), since every module block is built from one of them. The exports of a compilation unit keep `default` as written, so only nested modules reject `default` beside `$$default`. Claude-Session: https://claude.ai/code/session_01Xk6AeCYpCK4SZRtHYKHGoe Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pedro Castro <aspeddro@gmail.com>
1 parent 97aef28 commit d0dba1f

22 files changed

Lines changed: 143 additions & 66 deletions

‎changes/js-name-clash.fix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Report an error when two bindings of a module get the same JavaScript name, such as `delete` and `$$delete`, instead of emitting a duplicate or overwritten export.

‎compiler/core/js_implementation.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ let after_parsing_sig ppf outputprefix ast =
4848
if !((Clflags.current ()).dump_typedtree) then
4949
fprintf ppf "%a@." Printtyped.interface tsg;
5050
let sg = tsg.sig_type in
51+
Typemod.check_js_names ~toplevel:true sg;
5152
ignore (Includemod.signatures initial_env sg sg);
5253
Delayed_checks.force_delayed_checks ();
5354
Warnings.check_fatal ();

‎compiler/core/lam_coercion.ml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ let handle_exports (meta : Lam_stats.t) (lambda_exports : Lambda.t list)
8383
(reverse_input : Lam_group.t list) =
8484
let (original_exports : Ident.t list) = meta.exports in
8585
let (original_export_set : Set_ident.t) = meta.export_idents in
86-
let len = List.length original_exports in
87-
let tbl = Hash_set_string.create len in
8886
let ({export_list; export_set} as result) =
8987
Ext_list.fold_right2 original_exports lambda_exports
9088
{
@@ -94,9 +92,6 @@ let handle_exports (meta : Lam_stats.t) (lambda_exports : Lambda.t list)
9492
groups = [];
9593
} (fun (original_export_id : Ident.t) (lam : Lambda.t) (acc : t) ->
9694
let original_name = original_export_id.name in
97-
if Hash_set_string.mem tbl original_name then
98-
Bs_exception.error (Bs_duplicate_exports original_name);
99-
Hash_set_string.add tbl original_name ();
10095
match lam with
10196
| Lvar id ->
10297
if Ident.name id = original_name then

‎compiler/depends/bs_exception.ml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
type error =
2626
| Cmj_not_found of string
2727
| Js_not_found of string
28-
| Bs_duplicate_exports of string (* gpr_974 *)
2928
| Missing_ml_dependency of string
3029
| Dependency_script_module_dependent_not of string
3130
(** TODO: we need add location handling *)
@@ -47,7 +46,6 @@ let report_error ppf = function
4746
s
4847
| Js_not_found s ->
4948
Format.fprintf ppf "%s not found, needed in script mode " s
50-
| Bs_duplicate_exports str -> Format.fprintf ppf "%s is exported twice" str
5149

5250
let () =
5351
Location.register_error_of_exn (function

‎compiler/depends/bs_exception.mli‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
type error =
2626
| Cmj_not_found of string
2727
| Js_not_found of string
28-
| Bs_duplicate_exports of string (* gpr_974 *)
2928
| Missing_ml_dependency of string
3029
| Dependency_script_module_dependent_not of string
3130

‎compiler/ext/hash_set_string.ml‎

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎compiler/ext/hash_set_string.mli‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎compiler/ml/typemod.ml‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type error =
3434
| With_changes_module_alias of Longident.t * Ident.t * Path.t
3535
| With_cannot_remove_constrained_type
3636
| Repeated_name of string * string * Warnings.loc
37+
| Js_name_clash of string * string * string * Warnings.loc
3738
| Non_generalizable of type_expr
3839
| Non_generalizable_module of module_type
3940
| Interface_not_compiled of string
@@ -636,6 +637,42 @@ let check_sig_item names loc = function
636637
| Sig_typext (id, _, _) -> check_typext names loc (Ident.name id)
637638
| _ -> ()
638639

640+
(* Two runtime components of a module must not get the same JavaScript name:
641+
the module's fields, and the exports of a compilation unit, are named by
642+
[Ext_ident.convert], which maps both [delete] and [$$delete] to [$$delete]
643+
and [\"a-b"] like [a$b]. The exports of a compilation unit keep [default]
644+
as written. Values are checked after [simplify_signature], so a value that
645+
is shadowed by one of the same name is not a field. *)
646+
let check_js_names ~toplevel (sg : Types.signature) =
647+
let seen = Hashtbl.create 17 in
648+
List.iter
649+
(fun (item : Types.signature_item) ->
650+
let component =
651+
match item with
652+
| Sig_value (_, {val_kind = Val_prim _}) | Sig_type _ | Sig_modtype _ ->
653+
None
654+
| Sig_value (id, {val_loc}) -> Some ("value", id, val_loc)
655+
| Sig_typext (id, {ext_loc}, _) ->
656+
Some ("extension constructor", id, ext_loc)
657+
| Sig_module (id, {md_loc}, _) -> Some ("module", id, md_loc)
658+
in
659+
match component with
660+
| None -> ()
661+
| Some (kind, id, loc) -> (
662+
let name = Ident.name id in
663+
let js_name =
664+
if toplevel && name = "default" then name else Ext_ident.convert name
665+
in
666+
match Hashtbl.find_opt seen js_name with
667+
| Some (other_kind, other_loc) ->
668+
raise
669+
(Error
670+
( loc,
671+
Env.empty,
672+
Js_name_clash (kind, other_kind, js_name, other_loc) ))
673+
| None -> Hashtbl.add seen js_name (kind, loc)))
674+
sg
675+
639676
(* Simplify multiple specifications of a value or an extension in a signature.
640677
(Other signature components, e.g. types, modules, etc, are checked for
641678
name uniqueness.) If multiple specifications with the same name,
@@ -702,6 +739,7 @@ and transl_modtype_aux env smty =
702739
env loc smty.pmty_attributes
703740
| Pmty_signature ssg ->
704741
let sg = transl_signature env ssg in
742+
check_js_names ~toplevel:false sg.sig_type;
705743
mkmty (Tmty_signature sg) (Mty_signature sg.sig_type) env loc
706744
smty.pmty_attributes
707745
| Pmty_functor (param, sarg, sres) ->
@@ -1266,6 +1304,7 @@ and type_module_aux ~alias sttn funct_body anchor env smod =
12661304
}
12671305
in
12681306
let sg' = simplify_signature sg in
1307+
check_js_names ~toplevel:false sg';
12691308
if List.length sg' = List.length sg then md
12701309
else
12711310
wrap_constraint
@@ -1766,7 +1805,8 @@ let type_implementation_more ?check_exists sourcefile outputprefix modulename
17661805
(Cmt_format.Implementation str) (Some sourcefile) initial_env;
17671806
(str, coercion, finalenv, dclsig)
17681807
(* identifier is useless might read from serialized cmi files*))
1769-
else
1808+
else (
1809+
check_js_names ~toplevel:true simple_sg;
17701810
let coercion =
17711811
Includemod.compunit initial_env sourcefile sg "(inferred signature)"
17721812
simple_sg
@@ -1785,7 +1825,7 @@ let type_implementation_more ?check_exists sourcefile outputprefix modulename
17851825
(outputprefix ^ ".cmi"));
17861826
Cmt_format.save_cmt (outputprefix ^ ".cmt") modulename
17871827
(Cmt_format.Implementation str) (Some sourcefile) initial_env);
1788-
(str, coercion, finalenv, simple_sg)
1828+
(str, coercion, finalenv, simple_sg))
17891829
with e ->
17901830
Cmt_format.save_cmt (outputprefix ^ ".cmt") modulename
17911831
(Cmt_format.Partial_implementation
@@ -1855,6 +1895,11 @@ let report_error ppf = function
18551895
"@[Multiple definition of the %s name %s @ at @{<loc>%a@}@ @ Names must \
18561896
be unique in a given structure or signature.@]"
18571897
kind name Location.print_loc repeated_loc
1898+
| Js_name_clash (kind, other_kind, js_name, other_loc) ->
1899+
fprintf ppf
1900+
"@[This %s has the same JavaScript name %s@ as the %s at @{<loc>%a@}.@ \
1901+
Rename one of them.@]"
1902+
kind js_name other_kind Location.print_loc other_loc
18581903
| Non_generalizable typ ->
18591904
(* modified *)
18601905
fprintf ppf "@[<v>";

‎compiler/ml/typemod.mli‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ val type_implementation_more :
3535
Typedtree.structure * Typedtree.module_coercion * Env.t * Types.signature
3636

3737
val transl_signature : Env.t -> Parsetree.signature -> Typedtree.signature
38+
val check_js_names : toplevel:bool -> Types.signature -> unit
3839
val type_open_ :
3940
?used_slot:bool ref ->
4041
?toplevel:bool ->
@@ -60,6 +61,7 @@ type error =
6061
| With_changes_module_alias of Longident.t * Ident.t * Path.t
6162
| With_cannot_remove_constrained_type
6263
| Repeated_name of string * string * Warnings.loc
64+
| Js_name_clash of string * string * string * Warnings.loc
6365
| Non_generalizable of type_expr
6466
| Non_generalizable_module of module_type
6567
| Interface_not_compiled of string

‎tests/build_tests/gpr_978/input.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ const { execBuild, execClean } = setup(import.meta.dirname);
88

99
const output = await execBuild();
1010
const stderr = stripVTControlCharacters(output.stderr);
11-
assert.match(stderr, /M is exported twice/);
11+
assert.match(stderr, /This module has the same JavaScript name M/);
1212
await execClean();

0 commit comments

Comments
 (0)