From ed2a6206fd72f25f8a8a4903b60593c17b4592b0 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 06:57:05 +0200 Subject: [PATCH 1/6] Print a comment before a default value's = once walk_expr_parameter split the trailing comments of a labeled parameter's pattern into those adjacent to it and the rest, discarded the first part (_afterPat) and attached the whole list to the pattern while also walking the rest into the default expression, so (~x as y /* a */ = /* b */ 1) printed /* b */ twice. Attach only the adjacent comments, as walk_expr_argument does. Signed-off-by: Cristiano Calcagno Co-Authored-By: Claude Opus 5.5 --- compiler/syntax/src/res_comments_table.ml | 4 ++-- .../data/printer/comments/expected/namedArgs.res.txt | 2 ++ tests/syntax_tests/data/printer/comments/namedArgs.res | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/syntax/src/res_comments_table.ml b/compiler/syntax/src/res_comments_table.ml index a3bdf7701ee..baf5c716f33 100644 --- a/compiler/syntax/src/res_comments_table.ml +++ b/compiler/syntax/src/res_comments_table.ml @@ -1811,10 +1811,10 @@ and walk_expr_parameter (_attrs, _argLbl, expr_opt, pattern) t comments = walk_pattern pattern t inside; match expr_opt with | Some expr -> - let _afterPat, rest = + let after_pat, rest = partition_adjacent_trailing pattern.ppat_loc trailing in - attach t.trailing pattern.ppat_loc trailing; + attach t.trailing pattern.ppat_loc after_pat; if is_block_expr expr then walk_expression expr t rest else let leading, inside, trailing = partition_by_loc rest expr.pexp_loc in diff --git a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt index f244d48072c..c4bc74315cf 100644 --- a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt @@ -63,3 +63,5 @@ let make = ( ) => { () } + +let f = (~x as y /* a */=/* b */ 1, ()) => y diff --git a/tests/syntax_tests/data/printer/comments/namedArgs.res b/tests/syntax_tests/data/printer/comments/namedArgs.res index d1205c08797..0509812ed1b 100644 --- a/tests/syntax_tests/data/printer/comments/namedArgs.res +++ b/tests/syntax_tests/data/printer/comments/namedArgs.res @@ -63,3 +63,5 @@ let make = ( ) => { () } + +let f = (~x as y /* a */ = /* b */ 1, ()) => y From d45e58e12936b0c2e68706c671aea0ff8e89e4d5 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 06:57:41 +0200 Subject: [PATCH 2/6] Print comments on punned labelled parameters The printer prints a punned labelled parameter (`~x`, `~x: t`) from its label and type, without the pattern, so comments the comment table attaches to the pattern or to the variable inside its type constraint are printed only when that location coincides with the whole parameter's. With a default value it does not, and the comments are dropped: (~x /* a */ = /* b */ 1, ()) printed as (~x=/* b */ 1, ()) (~x: int /* a */ = 1, ()) printed as (~x: int=1, ()) (~x /* a */ : int, ()) printed as (~x: int, ()) For `~x /* a */ = ?` the comment was printed after `=?`, where reparsing attaches it to the next parameter, so formatting was not idempotent. The punned cases now print the pattern's and the variable's comments, and `=?` follows the parameter's trailing comments. The namedArgs comment fixture covers default, typed, optional and aliased parameters; before the change its snapshot lost `/* a */` in the first four new cases and placed it after `=?` in the last three. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Cristiano Calcagno --- compiler/syntax/src/res_printer.ml | 45 +++++++++++-------- .../comments/expected/namedArgs.res.txt | 7 +++ .../data/printer/comments/namedArgs.res | 7 +++ 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 02d61dc928e..60445c69d80 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -5478,27 +5478,34 @@ and print_exp_fun_parameter ~state parameter cmt_tbl = {ppat_desc = Ppat_var string_loc; ppat_attributes} ) when lbl = string_loc.txt -> (* ~d *) - Doc.concat - [ - print_attributes ~state ppat_attributes cmt_tbl; - Doc.text "~"; - print_ident_like lbl; - ] + print_comments + (Doc.concat + [ + print_attributes ~state ppat_attributes cmt_tbl; + Doc.text "~"; + print_ident_like lbl; + ]) + cmt_tbl pattern.ppat_loc | ( (Labelled {txt = lbl} | Optional {txt = lbl}), { - ppat_desc = Ppat_constraint ({ppat_desc = Ppat_var {txt}}, typ); + ppat_desc = + Ppat_constraint + (({ppat_desc = Ppat_var {txt}} as var_pattern), typ); ppat_attributes; } ) when lbl = txt -> (* ~d: e *) - Doc.concat - [ - print_attributes ~state ppat_attributes cmt_tbl; - Doc.text "~"; - print_ident_like lbl; - Doc.text ": "; - print_typ_expr ~state typ cmt_tbl; - ] + print_comments + (Doc.concat + [ + print_attributes ~state ppat_attributes cmt_tbl; + print_comments + (Doc.concat [Doc.text "~"; print_ident_like lbl]) + cmt_tbl var_pattern.ppat_loc; + Doc.text ": "; + print_typ_expr ~state typ cmt_tbl; + ]) + cmt_tbl pattern.ppat_loc | (Labelled {txt = lbl} | Optional {txt = lbl}), pattern -> (* ~b as c *) Doc.concat @@ -5515,9 +5522,7 @@ and print_exp_fun_parameter ~state parameter cmt_tbl = | _ -> Doc.nil in let doc = - Doc.group - (Doc.concat - [attrs; label_with_pattern; default_expr_doc; optional_label_suffix]) + Doc.group (Doc.concat [attrs; label_with_pattern; default_expr_doc]) in let lbl_loc = Asttypes.get_lbl_loc lbl in let cmt_loc = @@ -5525,7 +5530,9 @@ and print_exp_fun_parameter ~state parameter cmt_tbl = | None -> {lbl_loc with loc_end = pattern.ppat_loc.loc_end} | Some expr -> {lbl_loc with loc_end = expr.pexp_loc.loc_end} in - print_comments doc cmt_tbl cmt_loc + (* Without a default, [cmt_loc] ends before [=?], so the comments + attached to it precede [=?] in the source. *) + Doc.concat [print_comments doc cmt_tbl cmt_loc; optional_label_suffix] and print_expression_block ~state ~braces expr cmt_tbl = let expr = Parsetree_viewer.unwrap_braces expr in diff --git a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt index c4bc74315cf..2609e64e044 100644 --- a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt @@ -65,3 +65,10 @@ let make = ( } let f = (~x as y /* a */=/* b */ 1, ()) => y +let f = (~x /* a */=/* b */ 1, ()) => x +let f = (~x: int /* a */=1, ()) => x +let f = (~x /* a */: int /* b */=1, ()) => x +let f = (~x /* a */: int, ()) => x +let f = (~x /* a */=?, ()) => x +let f = (~x: int /* a */=?, ()) => x +let f = (~x as y /* a */=?, ()) => y diff --git a/tests/syntax_tests/data/printer/comments/namedArgs.res b/tests/syntax_tests/data/printer/comments/namedArgs.res index 0509812ed1b..ad3207d1b8a 100644 --- a/tests/syntax_tests/data/printer/comments/namedArgs.res +++ b/tests/syntax_tests/data/printer/comments/namedArgs.res @@ -65,3 +65,10 @@ let make = ( } let f = (~x as y /* a */ = /* b */ 1, ()) => y +let f = (~x /* a */ = /* b */ 1, ()) => x +let f = (~x: int /* a */ = 1, ()) => x +let f = (~x /* a */ : int /* b */ = 1, ()) => x +let f = (~x /* a */ : int, ()) => x +let f = (~x /* a */ = ?, ()) => x +let f = (~x: int /* a */ = ?, ()) => x +let f = (~x as y /* a */ = ?, ()) => y From b733f3949ab2c8fc5e88b46306d8db82e83ce433 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 06:57:42 +0200 Subject: [PATCH 3/6] =?UTF-8?q?Print=20=3D=3F=20after=20the=20comments=20o?= =?UTF-8?q?f=20an=20optional=20arrow-type=20argument?= The printer printed an optional labelled argument of an arrow type with `=?` inside the span whose trailing comments it prints, so a comment before `=?` was printed after it: type f = (~x: int /* a */ =?, unit) => int printed as (~x: int=? /* a */, unit) => int Reparsing attaches that comment to the next argument, so a second format gave (~x: int=?, /* a */ unit), and for a last argument moved it onto the return type. The argument's location ends at its type, before `=?`, so its trailing comments now print before `=?`. The typexpr comment fixture covers a type alias, a last argument and an external; before the change its snapshot placed `/* a */` after `=?` in all three. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Cristiano Calcagno --- compiler/syntax/src/res_printer.ml | 5 +++-- .../data/printer/comments/expected/typexpr.res.txt | 3 +++ tests/syntax_tests/data/printer/comments/typexpr.res | 3 +++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 60445c69d80..72d40cd50f3 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -2364,10 +2364,11 @@ and print_type_parameter ?inline_record_definitions ~state {attrs; lbl; typ} attrs; label; print_typ_expr ?inline_record_definitions ~state typ cmt_tbl; - optional_indicator; ]) in - print_comments doc cmt_tbl loc + (* [loc] ends before [=?], so the comments attached to it precede [=?] in + the source. *) + Doc.concat [print_comments doc cmt_tbl loc; optional_indicator] and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl i = diff --git a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt index a0e5607359b..dc7d3c511d6 100644 --- a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt @@ -95,3 +95,6 @@ external another_test: ( // comment 2 int, ) => unit = "test" +type optionalArg = (~x: int /* a */=?, unit) => int +type lastOptionalArg = (~x: int /* a */=?) => int +external optionalArgExternal: (~x: int /* a */=?, unit) => int = "f" diff --git a/tests/syntax_tests/data/printer/comments/typexpr.res b/tests/syntax_tests/data/printer/comments/typexpr.res index fd27fb3462b..07695aa80f0 100644 --- a/tests/syntax_tests/data/printer/comments/typexpr.res +++ b/tests/syntax_tests/data/printer/comments/typexpr.res @@ -78,3 +78,6 @@ external another_test: ( // comment 2 int, ) => unit = "test" +type optionalArg = (~x: int /* a */ =?, unit) => int +type lastOptionalArg = (~x: int /* a */ =?) => int +external optionalArgExternal: (~x: int /* a */ =?, unit) => int = "f" From bfcc089652125c863fa2789b746126e6329c6f17 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 08:31:49 +0200 Subject: [PATCH 4/6] Add CHANGELOG entry Co-Authored-By: Claude Opus 5.5 Signed-off-by: Cristiano Calcagno --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 426a36292bc..f69123134ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ #### :bug: Bug fix - Make rewatch compile independent modules after an unrelated failure and recompile blocked dependents when a changed interface survives a failed implementation, including across full watcher rebuilds. https://github.com/rescript-lang/rescript/pull/8667 +- Keep comments on labelled parameters with default values, and before `=?` in optional arrow-type arguments, when formatting. https://github.com/rescript-lang/rescript/pull/8690 #### :memo: Documentation From 4cf875250a5a037ccee4ee4d0a24f9f9947e7e70 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 22:07:12 +0200 Subject: [PATCH 5/6] =?UTF-8?q?Print=20comments=20after=20=3D=3F=20after?= =?UTF-8?q?=20the=20marker?= A trailing comment of an optional parameter without a default either precedes =? or follows it in the source. The comment's preceding token tells them apart: past the parameter's location it is the ? of =?. Both the arrow-type and the function-parameter printers now place each comment on its own side of =?. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Cristiano Calcagno --- compiler/syntax/src/res_printer.ml | 36 +++++++++++++++---- .../comments/expected/namedArgs.res.txt | 24 +++++++++++++ .../printer/comments/expected/typexpr.res.txt | 12 +++++++ .../data/printer/comments/namedArgs.res | 24 +++++++++++++ .../data/printer/comments/typexpr.res | 12 +++++++ 5 files changed, 102 insertions(+), 6 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 72d40cd50f3..3d989af4120 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -341,6 +341,32 @@ let print_comments doc (tbl : Comment_table.t) loc = let doc_with_leading_comments = print_leading_comments doc tbl.leading loc in print_trailing_comments doc_with_leading_comments tbl.trailing loc +(* Prints [doc] with the comments of [loc], followed by [marker], a token that + the source writes right after [loc] (such as [=?]). A trailing comment whose + preceding token ends after [loc] follows the marker in the source, so it + prints after [marker]; the other trailing comments print before it. *) +let print_comments_before_marker doc (tbl : Comment_table.t) loc marker = + let after_marker = + match Hashtbl.find_opt tbl.trailing loc with + | None -> [] + | Some comments -> + let after_marker, before_marker = + List.partition + (fun comment -> + (Comment.prev_tok_end_pos comment).pos_cnum + > loc.Location.loc_end.pos_cnum) + comments + in + Hashtbl.replace tbl.trailing loc before_marker; + after_marker + in + let doc = Doc.concat [print_comments doc tbl loc; marker] in + match after_marker with + | [] -> doc + | _ -> + Hashtbl.replace tbl.trailing loc after_marker; + print_trailing_comments doc tbl.trailing loc + let is_empty_doc doc = doc = Doc.nil let print_list ~get_loc ~nodes ~print ?(force_break = false) t = @@ -2366,9 +2392,8 @@ and print_type_parameter ?inline_record_definitions ~state {attrs; lbl; typ} print_typ_expr ?inline_record_definitions ~state typ cmt_tbl; ]) in - (* [loc] ends before [=?], so the comments attached to it precede [=?] in - the source. *) - Doc.concat [print_comments doc cmt_tbl loc; optional_indicator] + (* [loc] ends before [=?]. *) + print_comments_before_marker doc cmt_tbl loc optional_indicator and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl i = @@ -5531,9 +5556,8 @@ and print_exp_fun_parameter ~state parameter cmt_tbl = | None -> {lbl_loc with loc_end = pattern.ppat_loc.loc_end} | Some expr -> {lbl_loc with loc_end = expr.pexp_loc.loc_end} in - (* Without a default, [cmt_loc] ends before [=?], so the comments - attached to it precede [=?] in the source. *) - Doc.concat [print_comments doc cmt_tbl cmt_loc; optional_label_suffix] + (* Without a default, [cmt_loc] ends before [=?]. *) + print_comments_before_marker doc cmt_tbl cmt_loc optional_label_suffix and print_expression_block ~state ~braces expr cmt_tbl = let expr = Parsetree_viewer.unwrap_braces expr in diff --git a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt index 2609e64e044..ba5311bee42 100644 --- a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt @@ -72,3 +72,27 @@ let f = (~x /* a */: int, ()) => x let f = (~x /* a */=?, ()) => x let f = (~x: int /* a */=?, ()) => x let f = (~x as y /* a */=?, ()) => y +let f = ( + ~x=? /* a */, + ~someLongParameterName, + ~anotherLongParameterName, + ~yetAnotherLongParameterName, +) => x +let f = ( + ~x: int=? /* a */, + ~someLongParameterName, + ~anotherLongParameterName, + ~yetAnotherLongParameterName, +) => x +let f = ( + ~x /* a */=? /* b */, + ~someLongParameterName, + ~anotherLongParameterName, + ~yetAnotherLongParameterName, +) => x +let f = ( + ~x as y=? /* a */, + ~someLongParameterName, + ~anotherLongParameterName, + ~yetAnotherLongParameterName, +) => y diff --git a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt index dc7d3c511d6..af45179c6a3 100644 --- a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt @@ -98,3 +98,15 @@ external another_test: ( type optionalArg = (~x: int /* a */=?, unit) => int type lastOptionalArg = (~x: int /* a */=?) => int external optionalArgExternal: (~x: int /* a */=?, unit) => int = "f" +type optionalArgAfter = ( + ~x: int=? /* a */, + ~someLongParameterName: int, + ~anotherLongParameterName: int, + ~yetAnotherLongParameterName: int, +) => int +type optionalArgAround = ( + ~x: int /* a */=? /* b */, + ~someLongParameterName: int, + ~anotherLongParameterName: int, + ~yetAnotherLongParameterName: int, +) => int diff --git a/tests/syntax_tests/data/printer/comments/namedArgs.res b/tests/syntax_tests/data/printer/comments/namedArgs.res index ad3207d1b8a..894446aeb31 100644 --- a/tests/syntax_tests/data/printer/comments/namedArgs.res +++ b/tests/syntax_tests/data/printer/comments/namedArgs.res @@ -72,3 +72,27 @@ let f = (~x /* a */ : int, ()) => x let f = (~x /* a */ = ?, ()) => x let f = (~x: int /* a */ = ?, ()) => x let f = (~x as y /* a */ = ?, ()) => y +let f = ( + ~x=? /* a */, + ~someLongParameterName, + ~anotherLongParameterName, + ~yetAnotherLongParameterName, +) => x +let f = ( + ~x: int=? /* a */, + ~someLongParameterName, + ~anotherLongParameterName, + ~yetAnotherLongParameterName, +) => x +let f = ( + ~x /* a */ =? /* b */, + ~someLongParameterName, + ~anotherLongParameterName, + ~yetAnotherLongParameterName, +) => x +let f = ( + ~x as y=? /* a */, + ~someLongParameterName, + ~anotherLongParameterName, + ~yetAnotherLongParameterName, +) => y diff --git a/tests/syntax_tests/data/printer/comments/typexpr.res b/tests/syntax_tests/data/printer/comments/typexpr.res index 07695aa80f0..1e3bc020374 100644 --- a/tests/syntax_tests/data/printer/comments/typexpr.res +++ b/tests/syntax_tests/data/printer/comments/typexpr.res @@ -81,3 +81,15 @@ external another_test: ( type optionalArg = (~x: int /* a */ =?, unit) => int type lastOptionalArg = (~x: int /* a */ =?) => int external optionalArgExternal: (~x: int /* a */ =?, unit) => int = "f" +type optionalArgAfter = ( + ~x: int=? /* a */, + ~someLongParameterName: int, + ~anotherLongParameterName: int, + ~yetAnotherLongParameterName: int, +) => int +type optionalArgAround = ( + ~x: int /* a */ =? /* b */, + ~someLongParameterName: int, + ~anotherLongParameterName: int, + ~yetAnotherLongParameterName: int, +) => int From 0a294745df97a25ed26b77833665057c8e085eda Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Fri, 2 Oct 2026 22:11:33 +0200 Subject: [PATCH 6/6] =?UTF-8?q?Keep=20comments=20after=20=3D=3F=20with=20t?= =?UTF-8?q?heir=20parameter?= On a single line, a comment after the =? of an optional parameter became a leading comment of the next parameter, or of the body or return type for the last one, so `(~x=? /* a */, ~y)` reformatted to `(~x=?, /* a */ ~y)`. Comments record whether their preceding token is `?`, and the list walker keeps such comments, together with the comments adjacent to them, as trailing comments of the preceding parameter. The printer places before =? only the comments adjacent to the parameter, so a run such as `~x /* a */ /* a2 */ =?` stays before the marker. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Cristiano Calcagno --- compiler/syntax/src/res_comment.ml | 14 +++++++++- compiler/syntax/src/res_comment.mli | 6 ++++ compiler/syntax/src/res_comments_table.ml | 24 ++++++++++++++-- compiler/syntax/src/res_parser.ml | 6 +++- compiler/syntax/src/res_printer.ml | 14 ++++------ .../printer/comments/expected/expr.res.txt | 4 +-- .../comments/expected/namedArgs.res.txt | 28 ++++++------------- .../printer/comments/expected/typexpr.res.txt | 13 ++++----- .../data/printer/comments/namedArgs.res | 25 ++++++----------- .../data/printer/comments/typexpr.res | 13 ++++----- 10 files changed, 83 insertions(+), 64 deletions(-) diff --git a/compiler/syntax/src/res_comment.ml b/compiler/syntax/src/res_comment.ml index f03209f8295..ed72613745c 100644 --- a/compiler/syntax/src/res_comment.ml +++ b/compiler/syntax/src/res_comment.ml @@ -12,6 +12,7 @@ type t = { style: style; loc: Location.t; mutable prev_tok_end_pos: Lexing.position; + mutable prev_tok_is_question: bool; } let loc t = t.loc @@ -20,6 +21,10 @@ let prev_tok_end_pos t = t.prev_tok_end_pos let set_prev_tok_end_pos t pos = t.prev_tok_end_pos <- pos +let prev_tok_is_question t = t.prev_tok_is_question + +let set_prev_tok_is_question t b = t.prev_tok_is_question <- b + let is_single_line_comment t = t.style = SingleLine let is_doc_comment t = t.style = DocComment @@ -35,7 +40,13 @@ let to_string t = (loc_end.pos_cnum - loc_end.pos_bol) let make_single_line_comment ~loc txt = - {txt; loc; style = SingleLine; prev_tok_end_pos = Lexing.dummy_pos} + { + txt; + loc; + style = SingleLine; + prev_tok_end_pos = Lexing.dummy_pos; + prev_tok_is_question = false; + } let make_multi_line_comment ~loc ~doc_comment ~standalone txt = { @@ -45,6 +56,7 @@ let make_multi_line_comment ~loc ~doc_comment ~standalone txt = (if doc_comment then if standalone then ModuleComment else DocComment else MultiLine); prev_tok_end_pos = Lexing.dummy_pos; + prev_tok_is_question = false; } let trim_spaces s = diff --git a/compiler/syntax/src/res_comment.mli b/compiler/syntax/src/res_comment.mli index ce788ddb6c9..5cc7a446a93 100644 --- a/compiler/syntax/src/res_comment.mli +++ b/compiler/syntax/src/res_comment.mli @@ -8,6 +8,12 @@ val prev_tok_end_pos : t -> Lexing.position val set_prev_tok_end_pos : t -> Lexing.position -> unit +(* Whether the token before the comment is [?], e.g. the end of the [=?] of an + optional parameter. *) +val prev_tok_is_question : t -> bool + +val set_prev_tok_is_question : t -> bool -> unit + val is_doc_comment : t -> bool val is_module_comment : t -> bool diff --git a/compiler/syntax/src/res_comments_table.ml b/compiler/syntax/src/res_comments_table.ml index baf5c716f33..1a1b6bf786c 100644 --- a/compiler/syntax/src/res_comments_table.ml +++ b/compiler/syntax/src/res_comments_table.ml @@ -207,6 +207,26 @@ let partition_adjacent_trailing loc1 comments = in loop ~prev_end_pos:loc1.loc_end [] comments +(* Like [partition_adjacent_trailing], and also keeps the comments that follow + * a [?] token placed right after [loc1], as in the [=?] of an optional + * parameter without a default: + * (~x /* before */ =? /* after */, ~y) + * Both comments trail the parameter [~x]. *) +let partition_adjacent_trailing_through_question loc1 comments = + let after_loc1, rest = partition_adjacent_trailing loc1 comments in + match rest with + | first :: _ + when Comment.prev_tok_is_question first + && (Comment.prev_tok_end_pos first).pos_cnum + > loc1.Location.loc_end.pos_cnum -> + let after_question, rest = + partition_adjacent_trailing + {loc1 with loc_end = Comment.prev_tok_end_pos first} + rest + in + (after_loc1 @ after_question, rest) + | _ -> (after_loc1, rest) + (* Splits comments that follow a location but come before another token. * This is particularly useful for handling comments between two tokens * where traditional leading/trailing partitioning isn't precise enough. @@ -717,7 +737,7 @@ and visit_list_but_continue_with_remaining_comments : | Some loc -> let after_prev, rest = if newline_delimited then partition_by_on_same_line loc comments - else partition_adjacent_trailing loc comments + else partition_adjacent_trailing_through_question loc comments in attach t.trailing loc after_prev; rest @@ -735,7 +755,7 @@ and visit_list_but_continue_with_remaining_comments : (* Same line *) if prev_loc.loc_end.pos_lnum == curr_loc.loc_start.pos_lnum then let after_prev, before_curr = - partition_adjacent_trailing prev_loc leading + partition_adjacent_trailing_through_question prev_loc leading in let () = attach t.trailing prev_loc after_prev in let () = attach t.leading curr_loc before_curr in diff --git a/compiler/syntax/src/res_parser.ml b/compiler/syntax/src/res_parser.ml index c35f3db1cac..c1c6e9816b8 100644 --- a/compiler/syntax/src/res_parser.ml +++ b/compiler/syntax/src/res_parser.ml @@ -70,8 +70,12 @@ let rec read cache = match token with | Comment c when not (Comment.is_doc_comment c || Comment.is_module_comment c) -> - let _, preceding_end, _ = cache.scanned in + let _, preceding_end, preceding_token = cache.scanned in Comment.set_prev_tok_end_pos c preceding_end; + Comment.set_prev_tok_is_question c + (match preceding_token with + | Token.Question -> true + | _ -> false); cache.comments <- c :: cache.comments; cache.scanned <- scanned; read cache diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 3d989af4120..d1a4551a83e 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -342,20 +342,16 @@ let print_comments doc (tbl : Comment_table.t) loc = print_trailing_comments doc_with_leading_comments tbl.trailing loc (* Prints [doc] with the comments of [loc], followed by [marker], a token that - the source writes right after [loc] (such as [=?]). A trailing comment whose - preceding token ends after [loc] follows the marker in the source, so it - prints after [marker]; the other trailing comments print before it. *) + the source writes right after [loc] (such as [=?]). The trailing comments + adjacent to [loc] precede the marker in the source and print before it; the + remaining trailing comments print after it. *) let print_comments_before_marker doc (tbl : Comment_table.t) loc marker = let after_marker = match Hashtbl.find_opt tbl.trailing loc with | None -> [] | Some comments -> - let after_marker, before_marker = - List.partition - (fun comment -> - (Comment.prev_tok_end_pos comment).pos_cnum - > loc.Location.loc_end.pos_cnum) - comments + let before_marker, after_marker = + Comment_table.partition_adjacent_trailing loc comments in Hashtbl.replace tbl.trailing loc before_marker; after_marker diff --git a/tests/syntax_tests/data/printer/comments/expected/expr.res.txt b/tests/syntax_tests/data/printer/comments/expected/expr.res.txt index c3f20d18a4d..9f821840637 100644 --- a/tests/syntax_tests/data/printer/comments/expected/expr.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/expr.res.txt @@ -232,8 +232,8 @@ let multiply = (~x=/* c0 */ m1 /* c1 */, ~y=/* c2 */ m2 /* c3 */) => () let f = ( /* c0 */ ~greeting /* c1 */, /* c2 */ ~from as /* c3 */ /* c4 */ hometown /* c5 */, - /* c6 */ ~x=?, -) => /* c7 */ () + /* c6 */ ~x=? /* c7 */, +) => () let multiply = (type /* c-2 */ t /* c-1 */, /* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => () let multiply = ( diff --git a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt index ba5311bee42..c74b892f8a5 100644 --- a/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt @@ -72,27 +72,17 @@ let f = (~x /* a */: int, ()) => x let f = (~x /* a */=?, ()) => x let f = (~x: int /* a */=?, ()) => x let f = (~x as y /* a */=?, ()) => y +let f = (~x=? /* a */, ~y) => x +let f = (~x: int=? /* a */, ~y) => x +let f = (~x=? /* a */) => x +let f = (~x /* a */=? /* b */, ~y) => x +let f = (~x /* a */ /* a2 */=? /* b */ /* b2 */, ~y) => x +let f = (~x as y=? /* a */, ~z) => y +let f = (~x=?, /* a */ ~y) => x +let f = (~x=? /* a */, ~y) => x let f = ( - ~x=? /* a */, + ~x: int /* a */=? /* b */, ~someLongParameterName, ~anotherLongParameterName, ~yetAnotherLongParameterName, ) => x -let f = ( - ~x: int=? /* a */, - ~someLongParameterName, - ~anotherLongParameterName, - ~yetAnotherLongParameterName, -) => x -let f = ( - ~x /* a */=? /* b */, - ~someLongParameterName, - ~anotherLongParameterName, - ~yetAnotherLongParameterName, -) => x -let f = ( - ~x as y=? /* a */, - ~someLongParameterName, - ~anotherLongParameterName, - ~yetAnotherLongParameterName, -) => y diff --git a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt index af45179c6a3..c02f7ebe0b4 100644 --- a/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt +++ b/tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt @@ -98,15 +98,14 @@ external another_test: ( type optionalArg = (~x: int /* a */=?, unit) => int type lastOptionalArg = (~x: int /* a */=?) => int external optionalArgExternal: (~x: int /* a */=?, unit) => int = "f" -type optionalArgAfter = ( +type optionalArgAfter = (~x: int=? /* a */, ~y: int) => int +type lastOptionalArgAfter = (~x: int=? /* a */) => int +type optionalArgAround = (~x: int /* a */ /* a2 */=? /* b */, ~y: int) => int +type optionalArgThenComma = (~x: int=?, /* a */ ~y: int) => int +external optionalArgAfterExternal: (~x: int=? /* a */, unit) => int = "f" +type optionalArgAfterMultiline = ( ~x: int=? /* a */, ~someLongParameterName: int, ~anotherLongParameterName: int, ~yetAnotherLongParameterName: int, ) => int -type optionalArgAround = ( - ~x: int /* a */=? /* b */, - ~someLongParameterName: int, - ~anotherLongParameterName: int, - ~yetAnotherLongParameterName: int, -) => int diff --git a/tests/syntax_tests/data/printer/comments/namedArgs.res b/tests/syntax_tests/data/printer/comments/namedArgs.res index 894446aeb31..e211c6cde77 100644 --- a/tests/syntax_tests/data/printer/comments/namedArgs.res +++ b/tests/syntax_tests/data/printer/comments/namedArgs.res @@ -72,27 +72,20 @@ let f = (~x /* a */ : int, ()) => x let f = (~x /* a */ = ?, ()) => x let f = (~x: int /* a */ = ?, ()) => x let f = (~x as y /* a */ = ?, ()) => y +let f = (~x=? /* a */, ~y) => x +let f = (~x: int=? /* a */, ~y) => x +let f = (~x=? /* a */) => x +let f = (~x /* a */ =? /* b */, ~y) => x +let f = (~x /* a */ /* a2 */ =? /* b */ /* b2 */, ~y) => x +let f = (~x as y=? /* a */, ~z) => y +let f = (~x=?, /* a */ ~y) => x let f = ( ~x=? /* a */, - ~someLongParameterName, - ~anotherLongParameterName, - ~yetAnotherLongParameterName, -) => x -let f = ( - ~x: int=? /* a */, - ~someLongParameterName, - ~anotherLongParameterName, - ~yetAnotherLongParameterName, + ~y, ) => x let f = ( - ~x /* a */ =? /* b */, + ~x: int /* a */ =? /* b */, ~someLongParameterName, ~anotherLongParameterName, ~yetAnotherLongParameterName, ) => x -let f = ( - ~x as y=? /* a */, - ~someLongParameterName, - ~anotherLongParameterName, - ~yetAnotherLongParameterName, -) => y diff --git a/tests/syntax_tests/data/printer/comments/typexpr.res b/tests/syntax_tests/data/printer/comments/typexpr.res index 1e3bc020374..8979259b633 100644 --- a/tests/syntax_tests/data/printer/comments/typexpr.res +++ b/tests/syntax_tests/data/printer/comments/typexpr.res @@ -81,15 +81,14 @@ external another_test: ( type optionalArg = (~x: int /* a */ =?, unit) => int type lastOptionalArg = (~x: int /* a */ =?) => int external optionalArgExternal: (~x: int /* a */ =?, unit) => int = "f" -type optionalArgAfter = ( +type optionalArgAfter = (~x: int=? /* a */, ~y: int) => int +type lastOptionalArgAfter = (~x: int=? /* a */) => int +type optionalArgAround = (~x: int /* a */ /* a2 */ =? /* b */, ~y: int) => int +type optionalArgThenComma = (~x: int=?, /* a */ ~y: int) => int +external optionalArgAfterExternal: (~x: int=? /* a */, unit) => int = "f" +type optionalArgAfterMultiline = ( ~x: int=? /* a */, ~someLongParameterName: int, ~anotherLongParameterName: int, ~yetAnotherLongParameterName: int, ) => int -type optionalArgAround = ( - ~x: int /* a */ =? /* b */, - ~someLongParameterName: int, - ~anotherLongParameterName: int, - ~yetAnotherLongParameterName: int, -) => int