Skip to content

Commit

Permalink
Rename layout to jkind in most places (#69)
Browse files Browse the repository at this point in the history
* Rename `layout` to `jkind` in most places

Signed-off-by: Charlie Gunn <cgunn@janestreet.com>

* formatting

Signed-off-by: Charlie Gunn <cgunn@janestreet.com>

* It's still CR layouts; that's the thing we search for

---------

Signed-off-by: Charlie Gunn <cgunn@janestreet.com>
Co-authored-by: Charlie Gunn <cgunn@janestreet.com>
Co-authored-by: Richard Eisenberg <reisenberg@janestreet.com>
  • Loading branch information
3 people authored May 28, 2024
1 parent d6a910c commit 3a23ed2
Show file tree
Hide file tree
Showing 33 changed files with 1,077 additions and 497 deletions.
23 changes: 11 additions & 12 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -538,17 +538,16 @@ let fmt_quoted_string key ext s = function
(str (Format_.sprintf "|%s}" delim))
(str s)

let type_var_has_layout_annot (_, layout_opt) = Option.is_some layout_opt
let type_var_has_jkind_annot (_, jkind_opt) = Option.is_some jkind_opt

let layout_to_string = function Layout s -> s
let jkind_to_string = function Layout s -> s

let fmt_layout_str ~c ~loc string =
fmt "@ :@ " $ Cmts.fmt c loc @@ str string
let fmt_jkind_str ~c ~loc string = fmt "@ :@ " $ Cmts.fmt c loc @@ str string

let fmt_layout c l = fmt_layout_str ~c ~loc:l.loc (layout_to_string l.txt)
let fmt_jkind c l = fmt_jkind_str ~c ~loc:l.loc (jkind_to_string l.txt)

let fmt_type_var ~have_tick c s =
let {txt= name_opt; loc= name_loc}, layout_opt = s in
let {txt= name_opt; loc= name_loc}, jkind_opt = s in
( Cmts.fmt c name_loc
@@
match name_opt with
Expand All @@ -562,10 +561,10 @@ let fmt_type_var ~have_tick c s =
(String.length var_name > 1 && Char.equal var_name.[1] '\'')
" " )
$ str var_name )
$ Option.value_map layout_opt ~default:noop ~f:(fmt_layout c)
$ Option.value_map jkind_opt ~default:noop ~f:(fmt_jkind c)

let fmt_type_var_with_parenze ~have_tick c s =
wrap_if (type_var_has_layout_annot s) "(" ")" (fmt_type_var ~have_tick c s)
wrap_if (type_var_has_jkind_annot s) "(" ")" (fmt_type_var ~have_tick c s)

let split_global_flags_from_attrs atrs =
match
Expand Down Expand Up @@ -3534,7 +3533,7 @@ and fmt_tydcl_params c ctx params =
| [(p, _)] ->
( false
, match p.ptyp_desc with
| Ptyp_var s -> type_var_has_layout_annot s
| Ptyp_var s -> type_var_has_jkind_annot s
| _ -> false )
| _ :: _ :: _ -> (false, true)
in
Expand Down Expand Up @@ -3563,7 +3562,7 @@ and fmt_type_declaration c ?ext ?(pre = "") ?name ?(eq = "=") {ast= decl; _}
@@
let decl =
if Erase_jane_syntax.should_erase () then decl
else Sugar.rewrite_type_declaration_imm_attr_to_layout_annot c.cmts decl
else Sugar.rewrite_type_declaration_imm_attr_to_jkind_annot c.cmts decl
in
let { ptype_name= {txt; loc}
; ptype_params
Expand All @@ -3573,7 +3572,7 @@ and fmt_type_declaration c ?ext ?(pre = "") ?name ?(eq = "=") {ast= decl; _}
; ptype_manifest= m
; ptype_attributes
; ptype_loc
; ptype_layout } =
; ptype_jkind } =
decl
in
update_config_maybe_disabled c ptype_loc ptype_attributes
Expand Down Expand Up @@ -3602,7 +3601,7 @@ and fmt_type_declaration c ?ext ?(pre = "") ?name ?(eq = "=") {ast= decl; _}
0
( fmt_tydcl_params c ctx ptype_params
$ Option.value_map name ~default:(str txt) ~f:(fmt_longident_loc c)
$ fmt_opt (Option.map ~f:(fmt_layout c) ptype_layout) )
$ fmt_opt (Option.map ~f:(fmt_jkind c) ptype_jkind) )
$ k )
in
let fmt_manifest_kind =
Expand Down
45 changes: 31 additions & 14 deletions lib/Normalize_std_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let is_erasable_jane_syntax attr =
"erasable jane syntax" *)
|| String.equal "extension.curry" name

(* Immediate layout annotations should be treated the same as their attribute
(* Immediate jkind annotations should be treated the same as their attribute
counterparts *)
let normalize_immediate_annot_and_attrs attrs =
let overwrite_attr_name attr new_name =
Expand All @@ -39,8 +39,8 @@ let normalize_immediate_annot_and_attrs attrs =
in
let attrs, _ =
List.fold attrs ~init:([], false)
~f:(fun (new_attrs, deleted_layout_annot) attr ->
let new_attr, just_deleted_layout_annot =
~f:(fun (new_attrs, deleted_jkind_annot) attr ->
let new_attr, just_deleted_jkind_annot =
match (attr.attr_name.txt, attr.attr_payload) with
(* We also have to normalize "ocaml.immediate" into "immediate" for
this to work. Since if we rewrite [@@ocaml.immediate] into an
Expand All @@ -49,27 +49,44 @@ let normalize_immediate_annot_and_attrs attrs =
| ( "jane.erasable.layouts.annot"
, PStr
[ { pstr_desc=
Pstr_eval
( { pexp_desc= Pexp_ident {txt= Lident "immediate"; _}
; _ }
, _ )
Pstr_attribute
{ attr_name= {txt= "jane.erasable.layouts.prim"; _}
; attr_payload=
PStr
[ { pstr_desc=
Pstr_eval
( { pexp_desc=
Pexp_ident
{txt= Lident "immediate"; _}
; _ }
, _ )
; _ } ]
; _ }
; _ } ] ) ->
(Some (overwrite_attr_name attr "immediate"), true)
| "ocaml.immediate", PStr [] ->
(Some (overwrite_attr_name attr "immediate"), false)
| ( "jane.erasable.layouts.annot"
, PStr
[ { pstr_desc=
Pstr_eval
( { pexp_desc=
Pexp_ident {txt= Lident "immediate64"; _}
; _ }
, _ )
Pstr_attribute
{ attr_name= {txt= "jane.erasable.layouts.prim"; _}
; attr_payload=
PStr
[ { pstr_desc=
Pstr_eval
( { pexp_desc=
Pexp_ident
{txt= Lident "immediate64"; _}
; _ }
, _ )
; _ } ]
; _ }
; _ } ] ) ->
(Some (overwrite_attr_name attr "immediate64"), true)
| "ocaml.immediate64", PStr [] ->
(Some (overwrite_attr_name attr "immediate64"), false)
| "jane.erasable.layouts", PStr [] when deleted_layout_annot ->
| "jane.erasable.layouts", PStr [] when deleted_jkind_annot ->
(* Only remove [jane.erasable.layouts] if we previously rewrote
an associated [jane.erasable.layouts.annot] *)
(None, false)
Expand All @@ -80,7 +97,7 @@ let normalize_immediate_annot_and_attrs attrs =
| Some new_attr -> new_attr :: new_attrs
| None -> new_attrs
in
(new_attrs, deleted_layout_annot || just_deleted_layout_annot) )
(new_attrs, deleted_jkind_annot || just_deleted_jkind_annot) )
in
List.rev attrs

Expand Down
18 changes: 9 additions & 9 deletions lib/Sugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,31 @@ let remove_local_attrs cmts param =
in
Pparam_val (is_local, label, default, {pattern with ppat_attributes})

let get_layout_of_legacy_attr attr =
let get_jkind_of_legacy_attr attr =
match (attr.attr_name.txt, attr.attr_payload) with
| ("ocaml.immediate64" | "immediate64"), PStr [] ->
Some (Layout "immediate64")
| ("ocaml.immediate" | "immediate"), PStr [] -> Some (Layout "immediate")
| _ -> None

let rewrite_type_declaration_imm_attr_to_layout_annot cmts decl =
let rewrite_type_declaration_imm_attr_to_jkind_annot cmts decl =
let immediate_attrs, remaining_attrs =
decl.ptype_attributes
|> List.partition_map ~f:(fun attr ->
match get_layout_of_legacy_attr attr with
| Some layout -> First (layout, attr)
match get_jkind_of_legacy_attr attr with
| Some jkind -> First (jkind, attr)
| None -> Second attr )
in
match (decl.ptype_layout, immediate_attrs) with
| None, [(layout, attr)] ->
(* We only do this rewrite if (1.) there's no layout annotation already
match (decl.ptype_jkind, immediate_attrs) with
| None, [(jkind, attr)] ->
(* We only do this rewrite if (1.) there's no jkind annotation already
present and (2.) only one immediate attribute is attached *)
let ptype_layout = Some Location.(mknoloc layout) in
let ptype_jkind = Some Location.(mknoloc jkind) in
Cmts.relocate_all_to_before cmts ~src:attr.attr_name.loc
~before:decl.ptype_loc ;
Cmts.relocate_all_to_before cmts ~src:attr.attr_loc
~before:decl.ptype_loc ;
{decl with ptype_attributes= remaining_attrs; ptype_layout}
{decl with ptype_attributes= remaining_attrs; ptype_jkind}
| _ -> decl

module Exp = struct
Expand Down
4 changes: 2 additions & 2 deletions lib/Sugar.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ val cl_fun :

val remove_local_attrs : Cmts.t -> function_param_desc -> function_param_desc

val rewrite_type_declaration_imm_attr_to_layout_annot :
val rewrite_type_declaration_imm_attr_to_jkind_annot :
Cmts.t -> type_declaration -> type_declaration
(** Rewrites [@@immediate] to [_ : immediate] and do the same for [@@immediate64].
This only happens if there's no existing layout annotation AND there's only
This only happens if there's no existing jkind annotation AND there's only
one immediacy attribute. *)

module Exp : sig
Expand Down
11 changes: 5 additions & 6 deletions vendor/ocaml-common/warnings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type t =
| Unerasable_position_argument (* 188 *)
| Unnecessarily_partial_tuple_pattern (* 189 *)
| Probe_name_too_long of string (* 190 *)
| Unchecked_property_attribute of string (* 199 *)
| Unchecked_zero_alloc_attribute (* 199 *)
| Unboxing_impossible (* 210 *)
| Redundant_modality of string (* 250 *)

Expand Down Expand Up @@ -199,7 +199,7 @@ let number = function
| Unerasable_position_argument -> 188
| Unnecessarily_partial_tuple_pattern -> 189
| Probe_name_too_long _ -> 190
| Unchecked_property_attribute _ -> 199
| Unchecked_zero_alloc_attribute -> 199
| Unboxing_impossible -> 210
| Redundant_modality _ -> 250
;;
Expand Down Expand Up @@ -561,7 +561,7 @@ let descriptions = [
description = "Probe name must be at most 100 characters long.";
since = since 4 14 };
{ number = 199;
names = ["unchecked-property-attribute"];
names = ["unchecked-zero-alloc-attribute"];
description = "A property of a function that was \
optimized away cannot be checked.";
since = since 4 14 };
Expand Down Expand Up @@ -1189,12 +1189,11 @@ let message = function
Printf.sprintf
"This probe name is too long: `%s'. \
Probe names must be at most 100 characters long." name
| Unchecked_property_attribute property ->
Printf.sprintf "the %S attribute cannot be checked.\n\
| Unchecked_zero_alloc_attribute ->
Printf.sprintf "the zero_alloc attribute cannot be checked.\n\
The function it is attached to was optimized away. \n\
You can try to mark this function as [@inline never] \n\
or move the attribute to the relevant callers of this function."
property
| Unboxing_impossible ->
Printf.sprintf
"This [@unboxed] attribute cannot be used.\n\
Expand Down
2 changes: 1 addition & 1 deletion vendor/ocaml-common/warnings.mli
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type t =
| Unerasable_position_argument (* 188 *)
| Unnecessarily_partial_tuple_pattern (* 189 *)
| Probe_name_too_long of string (* 190 *)
| Unchecked_property_attribute of string (* 199 *)
| Unchecked_zero_alloc_attribute (* 199 *)
| Unboxing_impossible (* 210 *)
| Redundant_modality of string (* 250 *)

Expand Down
4 changes: 2 additions & 2 deletions vendor/parser-extended/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ module Type = struct
let mk ?(loc = !default_loc) ?(attrs = [])
?(docs = empty_docs) ?(text = [])
?(params = [])
?layout
?jkind
?(cstrs = [])
?(kind = Ptype_abstract)
?(priv = Public)
Expand All @@ -492,7 +492,7 @@ module Type = struct
ptype_attributes =
add_text_attrs text (add_docs_attrs docs attrs);
ptype_loc = loc;
ptype_layout = layout;
ptype_jkind = jkind;
}

let constructor ?(loc = !default_loc) ?(attrs = []) ?(info = empty_info)
Expand Down
4 changes: 2 additions & 2 deletions vendor/parser-extended/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ module T = struct
ptype_manifest;
ptype_attributes;
ptype_loc;
ptype_layout;
ptype_jkind;
} =
let loc = sub.location sub ptype_loc in
let attrs = sub.attributes sub ptype_attributes in
Type.mk ~loc ~attrs (map_loc sub ptype_name)
?layout:(ptype_layout)
?jkind:(ptype_jkind)
~params:(List.map (map_fst (sub.typ sub)) ptype_params)
~priv:(Flag.map_private sub ptype_private)
~cstrs:(List.map
Expand Down
6 changes: 3 additions & 3 deletions vendor/parser-extended/asttypes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ type 'a loc = 'a Location.loc = {
loc : Location.t;
}

type const_layout = Layout of string [@@unboxed]
type const_jkind = Layout of string [@@unboxed]

type layout_annotation = const_layout loc
type ty_var = string option loc * layout_annotation option
type jkind_annotation = const_jkind loc
type ty_var = string option loc * jkind_annotation option

type label = string

Expand Down
Loading

0 comments on commit 3a23ed2

Please sign in to comment.