Skip to content

Commit 0df692c

Browse files
authored
flambda-backend: Implement layout histories (#2196)
* Enable layout histories (#1823) * enable display_histories * accept test changes and mark them as unreviewed * improve CR messages for unreviewed tests --------- Co-authored-by: Alan Chang <[email protected]> Remove duplicated creation_reasons from layout history error messages (#1825) * stop storing duplicates in creation_reasons * accept test changes * reduce to only displaying one creation reason * accept test changes * prefer other reasons over Concrete_creation * accept test changes * stop building history tree if flatten is enabled * remove CR version Co-authored-by: Richard Eisenberg <[email protected]> * Add CR for assumption in combine_histories Co-authored-by: Richard Eisenberg <[email protected]> --------- Co-authored-by: Alan Chang <[email protected]> Co-authored-by: Richard Eisenberg <[email protected]> Improve layout history error message formatting and wording (#1832) * remove extra whitespace * fix Unequal_var_layouts error fmt * improve message wording * fix spacing * accept test changes * grammar and phrasing * test changes --------- Co-authored-by: Alan Chang <[email protected]> Improve layout history error message: add path field to Type_argument value_creation_reason (#1834) * add Path.t to Type_argument * test changes * add Imported_type_argument * Imported_type_argument for imported type params * fix float64 error format * test changes * add more tests * update layout reason not just in failure case * tests with more than one type param * combine Imported_type_argument with Type_argument * add option_argument_layout to predef * track position and arity for Type_argument * test changes * more tests around typa argument arity * have Imported_type_argument as a creation_reason * test changes * add note about performance benchmark * extract position format logic into function * add comments about 1-indexed * change equals to is * test changes * define list_argument_layout * format comments --------- Co-authored-by: Alan Chang <[email protected]> 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]> Improve layout history error message: change message wording and add more tests (#1887) * change imported reason wording * test changes * lowercase * test changes * add more tests * change wording for match * update Immediate_polymorphic_variant message * test changes Improve layout history error message: hide layout history reason of various generalized vars (#1889) * add generalized creation reason * use change layout reason for value decl * change layout reason for decl type params * test changes * additional tests * add "the" * test changes * improve generalize on type decl * test changes * update err msg test case * add print_loc_in_lowercase * test changes * update_decls_layout_reason code cleanup Improve layout history error message: better test coverage (#1924) * Annotation * concrete layout * value layout * immediate layout * float64 layout * the rest * fix probe test * fix tests * more changes * more value test * add note * remove unused With_constraint constructor * fix format * remove unused void_creation_reason * remove unused Value_kind * add Missing_cmi test * consistent names for Type_variable&Univar * add param to Unannotated_type_parameter * add CRs * fix wording * add warning to all reasons we can't produce * more CRs * fix formatting * add new tests * enhance creation reason when missing cmi * fix Unannotated_type_parameter * test changes * code formatting (only) * Record missing cmi in the layout history * Add a CR * add broken test * fix is_missing_cmi check --------- Co-authored-by: Richard Eisenberg <[email protected]> Support for [@error_message] attribute on layout annotations (#1943) support for error_message attr on layout annots add error_message to the builtin_attrs table change error_message_attr to return first refactor - layout_of_annotation_or_attr to layout_of_annotation - Err_msg_attr to With_error_message fix tests improve formatting Support for [@error_message] attribute on Pexp_constraint (#1954) * implement error_message on Pexp_constraint * add tests * force message to be on new line * fix typo Improve layout history error message: omit history for signature mismatch (#2089) strip away layout history for signature mismatch Layout history: mark `typing-immediate/immediate.ml` as reviewed (#2144) mark test file as reviewed Improve layout history error message: review new messages (#2091) * remove layouts v2.9 CRs * "must be representable" instead of var name * catch missing history generalization * add history to Bad_univar_layout * remove crs * Toplevel_nonvalue refer to types * change Non_value_in_sig to refer to types * fix typo of Polymorphic * type argument to a class constructor * change message of Not_a_value * remove stale test file * refer to types in all messages * update unannotated universal variable message * split into Class_type_argument&Class_term_argument * add comment on update_generalized_ty_layout_reason * undo change to Not_a_value * update Not_a_value message fix merge conflicts Layout histories: resolve rebase conflicts (#2197) * get things to build * test changes * remove unused jkind reason * fix formatting * fix rebase conflicts * fix format
1 parent f876877 commit 0df692c

File tree

80 files changed

+4090
-798
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+4090
-798
lines changed

lambda/matching.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4151,7 +4151,7 @@ let report_error ppf = function
41514151
fprintf ppf
41524152
"Non-value detected in translation:@ Please report this error to \
41534153
the Jane Street compilers team.@ %a"
4154-
(Jkind.Violation.report_with_name ~name:"This expression") err
4154+
(Jkind.Violation.report_with_name ~name:"this expression") err
41554155
| Illegal_record_field c ->
41564156
fprintf ppf
41574157
"Sort %s detected where value was expected in a record field:@ Please \

parsing/builtin_attributes.ml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ let builtin_attrs =
105105
; "builtin"; "ocaml.builtin"
106106
; "no_effects"; "ocaml.no_effects"
107107
; "no_coeffects"; "ocaml.no_coeffects"
108-
; "only_generative_effects"; "ocaml.only_generative_effects";
108+
; "only_generative_effects"; "ocaml.only_generative_effects"
109+
; "error_message"; "ocaml.error_message"
109110
]
110111

111112
(* nroberts: When we upstream the builtin-attribute whitelisting, we shouldn't
@@ -696,3 +697,18 @@ let tailcall attr =
696697
(Warnings.Attribute_payload
697698
(t.attr_name.txt, "Only 'hint' is supported"));
698699
Ok (Some `Tail_if_possible)
700+
701+
let error_message_attr l =
702+
let inner x =
703+
match x.attr_name.txt with
704+
| "ocaml.error_message"|"error_message" ->
705+
begin match string_of_payload x.attr_payload with
706+
| Some _ as r ->
707+
mark_used x.attr_name;
708+
r
709+
| None -> warn_payload x.attr_loc x.attr_name.txt
710+
"error_message attribute expects a string argument";
711+
None
712+
end
713+
| _ -> None in
714+
List.find_map inner l

parsing/builtin_attributes.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,10 @@ val jkind_attribute_of_string : string -> jkind_attribute option
208208
as the attribute mechanism predates layouts.
209209
*)
210210
val jkind : Parsetree.attributes -> jkind_attribute Location.loc option
211+
212+
(** Finds the first "error_message" attribute, marks it as used, and returns its
213+
string payload. Returns [None] if no such attribute is present.
214+
215+
There should be at most one "error_message" attribute, additional ones are sliently
216+
ignored. **)
217+
val error_message_attr : Parsetree.attributes -> string option

parsing/location.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ let print_filename ppf file =
261261
Some of the information (filename, line number or characters numbers) in the
262262
location might be invalid; in which case we do not print it.
263263
*)
264-
let print_loc ppf loc =
264+
let print_loc ~capitalize_first ppf loc =
265265
setup_colors ();
266266
let file_valid = function
267267
| "_none_" ->
@@ -287,7 +287,8 @@ let print_loc ppf loc =
287287

288288
let first = ref true in
289289
let capitalize s =
290-
if !first then (first := false; String.capitalize_ascii s)
290+
if !first then (first := false;
291+
if capitalize_first then String.capitalize_ascii s else s)
291292
else s in
292293
let comma () =
293294
if !first then () else Format.fprintf ppf ", " in
@@ -316,6 +317,9 @@ let print_loc ppf loc =
316317

317318
Format.fprintf ppf "@}"
318319

320+
let print_loc_in_lowercase = print_loc ~capitalize_first:false
321+
let print_loc = print_loc ~capitalize_first:true
322+
319323
(* Print a comma-separated list of locations *)
320324
let print_locs ppf locs =
321325
Format.pp_print_list ~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ ")

parsing/location.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ val show_filename: string -> string
202202
val print_filename: formatter -> string -> unit
203203

204204
val print_loc: formatter -> t -> unit
205+
val print_loc_in_lowercase: formatter -> t -> unit
205206
val print_locs: formatter -> t list -> unit
206207

207208

testsuite/tests/typing-immediate/immediate.ml

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,14 @@ end;;
143143
Line 2, characters 2-31:
144144
2 | type t = string [@@immediate]
145145
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146-
Error: Type string has layout value, which is not a sublayout of immediate.
146+
Error: The layout of type string is value, because
147+
it is the primitive value type string.
148+
But the layout of type string must be a sublayout of immediate, because
149+
of the definition of t at line 2, characters 2-31.
147150
|}];;
151+
(* CR layouts v2.9: The "of the definition of t ..." part is not great and it
152+
should only refer to definitions that type check. Fixing it will involve
153+
building a second [final_env] in [transl_type_decl] which is costly. *)
148154

149155
(* Cannot directly declare a non-immediate type as immediate (variant) *)
150156
module B = struct
@@ -154,7 +160,10 @@ end;;
154160
Line 2, characters 2-41:
155161
2 | type t = Foo of int | Bar [@@immediate]
156162
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
157-
Error: Type t has layout value, which is not a sublayout of immediate.
163+
Error: The layout of type t is value, because
164+
it's a boxed variant type.
165+
But the layout of type t must be a sublayout of immediate, because
166+
of the annotation on the declaration of the type t.
158167
|}];;
159168

160169
(* Cannot directly declare a non-immediate type as immediate (record) *)
@@ -165,7 +174,10 @@ end;;
165174
Line 2, characters 2-38:
166175
2 | type t = { foo : int } [@@immediate]
167176
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168-
Error: Type t has layout value, which is not a sublayout of immediate.
177+
Error: The layout of type t is value, because
178+
it's a boxed record type.
179+
But the layout of type t must be a sublayout of immediate, because
180+
of the annotation on the declaration of the type t.
169181
|}];;
170182

171183
(* Not guaranteed that t is immediate, so this is an invalid declaration *)
@@ -177,7 +189,10 @@ end;;
177189
Line 3, characters 2-26:
178190
3 | type s = t [@@immediate]
179191
^^^^^^^^^^^^^^^^^^^^^^^^
180-
Error: Type t has layout value, which is not a sublayout of immediate.
192+
Error: The layout of type t is value, because
193+
of the definition of t at line 2, characters 2-8.
194+
But the layout of type t must be a sublayout of immediate, because
195+
of the definition of s at line 3, characters 2-26.
181196
|}];;
182197

183198
(* Can't ascribe to an immediate type signature with a non-immediate type *)
@@ -198,12 +213,14 @@ Error: Signature mismatch:
198213
type t = string
199214
is not included in
200215
type t : immediate
201-
the first has layout value, which is not a sublayout of immediate.
216+
The layout of the first is value, because
217+
it is the primitive value type string.
218+
But the layout of the first must be a sublayout of immediate, because
219+
of the definition of t at line 1, characters 15-35.
202220
|}];;
203221

204222
(* Same as above but with explicit signature *)
205223
module M_invalid : S = struct type t = string end;;
206-
module FM_invalid = F (struct type t = string end);;
207224
[%%expect{|
208225
Line 1, characters 23-49:
209226
1 | module M_invalid : S = struct type t = string end;;
@@ -214,7 +231,27 @@ Error: Signature mismatch:
214231
type t = string
215232
is not included in
216233
type t : immediate
217-
the first has layout value, which is not a sublayout of immediate.
234+
The layout of the first is value, because
235+
it is the primitive value type string.
236+
But the layout of the first must be a sublayout of immediate, because
237+
of the definition of t at line 1, characters 20-40.
238+
|}];;
239+
240+
module FM_invalid = F (struct type t = string end);;
241+
[%%expect{|
242+
Line 1, characters 20-50:
243+
1 | module FM_invalid = F (struct type t = string end);;
244+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
245+
Error: Modules do not match: sig type t = string end is not included in
246+
S
247+
Type declarations do not match:
248+
type t = string
249+
is not included in
250+
type t : immediate
251+
The layout of the first is value, because
252+
it is the primitive value type string.
253+
But the layout of the first must be a sublayout of immediate, because
254+
of the definition of t at line 1, characters 20-40.
218255
|}];;
219256

220257
(* Can't use a non-immediate type even if mutually recursive *)
@@ -226,7 +263,10 @@ end;;
226263
Line 2, characters 2-26:
227264
2 | type t = s [@@immediate]
228265
^^^^^^^^^^^^^^^^^^^^^^^^
229-
Error: Type s has layout value, which is not a sublayout of immediate.
266+
Error: The layout of type s is value, because
267+
it is the primitive value type string.
268+
But the layout of type s must be a sublayout of immediate, because
269+
of the definition of t at line 2, characters 2-26.
230270
|}];;
231271

232272

0 commit comments

Comments
 (0)