Skip to content

Commit 38e792c

Browse files
authored
Support -open Foo where Foo is parameterised (ocaml-flambda#3489)
The command line ``` ocamlopt -open Foo -parameter P -c bar.ml ``` should be fine, even if `Foo` is itself parameterised by `P`: as usual, we compile `bar.ml` as if it began with `open! Foo`, and by the subset rule, `Bar` can refer to `Foo` because it takes at least the same parameters. Unfortunately, currently we process `-open` before `-parameter`, so when we go to check the implicit reference to `Foo`, we think there are no parameters, and we report an error. (Confusingly, the error suggests that the user add `-parameter P` to the command line.) The fix is simple: move the code that processes `-parameter` earlier so that the initial environment is constructed with the parameters already available.
1 parent 784dc96 commit 38e792c

File tree

7 files changed

+19
-21
lines changed

7 files changed

+19
-21
lines changed

driver/compile_common.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type compilation_unit_or_inferred =
3131
let with_info ~native ~tool_name ~source_file ~output_prefix
3232
~compilation_unit ~dump_ext k =
3333
Compmisc.init_path ();
34+
Compmisc.init_parameters ();
3435
let target = Unit_info.make ~source_file output_prefix in
3536
let compilation_unit =
3637
match compilation_unit with

driver/compmisc.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ let init_path ?(auto_include=auto_include) ?(dir="") () =
5555
Load_path.init ~auto_include ~visible ~hidden;
5656
Env.reset_cache ~preserve_persistent_env:false
5757

58+
(* Read any [-parameters] flags. Important to do this before [initial_env ()]
59+
because someone might [-open] a parameterised module *)
60+
let init_parameters () =
61+
let param_names = !Clflags.parameters in
62+
List.iter
63+
(fun param_name ->
64+
(* We don't (yet!) support parameterised parameters *)
65+
let param = Global_module.Name.create_no_args param_name in
66+
Env.register_parameter param
67+
)
68+
param_names
69+
5870
(* Return the initial environment in which compilation proceeds. *)
5971

6072
(* Note: do not do init_path() in initial_env, this breaks

driver/compmisc.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
val init_path :
1717
?auto_include:Load_path.auto_include_callback -> ?dir:string -> unit -> unit
18+
val init_parameters : unit -> unit
1819
val initial_env : unit -> Env.t
1920

2021
(* Support for flags that can also be set from an environment variable *)

testsuite/tests/templates/basic/main-ocamlobjinfo.reference

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Runtime parameters:
2626
Monoid_utils[Monoid:Monoid_of_semigroup]
2727
Monoid_of_semigroup
2828
Chain[Category:Category_of_monoid[Monoid:List_monoid]]
29-
Import
3029
Category_of_monoid[Monoid:List_monoid]
30+
Import
3131
List_element
3232
List_monoid
3333
Category_utils[Category:Category_of_monoid[Monoid:List_monoid]]

testsuite/tests/templates/basic/main.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
open Import
2-
31
val append3_semi
42
: Semigroup.t option
53
-> Semigroup.t option

testsuite/tests/templates/basic/test.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,11 @@
434434
ocamlc.byte;
435435
436436
{
437-
flags = "-parameter Semigroup -parameter List_element -w -misplaced-attribute";
437+
flags = "-open Import -parameter Semigroup -parameter List_element -w -misplaced-attribute";
438438
module = "main.mli";
439439
ocamlc.byte;
440440
{
441-
flags = "-parameter Semigroup -parameter List_element -w -misplaced-attribute -i";
441+
flags = "-open Import -parameter Semigroup -parameter List_element -w -misplaced-attribute -i";
442442
module = "main.ml";
443443
compiler_output = "main.output";
444444
ocamlc.byte;
@@ -919,11 +919,11 @@
919919
ocamlopt.byte;
920920
921921
{
922-
flags = "-parameter Semigroup -parameter List_element -w -misplaced-attribute";
922+
flags = "-open Import -parameter Semigroup -parameter List_element -w -misplaced-attribute";
923923
module = "main.mli";
924924
ocamlopt.byte;
925925
{
926-
flags = "-parameter Semigroup -parameter List_element -w -misplaced-attribute -i";
926+
flags = "-open Import -parameter Semigroup -parameter List_element -w -misplaced-attribute -i";
927927
module = "main.ml";
928928
compiler_output = "main.output";
929929
ocamlopt.byte;

typing/typemod.ml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,18 +3681,6 @@ let () =
36813681
type_module_type_of_fwd := type_module_type_of
36823682

36833683

3684-
(* File-level details *)
3685-
3686-
let register_params params =
3687-
List.iter
3688-
(fun param_name ->
3689-
(* We don't (yet!) support parameterised parameters *)
3690-
let param = Global_module.Name.create_no_args param_name in
3691-
Env.register_parameter param
3692-
)
3693-
params
3694-
3695-
36963684
(* Typecheck an implementation file *)
36973685

36983686
let gen_annot target annots =
@@ -3779,7 +3767,6 @@ let type_implementation target modulename initial_env ast =
37793767
ignore @@ Warnings.parse_options false "-32-34-37-38-60";
37803768
if !Clflags.as_parameter then
37813769
error Cannot_compile_implementation_as_parameter;
3782-
register_params !Clflags.parameters;
37833770
let (str, sg, names, shape, finalenv) =
37843771
Profile.record_call "infer" (fun () ->
37853772
type_structure initial_env ast) in
@@ -3967,7 +3954,6 @@ let type_interface ~sourcefile modulename env ast =
39673954
if !Clflags.as_parameter && !Clflags.parameters <> [] then begin
39683955
error Compiling_as_parameterised_parameter
39693956
end;
3970-
register_params !Clflags.parameters;
39713957
if !Clflags.binary_annotations_cms then begin
39723958
let uid = Shape.Uid.of_compilation_unit_id modulename in
39733959
cms_register_toplevel_signature_attributes ~uid ~sourcefile ast

0 commit comments

Comments
 (0)