Skip to content

Commit

Permalink
Hackily sort withs on a kind in outcometree
Browse files Browse the repository at this point in the history
we need to sort types in a [with] because the order comes from iterating a
TypeHash, which is nondeterministic (specifically, the order is different if we
disable stack allocation) and brittle. Unfortunately, we lack a good comparison
for types, so as a hacky workaround here we format to a string (actually, a
string list for easier line breaking when pretty printing) and sort
lexicographically before finally printing.
  • Loading branch information
glittershark committed Dec 30, 2024
1 parent a830b3a commit 56f9967
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
7 changes: 4 additions & 3 deletions testsuite/tests/parsetree/source_jane_street.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1070,9 +1070,10 @@ type 'a list : value mod many uncontended portable with 'a
type ('a, 'b) either : value mod many uncontended portable with 'a * 'b
type 'a contended : value mod many uncontended portable with 'a @@ contended
type 'a contended_with_int
: value mod many uncontended portable with 'a
@@
contended with int
: value mod many uncontended portable
with 'a @@ contended
with int
|}]

(* not yet supported *)
Expand Down
43 changes: 36 additions & 7 deletions typing/oprint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,28 @@ and print_out_label ppf (name, mut, arg, gbl) =

and print_out_jkind_const ppf ojkind =
let rec pp_element ~nested ppf (ojkind : Outcometree.out_jkind_const) =
match ojkind with
(* HACK: we strip off the [Ojkind_const_with]s and convert them to a [string string
list] so we can sort them lexicographically, because otherwise the order of printed
[with]s is nondeterministic. This is sad, but we'd need deterministic sorting of
types to work around it.
CR aspsmith: remove this if we ever add deterministic, semantic type comparison
*)
let rec strip_withs ojkind =
match ojkind with
| Ojkind_const_with (base, ty, modalities) ->
let base, withs = strip_withs base in
let with_ =
Format.asprintf "%a" print_out_type ty
:: (match modalities with
| [] -> []
| modalities -> "@@" :: modalities)
in
base, with_ :: withs
| base -> base, []
in
let base, withs = strip_withs ojkind in
(match base with
| Ojkind_const_default -> fprintf ppf "_"
| Ojkind_const_abbreviation abbrev -> fprintf ppf "%s" abbrev
| Ojkind_const_mod (base, modes) ->
Expand All @@ -631,13 +652,21 @@ and print_out_jkind_const ppf ojkind =
| Ojkind_const_product ts ->
let pp_sep ppf () = Format.fprintf ppf "@ & " in
Misc.pp_nested_list ~nested ~pp_element ~pp_sep ppf ts
| Ojkind_const_with (base, ty, modalities) ->
fprintf ppf "%a with @[%a@]%a"
(pp_element ~nested:false) base
print_out_type ty
print_out_modalities_new modalities
| Ojkind_const_with _ -> failwith "XXX unreachable (stripped off earlier)"
| Ojkind_const_kind_of _ ->
failwith "XXX unimplemented jkind syntax"
failwith "XXX unimplemented jkind syntax");
let withs = List.sort (List.compare String.compare) withs in
match withs with
| [] -> ()
| withs ->
pp_print_list
(fun ppf ->
Format.fprintf ppf "@ @[with %a@]"
(pp_print_list
~pp_sep:(fun ppf () -> fprintf ppf " ")
(fun ppf -> Format.fprintf ppf "%s")))
ppf
withs
in
pp_element ~nested:false ppf ojkind

Expand Down

0 comments on commit 56f9967

Please sign in to comment.