Skip to content

Commit 2b33f24

Browse files
authored
Refactor toplevel local escape check (ocaml-flambda#104)
1 parent ed2aec6 commit 2b33f24

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

testsuite/tests/typing-local/local.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,9 @@ val local_closure : unit -> unit = <fun>
673673
*)
674674
let toplevel_stack = local_ {contents=42}
675675
[%%expect{|
676-
Line 1, characters 4-18:
676+
Line 1, characters 21-41:
677677
1 | let toplevel_stack = local_ {contents=42}
678-
^^^^^^^^^^^^^^
678+
^^^^^^^^^^^^^^^^^^^^
679679
Error: This value escapes its region
680680
|}]
681681

@@ -688,9 +688,9 @@ module M : sig end
688688

689689
let _ = local_ {contents=42}
690690
[%%expect{|
691-
Line 1, characters 4-5:
691+
Line 1, characters 8-28:
692692
1 | let _ = local_ {contents=42}
693-
^
693+
^^^^^^^^^^^^^^^^^^^^
694694
Error: This value escapes its region
695695
|}]
696696

@@ -2603,4 +2603,4 @@ let f (local_ a : string array) =
26032603

26042604
[%%expect{|
26052605
val f : local_ string array -> string ref = <fun>
2606-
|}]
2606+
|}]

typing/typecore.ml

+6-2
Original file line numberDiff line numberDiff line change
@@ -6341,6 +6341,7 @@ and type_cases
63416341
and type_let
63426342
?(check = fun s -> Warnings.Unused_var s)
63436343
?(check_strict = fun s -> Warnings.Unused_var_strict s)
6344+
?(force_global = false)
63446345
existential_context
63456346
env rec_flag spat_sexp_list allow =
63466347
let open Ast_helper in
@@ -6392,7 +6393,9 @@ and type_let
63926393
| _ -> spat
63936394
in
63946395
let pat_mode, exp_mode =
6395-
match rec_mode_var with
6396+
if force_global
6397+
then simple_pat_mode Value_mode.global, mode_global
6398+
else match rec_mode_var with
63966399
| None -> begin
63976400
match pat_tuple_arity spat with
63986401
| Not_local_tuple | Maybe_local_tuple ->
@@ -6813,12 +6816,13 @@ and type_andops env sarg sands expected_ty =
68136816
68146817
(* Typing of toplevel bindings *)
68156818
6816-
let type_binding env rec_flag spat_sexp_list =
6819+
let type_binding env rec_flag ?force_global spat_sexp_list =
68176820
Typetexp.TyVarEnv.reset ();
68186821
let (pat_exp_list, new_env, _unpacks) =
68196822
type_let
68206823
~check:(fun s -> Warnings.Unused_value_declaration s)
68216824
~check_strict:(fun s -> Warnings.Unused_value_declaration s)
6825+
?force_global
68226826
At_toplevel
68236827
env rec_flag spat_sexp_list false
68246828
in

typing/typecore.mli

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type existential_restriction =
102102

103103
val type_binding:
104104
Env.t -> rec_flag ->
105+
?force_global:bool ->
105106
Parsetree.value_binding list ->
106107
Typedtree.value_binding list * Env.t
107108
val type_let:

typing/typemod.ml

+6-7
Original file line numberDiff line numberDiff line change
@@ -2546,17 +2546,16 @@ and type_structure ?(toplevel = None) funct_body anchor env sstr =
25462546
in
25472547
Tstr_eval (expr, attrs), [], shape_map, env
25482548
| Pstr_value(rec_flag, sdefs) ->
2549+
let force_global =
2550+
(* Values bound by '_' still escape in the toplevel, because
2551+
they may be printed even though they are not named *)
2552+
Option.is_some toplevel
2553+
in
25492554
let (defs, newenv) =
2550-
Typecore.type_binding env rec_flag sdefs in
2555+
Typecore.type_binding env rec_flag ~force_global sdefs in
25512556
let () = if rec_flag = Recursive then
25522557
Typecore.check_recursive_bindings env defs
25532558
in
2554-
if Option.is_some toplevel then begin
2555-
(* Values bound by '_' still escape in the toplevel, because
2556-
they may be printed even though they are not named *)
2557-
defs |> List.iter (fun vb ->
2558-
Typecore.escape ~loc:vb.vb_pat.pat_loc ~env:newenv vb.vb_expr.exp_mode);
2559-
end;
25602559
(* Note: Env.find_value does not trigger the value_used event. Values
25612560
will be marked as being used during the signature inclusion test. *)
25622561
let items, shape_map =

0 commit comments

Comments
 (0)