From 9a5c876c7d721f9af98f78a5e4652e71acbdf22a Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sat, 7 Sep 2024 09:19:58 +0800 Subject: [PATCH 1/7] WIP --- jscomp/syntax/src/res_parsetree_viewer.ml | 25 ++++++++++++++++++++++ jscomp/syntax/src/res_parsetree_viewer.mli | 2 ++ jscomp/syntax/src/res_printer.ml | 3 ++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/jscomp/syntax/src/res_parsetree_viewer.ml b/jscomp/syntax/src/res_parsetree_viewer.ml index 20478bb559..1f7a1b18ef 100644 --- a/jscomp/syntax/src/res_parsetree_viewer.ml +++ b/jscomp/syntax/src/res_parsetree_viewer.ml @@ -1,5 +1,30 @@ open Parsetree +let attrs_string attrs = + List.map (fun (attr, _) -> print_endline attr.Asttypes.txt) attrs + +let ident_string ident = + match ident with + | Longident.Lident v -> v + | Longident.Ldot (_, v) -> v + | Longident.Lapply _ -> "Lapply" + +let print_ct ct = + match ct with + | {ptyp_desc = Ptyp_constr (ident, _); ptyp_attributes = attrs} -> + let _ = + print_endline + ("Ptyp_constr: " ^ ident_string ident.txt ^ " attrs: " + ^ string_of_int (List.length attrs)) + in + let _ = attrs_string attrs in + () + | {ptyp_desc = Ptyp_arrow _; ptyp_attributes = attrs} -> + print_endline + ("Ptyp_arrow: " ^ " attrs: " ^ string_of_int (List.length attrs)) + | {ptyp_desc = Ptyp_variant _} -> print_endline "Ptyp_variant" + | _ -> print_endline "Something else" + let arrow_type ?(arity = max_int) ct = let has_as_attr attrs = Ext_list.exists attrs (fun (x, _) -> x.Asttypes.txt = "as") diff --git a/jscomp/syntax/src/res_parsetree_viewer.mli b/jscomp/syntax/src/res_parsetree_viewer.mli index 286515e274..da49359a25 100644 --- a/jscomp/syntax/src/res_parsetree_viewer.mli +++ b/jscomp/syntax/src/res_parsetree_viewer.mli @@ -8,6 +8,8 @@ val arrow_type : * (Parsetree.attributes * Asttypes.arg_label * Parsetree.core_type) list * Parsetree.core_type +val print_ct : Parsetree.core_type -> unit + val functor_type : Parsetree.module_type -> (Parsetree.attributes * string Asttypes.loc * Parsetree.module_type option) diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index 2d250442c3..85875eb30c 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -1591,6 +1591,7 @@ and print_label_declaration ~state (ld : Parsetree.label_declaration) cmt_tbl = ]) and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl = + let parent_has_attrs = Ast_uncurried.core_type_is_uncurried_fun typ_expr && not (typ_expr.ptyp_attributes = []) in let print_arrow ?(arity = max_int) typ_expr = let attrs_before, args, return_type = ParsetreeViewer.arrow_type ~arity typ_expr @@ -1626,7 +1627,7 @@ and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl = [ Doc.group attrs; Doc.group - (if has_attrs_before then + (if has_attrs_before || parent_has_attrs then Doc.concat [ Doc.lparen; From 31228bfb650d807eeea77d833ebdcc18bfa36ad5 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sat, 7 Sep 2024 20:16:00 +0800 Subject: [PATCH 2/7] Check for existence of attr and arity Check for existence of attr at Ptyp_constr (function$) as the @this attribute is attached to the Ptyp_constr, not the Ptyp_arrow. --- jscomp/syntax/src/res_printer.ml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index 85875eb30c..1ac5d82b2c 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -1591,7 +1591,14 @@ and print_label_declaration ~state (ld : Parsetree.label_declaration) cmt_tbl = ]) and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl = - let parent_has_attrs = Ast_uncurried.core_type_is_uncurried_fun typ_expr && not (typ_expr.ptyp_attributes = []) in + let parent_has_attrs = + let attrs = ParsetreeViewer.filter_parsing_attrs typ_expr.ptyp_attributes in + if Ast_uncurried.core_type_is_uncurried_fun typ_expr && not (attrs = []) + then + let arity, _ = Ast_uncurried.core_type_extract_uncurried_fun typ_expr in + arity > 0 + else false + in let print_arrow ?(arity = max_int) typ_expr = let attrs_before, args, return_type = ParsetreeViewer.arrow_type ~arity typ_expr From 0e3c3c118ec986e6c003da0050a9065c5fbf6a6d Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sat, 7 Sep 2024 20:30:24 +0800 Subject: [PATCH 3/7] Another way to do it --- jscomp/syntax/src/res_parsetree_viewer.ml | 9 +++++---- jscomp/syntax/src/res_parsetree_viewer.mli | 1 + jscomp/syntax/src/res_printer.ml | 14 ++++++-------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/jscomp/syntax/src/res_parsetree_viewer.ml b/jscomp/syntax/src/res_parsetree_viewer.ml index 1f7a1b18ef..c54a4571da 100644 --- a/jscomp/syntax/src/res_parsetree_viewer.ml +++ b/jscomp/syntax/src/res_parsetree_viewer.ml @@ -25,7 +25,7 @@ let print_ct ct = | {ptyp_desc = Ptyp_variant _} -> print_endline "Ptyp_variant" | _ -> print_endline "Something else" -let arrow_type ?(arity = max_int) ct = +let arrow_type ?(arity = max_int) ?(attrs = []) ct = let has_as_attr attrs = Ext_list.exists attrs (fun (x, _) -> x.Asttypes.txt = "as") in @@ -70,10 +70,11 @@ let arrow_type ?(arity = max_int) ct = | typ -> (attrs_before, List.rev acc, typ) in match ct with - | {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs} as - typ -> + | {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs1} + as typ -> + let attrs = List.concat [attrs; attrs1] in process attrs [] {typ with ptyp_attributes = []} arity - | typ -> process [] [] typ arity + | typ -> process attrs [] typ arity let functor_type modtype = let rec process acc modtype = diff --git a/jscomp/syntax/src/res_parsetree_viewer.mli b/jscomp/syntax/src/res_parsetree_viewer.mli index da49359a25..a0bff3ee51 100644 --- a/jscomp/syntax/src/res_parsetree_viewer.mli +++ b/jscomp/syntax/src/res_parsetree_viewer.mli @@ -3,6 +3,7 @@ * we restructure the tree into (a, b, c) and its returnType d *) val arrow_type : ?arity:int -> + ?attrs:Parsetree.attributes -> Parsetree.core_type -> Parsetree.attributes * (Parsetree.attributes * Asttypes.arg_label * Parsetree.core_type) list diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index 1ac5d82b2c..e60a9ddd31 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -1591,17 +1591,13 @@ and print_label_declaration ~state (ld : Parsetree.label_declaration) cmt_tbl = ]) and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl = - let parent_has_attrs = + let parent_attrs = let attrs = ParsetreeViewer.filter_parsing_attrs typ_expr.ptyp_attributes in - if Ast_uncurried.core_type_is_uncurried_fun typ_expr && not (attrs = []) - then - let arity, _ = Ast_uncurried.core_type_extract_uncurried_fun typ_expr in - arity > 0 - else false + if Ast_uncurried.core_type_is_uncurried_fun typ_expr then attrs else [] in let print_arrow ?(arity = max_int) typ_expr = let attrs_before, args, return_type = - ParsetreeViewer.arrow_type ~arity typ_expr + ParsetreeViewer.arrow_type ~arity ~attrs:parent_attrs typ_expr in let return_type_needs_parens = match return_type.ptyp_desc with @@ -1634,7 +1630,7 @@ and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl = [ Doc.group attrs; Doc.group - (if has_attrs_before || parent_has_attrs then + (if has_attrs_before then Doc.concat [ Doc.lparen; @@ -1849,6 +1845,8 @@ and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl = in let should_print_its_own_attributes = match typ_expr.ptyp_desc with + | Ptyp_constr _ when Ast_uncurried.core_type_is_uncurried_fun typ_expr -> + true | Ptyp_arrow _ (* es6 arrow types print their own attributes *) -> true | _ -> false in From 82bfdb4ac7a43531022d667a72f82d2a23ca8ba8 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sun, 8 Sep 2024 14:54:38 +0800 Subject: [PATCH 4/7] Commit changes in test --- .../printer/typexpr/expected/arrow.res.txt | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/jscomp/syntax/tests/printer/typexpr/expected/arrow.res.txt b/jscomp/syntax/tests/printer/typexpr/expected/arrow.res.txt index 4acc006e6d..d350436648 100644 --- a/jscomp/syntax/tests/printer/typexpr/expected/arrow.res.txt +++ b/jscomp/syntax/tests/printer/typexpr/expected/arrow.res.txt @@ -125,13 +125,13 @@ type t = ( type t = (@attr string, @attr float) => unit type t = (@attr @attr2 string, @attr @attr2 float, @attr3 int) => unit -type t = @attr string => unit +type t = @attr (string => unit) type t = @attr (foo, bar, baz) => unit type t = @attr (foo, @attr2 ~f: bar, @attr3 ~f: baz) => unit -type t = @attr string => @attr int => unit +type t = @attr (string => @attr (int => unit)) type t = @attr (string, int) => @attr (int, float) => unit -type t = @attr int => @attr (int, float) => @attr unit => unit => unit +type t = @attr (int => @attr (int, float) => @attr (unit => unit => unit)) type t = @attr (@attr2 ~f: int, @attr3 ~g: float) => unit type f = ( @@ -144,50 +144,50 @@ type f = ( @attr3 ~h: ccccrazysldkfjslkdjflksdjkf=?, ) => unit -type t = @attr -stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr2 -floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr3 -intWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => unitWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong +type t = @attr ( + stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr2 ( + floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => @attr3 ( + intWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong => unitWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong + ) + ) +) -type t = @attr -( +type t = @attr ( fooWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, barWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, bazWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, -) => @attr2 -( +) => @attr2 ( stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, ) => unit type t = @attr @attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong -@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong -( +@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong ( fooWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, barWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, bazWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, ) => @attr2 @attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong -@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong -( +@attrWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong ( stringWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, floatWithSuperLongIdentifierNameLoooooooooooooooooooooooooooooooooooooooooooooong, ) => unit external debounce: (int, @meth unit) => unit = "debounce" -external debounce: int => @meth unit => unit = "debounce" +external debounce: int => @meth (unit => unit) = "debounce" -external debounce: (int, @meth unit => unit) => @meth unit => unit = "debounce" +external debounce: (int, @meth (unit => unit)) => @meth (unit => unit) = "debounce" -external debounce: (int, @meth unit => unit, @meth unit => unit) => @meth unit => unit = "debounce" +external debounce: (int, @meth (unit => unit), @meth (unit => unit)) => @meth (unit => unit) = + "debounce" external debounce: ( int, - @meth unit => unit, - @meth unit => @meth unit => unit, -) => @meth unit => unit = "debounce" + @meth (unit => unit), + @meth (unit => @meth (unit => unit)), +) => @meth (unit => unit) = "debounce" type returnTyp = (int, int) => @magic float type returnTyp = ( @@ -214,7 +214,7 @@ type t = (@attrOnInt int, @attrOnInt int, @attrOnInt int, @attrOnInt int) => int type t = (@attr ~x: int, ~y: int, @attr ~z: int, @attr ~omega: int) => unit @val external requestAnimationFrame: (float => unit) => unit = "requestAnimationFrame" -@val external requestAnimationFrame: @attr (float => unit) => unit = "requestAnimationFrame" +@val external requestAnimationFrame: @attr ((float => unit) => unit) = "requestAnimationFrame" type arrows = (int, (float => unit) => unit, float) => unit From 0b489637b4a3ee78f55df95bc6446d219d3575a0 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Mon, 23 Sep 2024 14:24:58 +0800 Subject: [PATCH 5/7] Cleanup --- jscomp/syntax/src/res_parsetree_viewer.ml | 27 +--------------------- jscomp/syntax/src/res_parsetree_viewer.mli | 2 -- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/jscomp/syntax/src/res_parsetree_viewer.ml b/jscomp/syntax/src/res_parsetree_viewer.ml index c54a4571da..9edb373b1f 100644 --- a/jscomp/syntax/src/res_parsetree_viewer.ml +++ b/jscomp/syntax/src/res_parsetree_viewer.ml @@ -1,30 +1,5 @@ open Parsetree -let attrs_string attrs = - List.map (fun (attr, _) -> print_endline attr.Asttypes.txt) attrs - -let ident_string ident = - match ident with - | Longident.Lident v -> v - | Longident.Ldot (_, v) -> v - | Longident.Lapply _ -> "Lapply" - -let print_ct ct = - match ct with - | {ptyp_desc = Ptyp_constr (ident, _); ptyp_attributes = attrs} -> - let _ = - print_endline - ("Ptyp_constr: " ^ ident_string ident.txt ^ " attrs: " - ^ string_of_int (List.length attrs)) - in - let _ = attrs_string attrs in - () - | {ptyp_desc = Ptyp_arrow _; ptyp_attributes = attrs} -> - print_endline - ("Ptyp_arrow: " ^ " attrs: " ^ string_of_int (List.length attrs)) - | {ptyp_desc = Ptyp_variant _} -> print_endline "Ptyp_variant" - | _ -> print_endline "Something else" - let arrow_type ?(arity = max_int) ?(attrs = []) ct = let has_as_attr attrs = Ext_list.exists attrs (fun (x, _) -> x.Asttypes.txt = "as") @@ -72,7 +47,7 @@ let arrow_type ?(arity = max_int) ?(attrs = []) ct = match ct with | {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs1} as typ -> - let attrs = List.concat [attrs; attrs1] in + let attrs = attrs @ attrs1 in process attrs [] {typ with ptyp_attributes = []} arity | typ -> process attrs [] typ arity diff --git a/jscomp/syntax/src/res_parsetree_viewer.mli b/jscomp/syntax/src/res_parsetree_viewer.mli index a0bff3ee51..e84691dd25 100644 --- a/jscomp/syntax/src/res_parsetree_viewer.mli +++ b/jscomp/syntax/src/res_parsetree_viewer.mli @@ -9,8 +9,6 @@ val arrow_type : * (Parsetree.attributes * Asttypes.arg_label * Parsetree.core_type) list * Parsetree.core_type -val print_ct : Parsetree.core_type -> unit - val functor_type : Parsetree.module_type -> (Parsetree.attributes * string Asttypes.loc * Parsetree.module_type option) From 3a12f8e2e1c34ab295bad03d1170e5def4560dc2 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 26 Sep 2024 18:41:47 +0200 Subject: [PATCH 6/7] Fix format --- jscomp/test/reasonReact.res | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/jscomp/test/reasonReact.res b/jscomp/test/reasonReact.res index e98f67767d..291e60d0fa 100644 --- a/jscomp/test/reasonReact.res +++ b/jscomp/test/reasonReact.res @@ -117,8 +117,7 @@ and oldNewSelf<'state, 'retainedProps, 'action> = { type rec jsComponentThis<'state, 'props, 'retainedProps, 'action> = { "state": totalState<'state, 'retainedProps, 'action>, "props": {"reasonProps": 'props}, - "setState": @meth - ( + "setState": @meth ( ( totalState<'state, 'retainedProps, 'action>, 'props, From 26a7a2fd1320ed321ee8e064174ebae7f8cd52c6 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Fri, 27 Sep 2024 07:10:43 +0800 Subject: [PATCH 7/7] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6ef80b7a3..8843ee5877 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ #### :bug: Bug fix - Fix tuple coercion. https://github.com/rescript-lang/rescript-compiler/pull/7024 +- Fix attribute printing. https://github.com/rescript-lang/rescript-compiler/pull/7025 #### :nail_care: Polish