Skip to content
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

Restore short form formatting of record field aliases #2282

Merged
merged 2 commits into from
Mar 9, 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
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
### Changes

- JaneStreet profile: add extra parens around tuple with type annotation (#2281, @gpetiot)
- Module patterns with a module type constraint (`((module M) : (module S))`) are formatted using the short syntax (`(module M : S)`) (#2280, @gpetiot)
- Restore short form formatting of module patterns with a module type constraint (`((module M) : (module S))` formatted as `(module M : S)`) (#2280, @gpetiot)
- Restore short form formatting of record field aliases (#2282, @gpetiot)

### New features

Expand Down
76 changes: 68 additions & 8 deletions lib/Extended_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,78 @@ module Parse = struct
let open Asttypes in
let open Ast_mapper in
let record_field m (f, t, v) =
match v with
| Some {pexp_desc= Pexp_ident {txt= v_txt; _}; pexp_attributes= []; _}
match (t, v) with
(* [{ x = x }] -> [{ x }] *)
| ( _
, Some {pexp_desc= Pexp_ident {txt= v_txt; _}; pexp_attributes= []; _}
)
when Std_longident.field_alias ~field:f.txt v_txt ->
(f, t, None)
| v -> (f, t, Option.map ~f:(m.expr m) v)
(* [{ x = (x : t) }] -> [{ x : t }] *)
(* [{ x :> t = (x : t) }] -> [{ x : t :> t }] *)
| ( (None, t2)
, Some
{ pexp_desc=
Pexp_constraint
( { pexp_desc= Pexp_ident {txt= v_txt; _}
; pexp_attributes= []
; _ }
, t1 )
; pexp_attributes= []
; _ } )
when Std_longident.field_alias ~field:f.txt v_txt ->
(f, (Some t1, t2), None)
(* [{ x = (x :> t) }] -> [{ x :> t }] *)
(* [{ x = (x : t :> t) }] -> [{ x : t :> t }] *)
| ( (None, None)
, Some
{ pexp_desc=
Pexp_coerce
( { pexp_desc= Pexp_ident {txt= v_txt; _}
; pexp_attributes= []
; _ }
, t1
, t2 )
; pexp_attributes= []
; _ } )
when Std_longident.field_alias ~field:f.txt v_txt ->
(f, (t1, Some t2), None)
(* [{ x : t = (x :> t) }] -> [{ x : t :> t }] *)
| ( (Some t1, None)
, Some
{ pexp_desc=
Pexp_coerce
( { pexp_desc= Pexp_ident {txt= v_txt; _}
; pexp_attributes= []
; _ }
, None
, t2 )
; pexp_attributes= []
; _ } )
when Std_longident.field_alias ~field:f.txt v_txt ->
(f, (Some t1, Some t2), None)
| _ -> (f, t, Option.map ~f:(m.expr m) v)
in
let pat_record_field m (f, t, v) =
match v with
| Some {ppat_desc= Ppat_var {txt= v_txt; _}; ppat_attributes= []; _}
match (t, v) with
(* [{ x = x }] -> [{ x }] *)
| _, Some {ppat_desc= Ppat_var {txt= v_txt; _}; ppat_attributes= []; _}
when Std_longident.field_alias ~field:f.txt (Lident v_txt) ->
(f, t, None)
| v -> (f, t, Option.map ~f:(m.pat m) v)
(* [{ x = (x : t) }] -> [{ x : t}] *)
| ( None
, Some
{ ppat_desc=
Ppat_constraint
( { ppat_desc= Ppat_var {txt= v_txt; _}
; ppat_attributes= []
; _ }
, t )
; ppat_attributes= []
; _ } )
when Std_longident.field_alias ~field:f.txt (Lident v_txt) ->
(f, Some t, None)
| _ -> (f, t, Option.map ~f:(m.pat m) v)
in
let binding_op (m : Ast_mapper.mapper) b =
let b' =
Expand All @@ -78,7 +138,7 @@ module Parse = struct
| _ -> false ->
let pats = List.(rev (tl_exn (rev l))) in
{p with ppat_desc= Ppat_list pats}
(* [{ x = x }] -> [{ x }] *)
(* Field alias shorthand *)
| {ppat_desc= Ppat_record (fields, flag); _} as e ->
let fields = List.map ~f:(pat_record_field m) fields in
{e with ppat_desc= Ppat_record (fields, flag)}
Expand All @@ -105,7 +165,7 @@ module Parse = struct
| {pexp_desc= Pexp_beginend e'; pexp_attributes= []; _}
when not preserve_beginend ->
m.expr m e'
(* [{ x = x }] -> [{ x }] *)
(* Field alias shorthand *)
| {pexp_desc= Pexp_record (fields, with_); _} as e ->
let fields = List.map ~f:(record_field m) fields in
{ e with
Expand Down
4 changes: 2 additions & 2 deletions test/passing/tests/js_syntax.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
let _ =
[%raise_structural_sexp
"feature's tip is already an ancestor of new base"
{feature_tip= (old_tip : Rev.t); new_base= (new_base : Rev.t)}]
{feature_tip= (old_tip : Rev.t); new_base: Rev.t}]

let _ =
[%raise_structural_sexp
"feature's tip is already an ancestor of new base"
{feature_tip= (old_tip : Rev.t); new_base= (new_base : Rev.t)}]
{feature_tip= (old_tip : Rev.t); new_base: Rev.t}]
12 changes: 12 additions & 0 deletions test/passing/tests/record-loose.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ let _ = {(*a*) x (*b*) : (*c*) t (*d*) :> (*e*) t (*f*) = (*g*) e (*h*)}
type t = C of (*a*) {(*b*) x (*c*) : (*d*) t (*e*)} (*f*)

type t = C : (*a*) {(*b*) x (*c*) : (*d*) t (*e*)} (*f*) -> t

let _ = {x : t}

let _ = {x : t :> t}

let _ = {x :> t}

let _ = {x : t :> t}

let _ = {x : t :> t}

let {x : t} = x
12 changes: 12 additions & 0 deletions test/passing/tests/record-tight_decl.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ let _ = {(*a*) x (*b*) : (*c*) t (*d*) :> (*e*) t (*f*) = (*g*) e (*h*)}
type t = C of (*a*) {(*b*) x (*c*): (*d*) t (*e*)} (*f*)

type t = C : (*a*) {(*b*) x (*c*): (*d*) t (*e*)} (*f*) -> t

let _ = {x : t}

let _ = {x : t :> t}

let _ = {x :> t}

let _ = {x : t :> t}

let _ = {x : t :> t}

let {x : t} = x
8 changes: 8 additions & 0 deletions test/passing/tests/record.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,11 @@ let _ = { (*a*)x(*b*) : (*c*)t(*d*) :> (*e*)t(*f*) = (*g*)e(*h*) }
type t = C of (*a*) { (*b*) x (*c*) : (*d*) t (*e*)} (*f*)

type t = C : (*a*) { (*b*) x (*c*) : (*d*) t (*e*)} (*f*) -> t

let _ = { x = (x : t) }
let _ = { x :> t = (x : t) }
let _ = { x = (x :> t) }
let _ = { x = (x : t :> t) }
let _ = { x : t = (x :> t) }

let { x = (x : t) } = x
12 changes: 12 additions & 0 deletions test/passing/tests/record.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ let _ = {(*a*) x (*b*): (*c*) t (*d*) :> (*e*) t (*f*) = (*g*) e (*h*)}
type t = C of (*a*) {(*b*) x (*c*): (*d*) t (*e*)} (*f*)

type t = C : (*a*) {(*b*) x (*c*): (*d*) t (*e*)} (*f*) -> t

let _ = {x: t}

let _ = {x: t :> t}

let _ = {x:> t}

let _ = {x: t :> t}

let _ = {x: t :> t}

let {x: t} = x