Skip to content

Commit c7dac3d

Browse files
committed
Typemod: toplevel bindings escape even if no variables are bound
1 parent 82d6c3e commit c7dac3d

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

testsuite/tests/typing-local/local.ml

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,19 @@ Line 1, characters 4-18:
599599
Error: This value escapes its region
600600
|}]
601601

602+
module M = struct
603+
let _ = local_ {contents=42}
604+
end
605+
[%%expect{|
606+
module M : sig end
607+
|}]
608+
602609
let _ = local_ {contents=42}
603610
[%%expect{|
604-
- : int ref = {contents = 42}
611+
Line 1, characters 4-5:
612+
1 | let _ = local_ {contents=42}
613+
^
614+
Error: This value escapes its region
605615
|}]
606616

607617

@@ -1768,21 +1778,25 @@ let foo (local_ x) y =
17681778
val foo : local_ 'a option -> 'a option -> local_ 'a = <fun>
17691779
|}]
17701780

1771-
let (Some z, _, _) | (None, Some z, _)
1772-
| (None, None, z) = (Some (ref 0), (local_ (Some (ref 0))), (ref 0))
1781+
module M = struct
1782+
let (Some z, _, _) | (None, Some z, _)
1783+
| (None, None, z) = (Some (ref 0), (local_ (Some (ref 0))), (ref 0))
1784+
end
17731785
[%%expect{|
1774-
Line 1, characters 33-34:
1775-
1 | let (Some z, _, _) | (None, Some z, _)
1776-
^
1786+
Line 2, characters 35-36:
1787+
2 | let (Some z, _, _) | (None, Some z, _)
1788+
^
17771789
Error: This value escapes its region
17781790
|}]
17791791

1780-
let (Some z, _, _) | (None, Some z, _)
1781-
| (None, None, z) = ((local_ Some (ref 0)), (Some (ref 0)), (ref 0))
1792+
module M = struct
1793+
let (Some z, _, _) | (None, Some z, _)
1794+
| (None, None, z) = ((local_ Some (ref 0)), (Some (ref 0)), (ref 0))
1795+
end
17821796
[%%expect{|
1783-
Line 1, characters 10-11:
1784-
1 | let (Some z, _, _) | (None, Some z, _)
1785-
^
1797+
Line 2, characters 12-13:
1798+
2 | let (Some z, _, _) | (None, Some z, _)
1799+
^
17861800
Error: This value escapes its region
17871801
|}]
17881802

typing/typemod.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,12 @@ and type_structure ?(toplevel = false) funct_body anchor env sstr =
21692169
let () = if rec_flag = Recursive then
21702170
Typecore.check_recursive_bindings env defs
21712171
in
2172+
if toplevel then begin
2173+
(* Values bound by '_' still escape in the toplevel, because
2174+
they may be printed even though they are not named *)
2175+
defs |> List.iter (fun vb ->
2176+
Typecore.escape ~loc:vb.vb_pat.pat_loc ~env:newenv vb.vb_expr.exp_mode);
2177+
end;
21722178
(* Note: Env.find_value does not trigger the value_used event. Values
21732179
will be marked as being used during the signature inclusion test. *)
21742180
Tstr_value(rec_flag, defs),

0 commit comments

Comments
 (0)