Skip to content

Improve layout history error message: additional info on value layout defaulting #1855

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 6 commits into from
Sep 29, 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
6 changes: 3 additions & 3 deletions ocaml/testsuite/tests/typing-layouts-float64/basics_alpha.ml
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ Error: This expression has type t_float64
The layout of t_float64 is float64, because
of the annotation on the declaration of the type t_float64.
But the layout of t_float64 must be a sublayout of value, because
it's used as a function result.
it's used as a function result, defaulted to layout value.
|}];;

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

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

(*************************************)
Expand Down
7 changes: 7 additions & 0 deletions ocaml/testsuite/tests/typing-layouts/basics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,10 @@ Error: Layout void is used here, but the appropriate layouts extension is not en

(* CR layouts: This test moved to [basics_beta.ml] as it needs an immediate
type parameter. Bring back here when we have one enabled by default. *)


(****************************************************)
(* Test 35: unannotated type parameter defaults to layout value *)

(* CR layouts: This test moved to [basics_alpha.ml] as it needs a non-value
sort. Bring back here when we have one enabled by default. *)
31 changes: 26 additions & 5 deletions ocaml/testsuite/tests/typing-layouts/basics_alpha.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Error: The type constraints are not consistent.
The layout of t is any, because
of the annotation on the declaration of the type t.
But the layout of t must be a sublayout of '_representable_layout_3, because
it appears as an unannotated type parameter.
it instantiates an unannotated type parameter.
|}]

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

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

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

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

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

(* tested elsewhere *)

(****************************************************)
(* Test 35: unannotated type parameter defaults to layout value *)
type 'a t35 = 'a
let f35 (x: t_void): 'a t35 = x

[%%expect{|
type 'a t35 = 'a
Line 2, characters 30-31:
2 | let f35 (x: t_void): 'a t35 = x
^
Error: This expression has type t_void but an expression was expected of type
'a t35 = ('a : value)
The layout of t_void is void, because
of the annotation on the declaration of the type t_void.
But the layout of t_void must be a sublayout of value, because
it instantiates an unannotated type parameter, defaulted to layout value.
|}]
6 changes: 6 additions & 0 deletions ocaml/testsuite/tests/typing-layouts/basics_beta.ml
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,9 @@ Error: The layout of type 'a is value, because
But the layout of type 'a must be a sublayout of immediate, because
of the annotation on 'a in the declaration of the type t2_imm.
|}]

(****************************************************)
(* Test 35: unannotated type parameter defaults to layout value *)

(* CR layouts v2.5: This test moved to [basics_alpha.ml] as it needs a non-value
sort. Bring back here when we have one. *)
2 changes: 1 addition & 1 deletion ocaml/testsuite/tests/typing-layouts/datatypes_alpha.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Error:
The layout of 'b t1_constraint' is any, because
of the annotation on the declaration of the type t_any.
But the layout of 'b t1_constraint' must be a sublayout of '_representable_layout_4, because
it appears as an unannotated type parameter.
it instantiates an unannotated type parameter.
|}]
(* CR layouts errors: this error is blamed on the wrong piece *)

Expand Down
10 changes: 8 additions & 2 deletions ocaml/typing/layouts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ module Layout = struct
fprintf ppf "it's used in the declaration of the record field \"%a\""
Ident.print lbl
| Unannotated_type_parameter ->
fprintf ppf "it appears as an unannotated type parameter"
fprintf ppf "it instantiates an unannotated type parameter"
| Record_projection ->
fprintf ppf "it's used as the record in a projection"
| Record_assignment ->
Expand Down Expand Up @@ -852,7 +852,13 @@ module Layout = struct
format_desc lay;
begin match t.history with
| Creation reason ->
fprintf ppf ", because@ %a" format_creation_reason reason
fprintf ppf ", because@ %a" format_creation_reason reason;
begin match reason, lay with
| Concrete_creation _, Const _ ->
fprintf ppf ", defaulted to layout %a"
format_desc lay
| _ -> ()
end
| _ -> assert false
end;
fprintf ppf ".@]"
Expand Down