Skip to content

Commit ad092f5

Browse files
authored
Merge pull request #3876 from BuckleScript/fix_3852
Fix #3852
2 parents 2cc36bf + 8d29282 commit ad092f5

22 files changed

+183
-52
lines changed

jscomp/common/js_config.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,6 @@ let refmt = ref None
109109

110110
let is_reason = ref false
111111

112-
let js_stdout = ref true
112+
let js_stdout = ref true
113+
114+
let all_module_aliases = ref false

jscomp/common/js_config.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,6 @@ val jsx_version : int ref
103103
val refmt : string option ref
104104
val is_reason : bool ref
105105

106-
val js_stdout : bool ref
106+
val js_stdout : bool ref
107+
108+
val all_module_aliases : bool ref

jscomp/core/js_implementation.ml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,34 @@ let get_lambda = fun
126126
lambda
127127
#end -> lambda
128128

129+
130+
let all_module_alias (ast : Parsetree.structure)=
131+
Ext_list.for_all ast (fun {pstr_desc} ->
132+
match pstr_desc with
133+
| Pstr_module {pmb_expr = {pmod_desc = Pmod_ident _ }}
134+
-> true
135+
| Pstr_attribute _ -> true
136+
| Pstr_eval _
137+
| Pstr_value _
138+
| Pstr_primitive _
139+
| Pstr_type _
140+
| Pstr_typext _
141+
| Pstr_exception _
142+
| Pstr_module _
143+
| Pstr_recmodule _
144+
| Pstr_modtype _
145+
| Pstr_open _
146+
| Pstr_class _
147+
| Pstr_class_type _
148+
| Pstr_include _
149+
| Pstr_extension _ -> false
150+
)
151+
129152
let after_parsing_impl ppf outputprefix ast =
130-
153+
Js_config.all_module_aliases :=
154+
!Clflags.assume_no_mli = Mli_non_exists &&
155+
all_module_alias ast
156+
;
131157
if !Js_config.binary_ast then
132158
Binary_ast.write_ast ~sourcefile:!Location.input_name
133159
Ml ~output:(outputprefix ^

jscomp/core/lam_compile_main.ml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,19 @@ let compile
254254
|> Js_shake.shake_program
255255
|> _j "shake"
256256
|> ( fun (program: J.program) ->
257-
let external_module_ids =
258-
Lam_compile_env.get_required_modules
259-
may_required_modules
260-
(Js_fold_basic.calculate_hard_dependencies program.block)
261-
|>
262-
(fun x ->
263-
if !Js_config.sort_imports then
264-
Ext_list.sort_via_array x
265-
(fun id1 id2 ->
266-
Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2)
267-
)
268-
else
269-
x
270-
)
257+
let external_module_ids : Lam_module_ident.t list =
258+
if !Js_config.all_module_aliases then []
259+
else
260+
let x = Lam_compile_env.get_required_modules
261+
may_required_modules
262+
(Js_fold_basic.calculate_hard_dependencies program.block) in
263+
if !Js_config.sort_imports then
264+
Ext_list.sort_via_array x
265+
(fun id1 id2 ->
266+
Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2)
267+
)
268+
else
269+
x
271270
in
272271
Warnings.check_fatal ();
273272
let effect =

jscomp/core/lam_convert.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ let prim = Lam.prim
161161
type required_modules = Lam_module_ident.Hash_set.t
162162

163163

164-
(** drop Lseq (List! ) etc *)
164+
(** drop Lseq (List! ) etc
165+
see #3852, we drop all these required global modules
166+
but added it back based on our own module analysis
167+
*)
165168
let rec drop_global_marker (lam : Lam.t) =
166169
match lam with
167170
| Lsequence (Lglobal_module id, rest) ->

jscomp/test/bigarray_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
var Bigarray = require("../../lib/js/bigarray.js");
34
var Caml_int32 = require("../../lib/js/caml_int32.js");
45
var Caml_external_polyfill = require("../../lib/js/caml_external_polyfill.js");
56

@@ -42,4 +43,4 @@ exports.sum = sum;
4243
exports.init = init;
4344
exports.init2 = init2;
4445
exports.init3 = init3;
45-
/* No side effect */
46+
/* Bigarray Not a pure module */

jscomp/test/build.ninja

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
bsc = ../lib/bsc.exe
3-
bsc_flags = -absname -no-alias-deps -bs-no-version-header -bs-diagnose -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:jscomp/test -w -40-52 -warn-error A+8-3-30-26+101-102-103-104-52 -bin-annot -I runtime -I $stdlib -I others
3+
bsc_flags = -absname -bs-no-version-header -bs-diagnose -bs-cross-module-opt -bs-package-name bs-platform -bs-package-output commonjs:jscomp/test -w -40-52 -warn-error A+8-3-30-26+101-102-103-104-52 -bin-annot -I runtime -I $stdlib -I others
44

55
rule cc
66
command = $bsc -bs-cmi -bs-cmj $bsc_flags -I test $in
@@ -297,6 +297,10 @@ build test/gpr_3609_test.cmi test/gpr_3609_test.cmj : cc test/gpr_3609_test.ml |
297297
build test/gpr_3697_test.cmi test/gpr_3697_test.cmj : cc test/gpr_3697_test.ml | $stdlib
298298
build test/gpr_373_test.cmi test/gpr_373_test.cmj : cc test/gpr_373_test.ml | $stdlib
299299
build test/gpr_3770_test.cmi test/gpr_3770_test.cmj : cc test/gpr_3770_test.ml | $stdlib
300+
build test/gpr_3852_alias.cmi test/gpr_3852_alias.cmj : cc test/gpr_3852_alias.ml | test/gpr_3852_effect.cmj $stdlib
301+
build test/gpr_3852_alias_reify.cmj : cc_cmi test/gpr_3852_alias_reify.ml | test/gpr_3852_alias_reify.cmi test/gpr_3852_effect.cmj $stdlib
302+
build test/gpr_3852_alias_reify.cmi : cc test/gpr_3852_alias_reify.mli | $stdlib
303+
build test/gpr_3852_effect.cmi test/gpr_3852_effect.cmj : cc test/gpr_3852_effect.ml | $stdlib
300304
build test/gpr_3865.cmi test/gpr_3865.cmj : cc test/gpr_3865.re | test/gpr_3865_bar.cmj test/gpr_3865_foo.cmj $stdlib
301305
build test/gpr_3865_bar.cmi test/gpr_3865_bar.cmj : cc test/gpr_3865_bar.re | $stdlib
302306
build test/gpr_3865_foo.cmi test/gpr_3865_foo.cmj : cc test/gpr_3865_foo.re | $stdlib

jscomp/test/gpr_3852_alias.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
4+
var A = 0;
5+
6+
exports.A = A;
7+
/* No side effect */

jscomp/test/gpr_3852_alias.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
module A = Gpr_3852_effect
4+
5+
(* It should generate a dummy JS file *)

jscomp/test/gpr_3852_alias_reify.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
var Gpr_3852_effect = require("./gpr_3852_effect.js");
4+
5+
var A = Gpr_3852_effect;
6+
7+
exports.A = A;
8+
/* Gpr_3852_effect Not a pure module */

0 commit comments

Comments
 (0)