Skip to content

Better error when using a float64 library without extension #1859

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Sep 25, 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
15 changes: 15 additions & 0 deletions ocaml/testsuite/tests/typing-layouts-float64/basics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ Line 1, characters 15-26:
Error: Layout float64 is used here, but the appropriate layouts extension is not enabled
|}]

(* CR layouts: The below test checks that we give an acceptable error for cases
where a float64 was used from a library in a file where the user forgot to
enable the relevant extension. It can be deleted when float64 is on by
default (though at that time we should add tests for explicitly disabling the
layouts extension). *)
let f x = Stdlib__Float_u.sin x
[%%expect{|
Line 1, characters 6-31:
1 | let f x = Stdlib__Float_u.sin x
^^^^^^^^^^^^^^^^^^^^^^^^^
Error: Non-value layout float64 detected as sort for type float#,
but this requires extension layouts_beta, which is not enabled.
If you intended to use this layout, please add this flag to your build file.
Otherwise, please report this error to the Jane Street compilers team.
|}]
20 changes: 18 additions & 2 deletions ocaml/typing/typeopt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ open Lambda
type error =
Non_value_layout of type_expr * Layout.Violation.t option
| Non_value_sort of Sort.t * type_expr
| Sort_without_extension of
Sort.t * Language_extension.maturity * type_expr option
| Non_value_sort_unknown_ty of Sort.t

exception Error of Location.t * error
Expand Down Expand Up @@ -565,15 +567,17 @@ let layout env loc sort ty =
| Value -> Lambda.Pvalue (value_kind env loc ty)
| Float64 when Language_extension.(is_at_least Layouts Beta) ->
Lambda.Punboxed_float
| Float64 -> raise (Error (loc, Non_value_sort (Sort.float64,ty)))
| Float64 ->
raise (Error (loc, Sort_without_extension (Sort.float64, Beta, Some ty)))
| Void -> raise (Error (loc, Non_value_sort (Sort.void,ty)))

let layout_of_sort loc sort =
match Layouts.Sort.get_default_value sort with
| Value -> Lambda.Pvalue Pgenval
| Float64 when Language_extension.(is_at_least Layouts Beta) ->
Lambda.Punboxed_float
| Float64 -> raise (Error (loc, Non_value_sort_unknown_ty Sort.float64))
| Float64 ->
raise (Error (loc, Sort_without_extension (Sort.float64, Beta, None)))
| Void -> raise (Error (loc, Non_value_sort_unknown_ty Sort.void))

let layout_of_const_sort (s : Layouts.Sort.const) =
Expand Down Expand Up @@ -681,6 +685,18 @@ let report_error ppf = function
"Non-value layout %a detected in [layout_of_sort]@ Please report this \
error to the Jane Street compilers team."
Sort.format sort
| Sort_without_extension (sort, maturity, ty) ->
fprintf ppf "Non-value layout %a detected" Sort.format sort;
begin match ty with
| None -> ()
| Some ty -> fprintf ppf " as sort for type@ %a" Printtyp.type_expr ty
end;
fprintf ppf
",@ but this requires extension %s, which is not enabled.@ \
If you intended to use this layout, please add this flag to your \
build file.@ \
Otherwise, please report this error to the Jane Street compilers team."
(Language_extension.to_command_line_string Layouts maturity)

let () =
Location.register_error_of_exn
Expand Down