Skip to content

Ensure that all [val]s are [value]s. #1481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ocaml/testsuite/tests/typing-layouts/basics_alpha.ml
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ module F2 (X : sig val x : t_void end) = struct
let f () = X.x
end;;
[%%expect{|
Line 2, characters 8-16:
2 | let f () = X.x
^^^^^^^^
Error: Non-value detected in [value_kind].
Please report this error to the Jane Street compilers team.
t_void has layout void, which is not a sublayout of value.
Line 1, characters 27-33:
1 | module F2 (X : sig val x : t_void end) = struct
^^^^^^
Error: This type signature for x is not a value type.
x has layout void, which is not a sublayout of value.
|}];;
(* CR layouts v5: the test above should be made to work *)

module F2 (X : sig val f : void_record -> unit end) = struct
let g z = X.f { vr_void = z; vr_int = 42 }
Expand Down
5 changes: 5 additions & 0 deletions ocaml/testsuite/tests/typing-layouts/modules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,8 @@ end;;
[%%expect {|
module F : sig end -> sig end
|}];;

(****************************************)
(* Test 8: [val]s must be representable *)

(* CR layouts: Bring this test back from modules_alpha *)
17 changes: 17 additions & 0 deletions ocaml/testsuite/tests/typing-layouts/modules_alpha.ml
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,20 @@ end;;
[%%expect {|
module F : sig end -> sig end
|}];;

(****************************************)
(* Test 8: [val]s must be representable *)

module type S = sig val x : t_any end

module M = struct
let x : t_void = assert false
end

[%%expect{|
Line 1, characters 28-33:
1 | module type S = sig val x : t_any end
^^^^^
Error: This type signature for x is not a value type.
x has layout any, which is not a sublayout of value.
|}]
5 changes: 5 additions & 0 deletions ocaml/testsuite/tests/typing-layouts/modules_beta.ml
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,8 @@ end;;
[%%expect {|
module F : sig end -> sig end
|}];;

(****************************************)
(* Test 8: [val]s must be representable *)

(* CR layouts: Bring this test back from modules_alpha *)
11 changes: 11 additions & 0 deletions ocaml/typing/typedecl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type error =
; err : Layout.Violation.t
}
| Layout_empty_record
| Non_value_in_sig of Layout.Violation.t * string
| Separability of Typedecl_separability.error
| Bad_unboxed_attribute of string
| Boxed_and_unboxed
Expand Down Expand Up @@ -1957,6 +1958,13 @@ let check_unboxable env loc ty =
(* Translate a value declaration *)
let transl_value_decl env loc valdecl =
let cty = Typetexp.transl_type_scheme env valdecl.pval_type in
(* CR layouts v5: relax this to check for representability. *)
begin match Ctype.constrain_type_layout env cty.ctyp_type
(Layout.value ~why:Structure_element) with
| Ok () -> ()
| Error err ->
raise(Error(cty.ctyp_loc, Non_value_in_sig(err, valdecl.pval_name.txt)))
end;
let ty = cty.ctyp_type in
let v =
match valdecl.pval_prim with
Expand Down Expand Up @@ -2506,6 +2514,9 @@ let report_error ppf = function
~offender:(fun ppf -> Printtyp.type_expr ppf typ)) err
| Layout_empty_record ->
fprintf ppf "@[Records must contain at least one runtime value.@]"
| Non_value_in_sig (err, val_name) ->
fprintf ppf "@[This type signature for %s is not a value type.@ %a@]"
val_name (Layout.Violation.report_with_name ~name:val_name) err
| Bad_unboxed_attribute msg ->
fprintf ppf "@[This type cannot be unboxed because@ %s.@]" msg
| Separability (Typedecl_separability.Non_separable_evar evar) ->
Expand Down
1 change: 1 addition & 0 deletions ocaml/typing/typedecl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type error =
; err : Layout.Violation.t
}
| Layout_empty_record
| Non_value_in_sig of Layout.Violation.t * string
| Separability of Typedecl_separability.error
| Bad_unboxed_attribute of string
| Boxed_and_unboxed
Expand Down