Skip to content

Commit b1e5efe

Browse files
authored
Better error when using a float64 library without extension (#1859)
1 parent 198bbc4 commit b1e5efe

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

ocaml/testsuite/tests/typing-layouts-float64/basics.ml

+15
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ Line 1, characters 15-26:
2222
Error: Layout float64 is used here, but the appropriate layouts extension is not enabled
2323
|}]
2424

25+
(* CR layouts: The below test checks that we give an acceptable error for cases
26+
where a float64 was used from a library in a file where the user forgot to
27+
enable the relevant extension. It can be deleted when float64 is on by
28+
default (though at that time we should add tests for explicitly disabling the
29+
layouts extension). *)
30+
let f x = Stdlib__Float_u.sin x
31+
[%%expect{|
32+
Line 1, characters 6-31:
33+
1 | let f x = Stdlib__Float_u.sin x
34+
^^^^^^^^^^^^^^^^^^^^^^^^^
35+
Error: Non-value layout float64 detected as sort for type float#,
36+
but this requires extension layouts_beta, which is not enabled.
37+
If you intended to use this layout, please add this flag to your build file.
38+
Otherwise, please report this error to the Jane Street compilers team.
39+
|}]

ocaml/typing/typeopt.ml

+18-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ open Lambda
2525
type error =
2626
Non_value_layout of type_expr * Layout.Violation.t option
2727
| Non_value_sort of Sort.t * type_expr
28+
| Sort_without_extension of
29+
Sort.t * Language_extension.maturity * type_expr option
2830
| Non_value_sort_unknown_ty of Sort.t
2931

3032
exception Error of Location.t * error
@@ -565,15 +567,17 @@ let layout env loc sort ty =
565567
| Value -> Lambda.Pvalue (value_kind env loc ty)
566568
| Float64 when Language_extension.(is_at_least Layouts Beta) ->
567569
Lambda.Punboxed_float
568-
| Float64 -> raise (Error (loc, Non_value_sort (Sort.float64,ty)))
570+
| Float64 ->
571+
raise (Error (loc, Sort_without_extension (Sort.float64, Beta, Some ty)))
569572
| Void -> raise (Error (loc, Non_value_sort (Sort.void,ty)))
570573

571574
let layout_of_sort loc sort =
572575
match Layouts.Sort.get_default_value sort with
573576
| Value -> Lambda.Pvalue Pgenval
574577
| Float64 when Language_extension.(is_at_least Layouts Beta) ->
575578
Lambda.Punboxed_float
576-
| Float64 -> raise (Error (loc, Non_value_sort_unknown_ty Sort.float64))
579+
| Float64 ->
580+
raise (Error (loc, Sort_without_extension (Sort.float64, Beta, None)))
577581
| Void -> raise (Error (loc, Non_value_sort_unknown_ty Sort.void))
578582

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

685701
let () =
686702
Location.register_error_of_exn

0 commit comments

Comments
 (0)