diff --git a/jscomp/common/js_config.ml b/jscomp/common/js_config.ml index 19df912a242..77002f14605 100644 --- a/jscomp/common/js_config.ml +++ b/jscomp/common/js_config.ml @@ -109,4 +109,6 @@ let refmt = ref None let is_reason = ref false -let js_stdout = ref true \ No newline at end of file +let js_stdout = ref true + +let all_module_aliases = ref false \ No newline at end of file diff --git a/jscomp/common/js_config.mli b/jscomp/common/js_config.mli index 38655d63e6d..67cdab50782 100644 --- a/jscomp/common/js_config.mli +++ b/jscomp/common/js_config.mli @@ -103,4 +103,6 @@ val jsx_version : int ref val refmt : string option ref val is_reason : bool ref -val js_stdout : bool ref \ No newline at end of file +val js_stdout : bool ref + +val all_module_aliases : bool ref \ No newline at end of file diff --git a/jscomp/core/js_implementation.ml b/jscomp/core/js_implementation.ml index 3c3cdf65e6c..7154fd12ceb 100644 --- a/jscomp/core/js_implementation.ml +++ b/jscomp/core/js_implementation.ml @@ -126,8 +126,34 @@ let get_lambda = fun lambda #end -> lambda + +let all_module_alias (ast : Parsetree.structure)= + Ext_list.for_all ast (fun {pstr_desc} -> + match pstr_desc with + | Pstr_module {pmb_expr = {pmod_desc = Pmod_ident _ }} + -> true + | Pstr_attribute _ -> true + | Pstr_eval _ + | Pstr_value _ + | Pstr_primitive _ + | Pstr_type _ + | Pstr_typext _ + | Pstr_exception _ + | Pstr_module _ + | Pstr_recmodule _ + | Pstr_modtype _ + | Pstr_open _ + | Pstr_class _ + | Pstr_class_type _ + | Pstr_include _ + | Pstr_extension _ -> false + ) + let after_parsing_impl ppf outputprefix ast = - + Js_config.all_module_aliases := + !Clflags.assume_no_mli = Mli_non_exists && + all_module_alias ast + ; if !Js_config.binary_ast then Binary_ast.write_ast ~sourcefile:!Location.input_name Ml ~output:(outputprefix ^ diff --git a/jscomp/core/lam_compile_main.ml b/jscomp/core/lam_compile_main.ml index 64e84d7cbd2..16a030052bf 100644 --- a/jscomp/core/lam_compile_main.ml +++ b/jscomp/core/lam_compile_main.ml @@ -254,20 +254,19 @@ let compile |> Js_shake.shake_program |> _j "shake" |> ( fun (program: J.program) -> - let external_module_ids = - Lam_compile_env.get_required_modules - may_required_modules - (Js_fold_basic.calculate_hard_dependencies program.block) - |> - (fun x -> - if !Js_config.sort_imports then - Ext_list.sort_via_array x - (fun id1 id2 -> - Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2) - ) - else - x - ) + let external_module_ids : Lam_module_ident.t list = + if !Js_config.all_module_aliases then [] + else + let x = Lam_compile_env.get_required_modules + may_required_modules + (Js_fold_basic.calculate_hard_dependencies program.block) in + if !Js_config.sort_imports then + Ext_list.sort_via_array x + (fun id1 id2 -> + Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2) + ) + else + x in Warnings.check_fatal (); let effect = diff --git a/jscomp/core/lam_convert.ml b/jscomp/core/lam_convert.ml index ff16cb41ed7..fc11712789d 100644 --- a/jscomp/core/lam_convert.ml +++ b/jscomp/core/lam_convert.ml @@ -161,7 +161,10 @@ let prim = Lam.prim type required_modules = Lam_module_ident.Hash_set.t -(** drop Lseq (List! ) etc *) +(** drop Lseq (List! ) etc + see #3852, we drop all these required global modules + but added it back based on our own module analysis +*) let rec drop_global_marker (lam : Lam.t) = match lam with | Lsequence (Lglobal_module id, rest) -> diff --git a/jscomp/test/bigarray_test.js b/jscomp/test/bigarray_test.js index 2abd897bb63..888d9ab9b7d 100644 --- a/jscomp/test/bigarray_test.js +++ b/jscomp/test/bigarray_test.js @@ -1,5 +1,6 @@ 'use strict'; +var Bigarray = require("../../lib/js/bigarray.js"); var Caml_int32 = require("../../lib/js/caml_int32.js"); var Caml_external_polyfill = require("../../lib/js/caml_external_polyfill.js"); @@ -42,4 +43,4 @@ exports.sum = sum; exports.init = init; exports.init2 = init2; exports.init3 = init3; -/* No side effect */ +/* Bigarray Not a pure module */ diff --git a/jscomp/test/build.ninja b/jscomp/test/build.ninja index ac1ae383bc8..4039a3d9a37 100644 --- a/jscomp/test/build.ninja +++ b/jscomp/test/build.ninja @@ -1,6 +1,6 @@ bsc = ../lib/bsc.exe -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 +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 rule cc 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 | build test/gpr_3697_test.cmi test/gpr_3697_test.cmj : cc test/gpr_3697_test.ml | $stdlib build test/gpr_373_test.cmi test/gpr_373_test.cmj : cc test/gpr_373_test.ml | $stdlib build test/gpr_3770_test.cmi test/gpr_3770_test.cmj : cc test/gpr_3770_test.ml | $stdlib +build test/gpr_3852_alias.cmi test/gpr_3852_alias.cmj : cc test/gpr_3852_alias.ml | test/gpr_3852_effect.cmj $stdlib +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 +build test/gpr_3852_alias_reify.cmi : cc test/gpr_3852_alias_reify.mli | $stdlib +build test/gpr_3852_effect.cmi test/gpr_3852_effect.cmj : cc test/gpr_3852_effect.ml | $stdlib 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 build test/gpr_3865_bar.cmi test/gpr_3865_bar.cmj : cc test/gpr_3865_bar.re | $stdlib build test/gpr_3865_foo.cmi test/gpr_3865_foo.cmj : cc test/gpr_3865_foo.re | $stdlib diff --git a/jscomp/test/gpr_3852_alias.js b/jscomp/test/gpr_3852_alias.js new file mode 100644 index 00000000000..b99410b606e --- /dev/null +++ b/jscomp/test/gpr_3852_alias.js @@ -0,0 +1,7 @@ +'use strict'; + + +var A = 0; + +exports.A = A; +/* No side effect */ diff --git a/jscomp/test/gpr_3852_alias.ml b/jscomp/test/gpr_3852_alias.ml new file mode 100644 index 00000000000..2f281bb2b84 --- /dev/null +++ b/jscomp/test/gpr_3852_alias.ml @@ -0,0 +1,5 @@ + + +module A = Gpr_3852_effect + +(* It should generate a dummy JS file *) \ No newline at end of file diff --git a/jscomp/test/gpr_3852_alias_reify.js b/jscomp/test/gpr_3852_alias_reify.js new file mode 100644 index 00000000000..3e2f2a04165 --- /dev/null +++ b/jscomp/test/gpr_3852_alias_reify.js @@ -0,0 +1,8 @@ +'use strict'; + +var Gpr_3852_effect = require("./gpr_3852_effect.js"); + +var A = Gpr_3852_effect; + +exports.A = A; +/* Gpr_3852_effect Not a pure module */ diff --git a/jscomp/test/gpr_3852_alias_reify.ml b/jscomp/test/gpr_3852_alias_reify.ml new file mode 100644 index 00000000000..a3171b9557e --- /dev/null +++ b/jscomp/test/gpr_3852_alias_reify.ml @@ -0,0 +1 @@ +module A = Gpr_3852_effect \ No newline at end of file diff --git a/jscomp/test/gpr_3852_alias_reify.mli b/jscomp/test/gpr_3852_alias_reify.mli new file mode 100644 index 00000000000..e81da1ce740 --- /dev/null +++ b/jscomp/test/gpr_3852_alias_reify.mli @@ -0,0 +1,3 @@ +module A : sig + val v : int +end \ No newline at end of file diff --git a/jscomp/test/gpr_3852_effect.js b/jscomp/test/gpr_3852_effect.js new file mode 100644 index 00000000000..e21bf089d60 --- /dev/null +++ b/jscomp/test/gpr_3852_effect.js @@ -0,0 +1,9 @@ +'use strict'; + + +console.log("hello"); + +var v = 0; + +exports.v = v; +/* Not a pure module */ diff --git a/jscomp/test/gpr_3852_effect.ml b/jscomp/test/gpr_3852_effect.ml new file mode 100644 index 00000000000..48b785f2ce9 --- /dev/null +++ b/jscomp/test/gpr_3852_effect.ml @@ -0,0 +1,3 @@ + +let v = 0 +let () = Js.log "hello" \ No newline at end of file diff --git a/lib/4.02.3/bsdep.ml b/lib/4.02.3/bsdep.ml index 67616f1ab0e..70fe698a243 100644 --- a/lib/4.02.3/bsdep.ml +++ b/lib/4.02.3/bsdep.ml @@ -29759,6 +29759,8 @@ val refmt : string option ref val is_reason : bool ref val js_stdout : bool ref + +val all_module_aliases : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -29873,6 +29875,8 @@ let refmt = ref None let is_reason = ref false let js_stdout = ref true + +let all_module_aliases = ref false end module Bs_warnings : sig #1 "bs_warnings.mli" diff --git a/lib/4.02.3/bsppx.ml b/lib/4.02.3/bsppx.ml index 5751a304b86..48de487f247 100644 --- a/lib/4.02.3/bsppx.ml +++ b/lib/4.02.3/bsppx.ml @@ -2649,6 +2649,8 @@ val refmt : string option ref val is_reason : bool ref val js_stdout : bool ref + +val all_module_aliases : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -2763,6 +2765,8 @@ let refmt = ref None let is_reason = ref false let js_stdout = ref true + +let all_module_aliases = ref false end (** Interface as module *) module Asttypes diff --git a/lib/4.02.3/unstable/all_ounit_tests.ml b/lib/4.02.3/unstable/all_ounit_tests.ml index 5ed6bcb8a43..cf48b237d9d 100644 --- a/lib/4.02.3/unstable/all_ounit_tests.ml +++ b/lib/4.02.3/unstable/all_ounit_tests.ml @@ -7411,6 +7411,8 @@ val refmt : string option ref val is_reason : bool ref val js_stdout : bool ref + +val all_module_aliases : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -7525,6 +7527,8 @@ let refmt = ref None let is_reason = ref false let js_stdout = ref true + +let all_module_aliases = ref false end module Ml_binary : sig #1 "ml_binary.mli" diff --git a/lib/4.02.3/unstable/bspack.ml b/lib/4.02.3/unstable/bspack.ml index f72b0116c58..8d26854047d 100644 --- a/lib/4.02.3/unstable/bspack.ml +++ b/lib/4.02.3/unstable/bspack.ml @@ -8178,6 +8178,8 @@ val refmt : string option ref val is_reason : bool ref val js_stdout : bool ref + +val all_module_aliases : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -8292,6 +8294,8 @@ let refmt = ref None let is_reason = ref false let js_stdout = ref true + +let all_module_aliases = ref false end module Ml_binary : sig #1 "ml_binary.mli" diff --git a/lib/4.02.3/unstable/js_compiler.ml b/lib/4.02.3/unstable/js_compiler.ml index 51d3b410904..70874ebda64 100644 --- a/lib/4.02.3/unstable/js_compiler.ml +++ b/lib/4.02.3/unstable/js_compiler.ml @@ -11709,6 +11709,8 @@ val refmt : string option ref val is_reason : bool ref val js_stdout : bool ref + +val all_module_aliases : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -11823,6 +11825,8 @@ let refmt = ref None let is_reason = ref false let js_stdout = ref true + +let all_module_aliases = ref false end module Bs_warnings : sig #1 "bs_warnings.mli" @@ -112598,7 +112602,10 @@ let prim = Lam.prim type required_modules = Lam_module_ident.Hash_set.t -(** drop Lseq (List! ) etc *) +(** drop Lseq (List! ) etc + see #3852, we drop all these required global modules + but added it back based on our own module analysis +*) let rec drop_global_marker (lam : Lam.t) = match lam with | Lsequence (Lglobal_module id, rest) -> @@ -116270,20 +116277,19 @@ let compile |> Js_shake.shake_program |> _j "shake" |> ( fun (program: J.program) -> - let external_module_ids = - Lam_compile_env.get_required_modules - may_required_modules - (Js_fold_basic.calculate_hard_dependencies program.block) - |> - (fun x -> - if !Js_config.sort_imports then - Ext_list.sort_via_array x - (fun id1 id2 -> - Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2) - ) - else - x - ) + let external_module_ids : Lam_module_ident.t list = + if !Js_config.all_module_aliases then [] + else + let x = Lam_compile_env.get_required_modules + may_required_modules + (Js_fold_basic.calculate_hard_dependencies program.block) in + if !Js_config.sort_imports then + Ext_list.sort_via_array x + (fun id1 id2 -> + Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2) + ) + else + x in Warnings.check_fatal (); let effect = diff --git a/lib/4.02.3/unstable/native_ppx.ml b/lib/4.02.3/unstable/native_ppx.ml index 52680b9e32f..09e1b700d70 100644 --- a/lib/4.02.3/unstable/native_ppx.ml +++ b/lib/4.02.3/unstable/native_ppx.ml @@ -11687,6 +11687,8 @@ val refmt : string option ref val is_reason : bool ref val js_stdout : bool ref + +val all_module_aliases : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -11801,6 +11803,8 @@ let refmt = ref None let is_reason = ref false let js_stdout = ref true + +let all_module_aliases = ref false end module Bs_warnings : sig #1 "bs_warnings.mli" diff --git a/lib/4.02.3/whole_compiler.ml b/lib/4.02.3/whole_compiler.ml index 75ecfcb42c4..c996513aa00 100644 --- a/lib/4.02.3/whole_compiler.ml +++ b/lib/4.02.3/whole_compiler.ml @@ -5014,6 +5014,8 @@ val refmt : string option ref val is_reason : bool ref val js_stdout : bool ref + +val all_module_aliases : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -5128,6 +5130,8 @@ let refmt = ref None let is_reason = ref false let js_stdout = ref true + +let all_module_aliases = ref false end module Ast_reason_pp : sig #1 "ast_reason_pp.mli" @@ -110283,7 +110287,10 @@ let prim = Lam.prim type required_modules = Lam_module_ident.Hash_set.t -(** drop Lseq (List! ) etc *) +(** drop Lseq (List! ) etc + see #3852, we drop all these required global modules + but added it back based on our own module analysis +*) let rec drop_global_marker (lam : Lam.t) = match lam with | Lsequence (Lglobal_module id, rest) -> @@ -113955,20 +113962,19 @@ let compile |> Js_shake.shake_program |> _j "shake" |> ( fun (program: J.program) -> - let external_module_ids = - Lam_compile_env.get_required_modules - may_required_modules - (Js_fold_basic.calculate_hard_dependencies program.block) - |> - (fun x -> - if !Js_config.sort_imports then - Ext_list.sort_via_array x - (fun id1 id2 -> - Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2) - ) - else - x - ) + let external_module_ids : Lam_module_ident.t list = + if !Js_config.all_module_aliases then [] + else + let x = Lam_compile_env.get_required_modules + may_required_modules + (Js_fold_basic.calculate_hard_dependencies program.block) in + if !Js_config.sort_imports then + Ext_list.sort_via_array x + (fun id1 id2 -> + Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2) + ) + else + x in Warnings.check_fatal (); let effect = @@ -123163,8 +123169,34 @@ let get_lambda = fun lambda -> lambda + +let all_module_alias (ast : Parsetree.structure)= + Ext_list.for_all ast (fun {pstr_desc} -> + match pstr_desc with + | Pstr_module {pmb_expr = {pmod_desc = Pmod_ident _ }} + -> true + | Pstr_attribute _ -> true + | Pstr_eval _ + | Pstr_value _ + | Pstr_primitive _ + | Pstr_type _ + | Pstr_typext _ + | Pstr_exception _ + | Pstr_module _ + | Pstr_recmodule _ + | Pstr_modtype _ + | Pstr_open _ + | Pstr_class _ + | Pstr_class_type _ + | Pstr_include _ + | Pstr_extension _ -> false + ) + let after_parsing_impl ppf outputprefix ast = - + Js_config.all_module_aliases := + !Clflags.assume_no_mli = Mli_non_exists && + all_module_alias ast + ; if !Js_config.binary_ast then Binary_ast.write_ast ~sourcefile:!Location.input_name Ml ~output:(outputprefix ^ diff --git a/scripts/ninja.js b/scripts/ninja.js index ccd5a0e9a95..63441f9ce27 100755 --- a/scripts/ninja.js +++ b/scripts/ninja.js @@ -1229,7 +1229,7 @@ async function testNinja() { var ninjaCwd = `test`; var templateTestRules = ` ${BSC_COMPILER} -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 +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 ${ruleCC(ninjaCwd)}