Skip to content

Commit 37317bb

Browse files
authored
flambda-backend: Quick temporary fix for rec mod layouts segfault (#2104)
1 parent 5136440 commit 37317bb

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-2
lines changed

lambda/translmod.ml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type unsafe_component =
3131
| Unsafe_functor
3232
| Unsafe_non_function
3333
| Unsafe_typext
34+
| Unsafe_non_value_arg
3435

3536
type unsafe_info =
3637
| Unsafe of { reason:unsafe_component; loc:Location.t; subid:Ident.t }
@@ -322,8 +323,17 @@ let init_shape id modl =
322323
| Sig_value(subid, {val_kind=Val_reg; val_type=ty; val_loc=loc},_) :: rem ->
323324
let init_v =
324325
match get_desc (Ctype.expand_head env ty) with
325-
Tarrow(_,_,_,_) ->
326-
const_int 0 (* camlinternalMod.Function *)
326+
Tarrow(_,ty_arg,_,_) -> begin
327+
(* CR layouts: We should allow any representable layout here. It
328+
will require reworking [camlinternalMod.init_mod]. *)
329+
let jkind = Jkind.value ~why:Recmod_fun_arg in
330+
let ty_arg = Ctype.correct_levels ty_arg in
331+
match Ctype.check_type_jkind env ty_arg jkind with
332+
| Ok _ -> const_int 0 (* camlinternalMod.Function *)
333+
| Error _ ->
334+
let unsafe = Unsafe {reason=Unsafe_non_value_arg; loc; subid} in
335+
raise (Initialization_failure unsafe)
336+
end
327337
| Tconstr(p, _, _) when Path.same p Predef.path_lazy_t ->
328338
const_int 1 (* camlinternalMod.Lazy *)
329339
| _ ->
@@ -1890,6 +1900,9 @@ let explanation_submsg (id, unsafe_info) =
18901900
| Unsafe_typext ->
18911901
print "Module %s defines an unsafe extension constructor, %s ."
18921902
| Unsafe_non_function -> print "Module %s defines an unsafe value, %s ."
1903+
| Unsafe_non_value_arg ->
1904+
print "Module %s defines a function whose first argument \
1905+
is not a value, %s ."
18931906

18941907
let report_error loc = function
18951908
| Circular_dependency cycle ->

lambda/translmod.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type unsafe_component =
4545
| Unsafe_functor
4646
| Unsafe_non_function
4747
| Unsafe_typext
48+
| Unsafe_non_value_arg
4849

4950
type unsafe_info =
5051
| Unsafe of { reason:unsafe_component; loc:Location.t; subid:Ident.t }

testsuite/tests/typing-layouts/modules.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,25 @@ Line 1, characters 28-33:
572572
Error: This type signature for x is not a value type.
573573
x has layout any, which is not a sublayout of value.
574574
|}]
575+
576+
(****************************************************************)
577+
(* Test 9: Non-values temporarily banned in recmod safety check *)
578+
module type S = sig
579+
val f : ('a : float64). 'a -> 'a
580+
end
581+
582+
module rec M : S = M
583+
584+
[%%expect{|
585+
module type S = sig val f : ('a : float64). 'a -> 'a end
586+
Line 5, characters 19-20:
587+
5 | module rec M : S = M
588+
^
589+
Error: Cannot safely evaluate the definition of the following cycle
590+
of recursively-defined modules: M -> M.
591+
There are no safe modules in this cycle (see manual section 12.2).
592+
Line 2, characters 2-34:
593+
2 | val f : ('a : float64). 'a -> 'a
594+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
595+
Module M defines a function whose first argument is not a value, f .
596+
|}]

testsuite/tests/typing-layouts/modules_alpha.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,25 @@ Line 1, characters 28-33:
488488
Error: This type signature for x is not a value type.
489489
x has layout any, which is not a sublayout of value.
490490
|}]
491+
492+
(****************************************************************)
493+
(* Test 9: Non-values temporarily banned in recmod safety check *)
494+
module type S = sig
495+
val f : ('a : float64). 'a -> 'a
496+
end
497+
498+
module rec M : S = M
499+
500+
[%%expect{|
501+
module type S = sig val f : ('a : float64). 'a -> 'a end
502+
Line 5, characters 19-20:
503+
5 | module rec M : S = M
504+
^
505+
Error: Cannot safely evaluate the definition of the following cycle
506+
of recursively-defined modules: M -> M.
507+
There are no safe modules in this cycle (see manual section 12.2).
508+
Line 2, characters 2-34:
509+
2 | val f : ('a : float64). 'a -> 'a
510+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
511+
Module M defines a function whose first argument is not a value, f .
512+
|}]

typing/jkind.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ type value_creation_reason =
604604
| Debug_printer_argument
605605
| V1_safety_check
606606
| Captured_in_object
607+
| Recmod_fun_arg
607608
| Unknown of string
608609

609610
type immediate_creation_reason =
@@ -1051,6 +1052,9 @@ end = struct
10511052
fprintf ppf "used as the argument to a debugger printer function"
10521053
| V1_safety_check -> fprintf ppf "to be value for the V1 safety check"
10531054
| Captured_in_object -> fprintf ppf "captured in an object"
1055+
| Recmod_fun_arg ->
1056+
fprintf ppf
1057+
"used as the first argument to a function in a recursive module"
10541058
| Unknown s ->
10551059
fprintf ppf
10561060
"unknown @[(please alert the Jane Street@;\
@@ -1405,6 +1409,7 @@ module Debug_printers = struct
14051409
| Debug_printer_argument -> fprintf ppf "Debug_printer_argument"
14061410
| V1_safety_check -> fprintf ppf "V1_safety_check"
14071411
| Captured_in_object -> fprintf ppf "Captured_in_object"
1412+
| Recmod_fun_arg -> fprintf ppf "Recmod_fun_arg"
14081413
| Unknown s -> fprintf ppf "Unknown %s" s
14091414

14101415
let void_creation_reason ppf : void_creation_reason -> _ = function

typing/jkind.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ type value_creation_reason =
228228
| Debug_printer_argument
229229
| V1_safety_check
230230
| Captured_in_object
231+
| Recmod_fun_arg
231232
| Unknown of string (* CR layouts: get rid of these *)
232233

233234
type immediate_creation_reason =

0 commit comments

Comments
 (0)