Skip to content

Commit cc97ac4

Browse files
authored
Improve layout history error message: additional info on value layout defaulting (#1855)
* add message when defaulting to layout value * test changes * relax to all const defaulting * test changes * improve wording * test changes --------- Co-authored-by: alanechang <[email protected]>
1 parent 7ac8445 commit cc97ac4

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ Error: This expression has type t_float64
405405
The layout of t_float64 is float64, because
406406
of the annotation on the declaration of the type t_float64.
407407
But the layout of t_float64 must be a sublayout of value, because
408-
it's used as a function result.
408+
it's used as a function result, defaulted to layout value.
409409
|}];;
410410

411411
let x8_2 = id_value (make_t_float64_id ());;
@@ -418,7 +418,7 @@ Error: This expression has type 'a t_float64_id = ('a : float64)
418418
The layout of 'a t_float64_id is float64, because
419419
of the annotation on 'a in the declaration of the type t_float64_id.
420420
But the layout of 'a t_float64_id must overlap with value, because
421-
it's used as a function result.
421+
it's used as a function result, defaulted to layout value.
422422
|}];;
423423

424424
let x8_3 = id_value (make_floatu ());;
@@ -431,7 +431,7 @@ Error: This expression has type float# but an expression was expected of type
431431
The layout of float# is float64, because
432432
it is the primitive float64 type float#.
433433
But the layout of float# must be a sublayout of value, because
434-
it's used as a function result.
434+
it's used as a function result, defaulted to layout value.
435435
|}];;
436436

437437
(*************************************)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,10 @@ Error: Layout void is used here, but the appropriate layouts extension is not en
328328

329329
(* CR layouts: This test moved to [basics_beta.ml] as it needs an immediate
330330
type parameter. Bring back here when we have one enabled by default. *)
331+
332+
333+
(****************************************************)
334+
(* Test 35: unannotated type parameter defaults to layout value *)
335+
336+
(* CR layouts: This test moved to [basics_alpha.ml] as it needs a non-value
337+
sort. Bring back here when we have one enabled by default. *)

ocaml/testsuite/tests/typing-layouts/basics_alpha.ml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Error: The type constraints are not consistent.
7171
The layout of t is any, because
7272
of the annotation on the declaration of the type t.
7373
But the layout of t must be a sublayout of '_representable_layout_3, because
74-
it appears as an unannotated type parameter.
74+
it instantiates an unannotated type parameter.
7575
|}]
7676

7777
module type S1 = sig
@@ -88,7 +88,7 @@ Error: The type constraints are not consistent.
8888
The layout of t is any, because
8989
of the annotation on the declaration of the type t.
9090
But the layout of t must be a sublayout of '_representable_layout_4, because
91-
it appears as an unannotated type parameter.
91+
it instantiates an unannotated type parameter.
9292
|}]
9393

9494
let f1 () : t_any = assert false;;
@@ -902,10 +902,13 @@ Line 3, characters 11-12:
902902
^
903903
Error: Variables bound in a class must have layout value.
904904
The layout of v is void, because
905-
it's bound by a `let`.
905+
it's bound by a `let`, defaulted to layout void.
906906
But the layout of v must be a sublayout of value, because
907907
it's let-bound in a class expression.
908908
|}];;
909+
(* CR layouts v2.9: The part about defaulting here is incorrect.
910+
It's due to the logic in Pcl_let using sorts directly instead of
911+
layouts. *)
909912

910913
(* Hits the Cfk_concrete case of Pcf_val *)
911914
module M12_2 = struct
@@ -1709,7 +1712,7 @@ Error: This expression has type t_void but an expression was expected of type
17091712
The layout of t_void is void, because
17101713
of the annotation on the declaration of the type t_void.
17111714
But the layout of t_void must be a sublayout of value, because
1712-
it's used as a function argument.
1715+
it's used as a function argument, defaulted to layout value.
17131716
|}]
17141717

17151718
(**************************************)
@@ -1738,7 +1741,7 @@ Error: This expression has type t_void but an expression was expected of type
17381741
The layout of t_void is void, because
17391742
of the annotation on the declaration of the type t_void.
17401743
But the layout of t_void must be a sublayout of value, because
1741-
it's used as a function argument.
1744+
it's used as a function argument, defaulted to layout value.
17421745
|}]
17431746

17441747
(**************************************************)
@@ -1801,3 +1804,21 @@ Error: This type signature for foo33 is not a value type.
18011804
(* Test 34: Layout clash in polymorphic record type *)
18021805

18031806
(* tested elsewhere *)
1807+
1808+
(****************************************************)
1809+
(* Test 35: unannotated type parameter defaults to layout value *)
1810+
type 'a t35 = 'a
1811+
let f35 (x: t_void): 'a t35 = x
1812+
1813+
[%%expect{|
1814+
type 'a t35 = 'a
1815+
Line 2, characters 30-31:
1816+
2 | let f35 (x: t_void): 'a t35 = x
1817+
^
1818+
Error: This expression has type t_void but an expression was expected of type
1819+
'a t35 = ('a : value)
1820+
The layout of t_void is void, because
1821+
of the annotation on the declaration of the type t_void.
1822+
But the layout of t_void must be a sublayout of value, because
1823+
it instantiates an unannotated type parameter, defaulted to layout value.
1824+
|}]

ocaml/testsuite/tests/typing-layouts/basics_beta.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,3 +579,9 @@ Error: The layout of type 'a is value, because
579579
But the layout of type 'a must be a sublayout of immediate, because
580580
of the annotation on 'a in the declaration of the type t2_imm.
581581
|}]
582+
583+
(****************************************************)
584+
(* Test 35: unannotated type parameter defaults to layout value *)
585+
586+
(* CR layouts v2.5: This test moved to [basics_alpha.ml] as it needs a non-value
587+
sort. Bring back here when we have one. *)

ocaml/testsuite/tests/typing-layouts/datatypes_alpha.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Error:
9090
The layout of 'b t1_constraint' is any, because
9191
of the annotation on the declaration of the type t_any.
9292
But the layout of 'b t1_constraint' must be a sublayout of '_representable_layout_4, because
93-
it appears as an unannotated type parameter.
93+
it instantiates an unannotated type parameter.
9494
|}]
9595
(* CR layouts errors: this error is blamed on the wrong piece *)
9696

ocaml/typing/layouts.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ module Layout = struct
659659
fprintf ppf "it's used in the declaration of the record field \"%a\""
660660
Ident.print lbl
661661
| Unannotated_type_parameter ->
662-
fprintf ppf "it appears as an unannotated type parameter"
662+
fprintf ppf "it instantiates an unannotated type parameter"
663663
| Record_projection ->
664664
fprintf ppf "it's used as the record in a projection"
665665
| Record_assignment ->
@@ -852,7 +852,13 @@ module Layout = struct
852852
format_desc lay;
853853
begin match t.history with
854854
| Creation reason ->
855-
fprintf ppf ", because@ %a" format_creation_reason reason
855+
fprintf ppf ", because@ %a" format_creation_reason reason;
856+
begin match reason, lay with
857+
| Concrete_creation _, Const _ ->
858+
fprintf ppf ", defaulted to layout %a"
859+
format_desc lay
860+
| _ -> ()
861+
end
856862
| _ -> assert false
857863
end;
858864
fprintf ppf ".@]"

0 commit comments

Comments
 (0)