Skip to content

Commit 5506f54

Browse files
authored
Make environment lazy in preparation for simd extension (#1570)
1 parent 3109a08 commit 5506f54

File tree

12 files changed

+54
-16
lines changed

12 files changed

+54
-16
lines changed

ocaml/debugger/loadprinter.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ let match_printer_type desc typename =
105105
let printer_type =
106106
match
107107
Env.find_type_by_name
108-
(Ldot(Lident "Topdirs", typename)) Env.initial_safe_string
108+
(Ldot(Lident "Topdirs", typename)) (Lazy.force Env.initial_safe_string)
109109
with
110110
| path, _ -> path
111111
| exception Not_found ->
112112
raise (Error(Unbound_identifier(Ldot(Lident "Topdirs", typename))))
113113
in
114114
Ctype.begin_def();
115115
let ty_arg = Ctype.newvar Layout.(value ~why:Debug_printer_argument) in
116-
Ctype.unify Env.initial_safe_string
116+
Ctype.unify (Lazy.force Env.initial_safe_string)
117117
(Ctype.newconstr printer_type [ty_arg])
118118
(Ctype.instance desc.val_type);
119119
Ctype.end_def();

ocaml/lambda/matching.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,8 @@ let get_mod_field modname field =
18981898
lazy
18991899
(let mod_ident = Ident.create_persistent modname in
19001900
let env =
1901-
Env.add_persistent_structure mod_ident Env.initial_safe_string
1901+
Env.add_persistent_structure mod_ident
1902+
(Lazy.force Env.initial_safe_string)
19021903
in
19031904
match Env.open_pers_signature modname env with
19041905
| Error `Not_found ->
@@ -3580,7 +3581,7 @@ let failure_handler ~scopes loc ~failer () =
35803581
let sloc = Scoped_location.of_location ~scopes loc in
35813582
let slot =
35823583
transl_extension_path sloc
3583-
Env.initial_safe_string Predef.path_match_failure
3584+
(Lazy.force Env.initial_safe_string) Predef.path_match_failure
35843585
in
35853586
let fname, line, char =
35863587
Location.get_pos_info loc.Location.loc_start in

ocaml/lambda/transl_array_comprehension.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ end = struct
210210
let slot =
211211
transl_extension_path
212212
loc
213-
Env.initial_safe_string
213+
(Lazy.force Env.initial_safe_string)
214214
Predef.path_invalid_argument
215215
in
216216
(* CR-someday aspectorzabusky: We might want to raise an event here for

ocaml/lambda/translcore.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ let event_function ~scopes exp lam =
297297
let assert_failed ~scopes exp =
298298
let slot =
299299
transl_extension_path Loc_unknown
300-
Env.initial_safe_string Predef.path_assert_failure
300+
(Lazy.force Env.initial_safe_string) Predef.path_assert_failure
301301
in
302302
let loc = exp.exp_loc in
303303
let (fname, line, char) =

ocaml/testsuite/tests/compiler-libs/test_untypeast.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
let res =
1010
let s = {| match None with Some (Some _) -> () | _ -> () |} in
1111
let pe = Parse.expression (Lexing.from_string s) in
12-
let te = Typecore.type_expression (Env.initial_safe_string) pe in
12+
let te = Typecore.type_expression (Lazy.force Env.initial_safe_string) pe in
1313
let ute = Untypeast.untype_expression te in
1414
Format.asprintf "%a" Pprintast.expression ute
1515

ocaml/testsuite/tests/language-extensions/language_extensions.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let report ~name ~text =
2222

2323
let typecheck_with_extension ?(full_name = false) name =
2424
let success =
25-
match Typecore.type_expression Env.initial_safe_string
25+
match Typecore.type_expression (Lazy.force Env.initial_safe_string)
2626
extension_parsed_expression
2727
with
2828
| _ -> true

ocaml/typing/ctype.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ let in_current_module = function
373373

374374
let in_pervasives p =
375375
in_current_module p &&
376-
try ignore (Env.find_type p Env.initial_safe_string); true
376+
try ignore (Env.find_type p (Lazy.force Env.initial_safe_string)); true
377377
with Not_found -> false
378378

379379
let is_datatype decl=

ocaml/typing/env.ml

+20-2
Original file line numberDiff line numberDiff line change
@@ -2677,13 +2677,31 @@ let save_signature_with_imports ~alerts sg modname filename imports =
26772677
save_signature_with_transform with_imports
26782678
~alerts sg modname filename
26792679

2680-
(* Make the initial environment *)
2680+
(* Make the initial environment, without language extensions *)
26812681
let (initial_safe_string, initial_unsafe_string) =
26822682
Predef.build_initial_env
26832683
(add_type ~check:false)
26842684
(add_extension ~check:false ~rebind:false)
26852685
empty
26862686

2687+
let add_language_extension_types env =
2688+
lazy
2689+
((* CR ccasinghino for mslater: Here, check the simd extension. If it's on,
2690+
return [add_simd_extension_types (add_type ~check:false) env].
2691+
Otherwise, return env. *)
2692+
env)
2693+
2694+
(* Some predefined types are part of language extensions, and we don't want to
2695+
make them available in the initial environment if those extensions are not
2696+
turned on. We can't do this at startup because command line flags haven't
2697+
been parsed yet. So, we make the initial environment lazy.
2698+
2699+
If language extensions are adjusted after [initial_safe_string] and
2700+
[initial_unsafe_string] are forced, these environments may be inaccurate.
2701+
*)
2702+
let initial_safe_string = add_language_extension_types initial_safe_string
2703+
let initial_unsafe_string = add_language_extension_types initial_unsafe_string
2704+
26872705
(* Tracking usage *)
26882706

26892707
let mark_module_used uid =
@@ -3174,7 +3192,7 @@ let lookup_all_dot_constructors ~errors ~use ~loc usage l s env =
31743192
| Longident.Lident "*predef*" ->
31753193
(* Hack to support compilation of default arguments *)
31763194
lookup_all_ident_constructors
3177-
~errors ~use ~loc usage s initial_safe_string
3195+
~errors ~use ~loc usage s (Lazy.force initial_safe_string)
31783196
| _ ->
31793197
let (_, comps) = lookup_structure_components ~errors ~use ~loc l env in
31803198
match NameMap.find s comps.comp_constrs with

ocaml/typing/env.mli

+9-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,15 @@ type address =
5959
type t
6060

6161
val empty: t
62-
val initial_safe_string: t
63-
val initial_unsafe_string: t
62+
63+
(* These environments are lazy so that they may depend on the enabled
64+
extensions, typically adjusted via command line flags. If extensions are
65+
changed after these environments are forced, they may be inaccurate. This
66+
could happen, for example, if extensions are adjusted via the
67+
compiler-libs. *)
68+
val initial_safe_string: t Lazy.t
69+
val initial_unsafe_string: t Lazy.t
70+
6471
val diff: t -> t -> Ident.t list
6572

6673
type type_descr_kind =

ocaml/typing/predef.ml

+6
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ let build_initial_env add_type add_exception empty_env =
295295
let unsafe_string = add_type ident_bytes ~manifest:type_string common in
296296
(safe_string, unsafe_string)
297297

298+
let add_simd_extension_types add_type env =
299+
let add_type = mk_add_type add_type in
300+
(* CR ccasinghino for mslater: Change the line below to [add_type ident_vec128
301+
env]. *)
302+
ignore add_type; env
303+
298304
let builtin_values =
299305
List.map (fun id -> (Ident.name id, id)) all_predef_exns
300306

ocaml/typing/predef.mli

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ val build_initial_env:
8080
(Ident.t -> extension_constructor -> 'a -> 'a) ->
8181
'a -> 'a * 'a
8282

83+
(* Add simd types to an environment. This is separate from [build_initial_env]
84+
because we'd like to only do it if the simd extension is on. *)
85+
val add_simd_extension_types :
86+
(Ident.t -> type_declaration -> 'a -> 'a) -> 'a -> 'a
87+
8388
(* To initialize linker tables *)
8489

8590
val builtin_values: (string * Ident.t) list

ocaml/typing/typemod.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ let initial_env ~loc ~safe_string ~initially_opened_module
193193
~open_implicit_modules =
194194
let env =
195195
if safe_string then
196-
Env.initial_safe_string
196+
Lazy.force Env.initial_safe_string
197197
else
198-
Env.initial_unsafe_string
198+
Lazy.force Env.initial_unsafe_string
199199
in
200200
let open_module env m =
201201
let open Asttypes in
@@ -3382,7 +3382,8 @@ let package_units initial_env objfiles cmifile modulename =
33823382
let modname = Compilation_unit.create_child modulename unit in
33833383
let sg = Env.read_signature modname (pref ^ ".cmi") in
33843384
if Filename.check_suffix f ".cmi" &&
3385-
not(Mtype.no_code_needed_sig Env.initial_safe_string sg)
3385+
not(Mtype.no_code_needed_sig (Lazy.force Env.initial_safe_string)
3386+
sg)
33863387
then raise(Error(Location.none, Env.empty,
33873388
Implementation_is_required f));
33883389
Compilation_unit.name modname,

0 commit comments

Comments
 (0)