Skip to content

Commit cf0a239

Browse files
cknittclaude
andcommitted
Keep a doc comment in front of a record or open type representation
A doc comment directly after `=` now goes through the same lookahead as an attribute: in front of `{` or `..` it attaches to the type declaration, in front of a constructor it still attaches to that constructor. Before, only a doc comment after an attribute reached the declaration; one at the start was parsed as a constructor list and failed on `{` or `..`. Example (rescript): type t = /** doc */ {x: int} type u = /** doc */ .. Before (text): syntax errors: a constructor is expected at `{` and at `..` After (rescript): /** doc */ type t = {x: int} /** doc */ type u = .. Area: syntax tests Output: unchanged Formatting: changed `type t = /** doc */ {...}` and `type t = /** doc */ ..` now parse and print with the doc comment in front of `type` Formats: none PPX: unchanged Editor: unchanged Tests: ~tests/syntax_tests/data/parsing/grammar/typedefinition/typeRepresentation.res ~tests/syntax_tests/data/parsing/grammar/typedefinition/expected/typeRepresentation.res.txt ~tests/syntax_tests/data/printer/typeDef/attributes.res ~tests/syntax_tests/data/printer/typeDef/expected/attributes.res.txt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent bc9e90b commit cf0a239

5 files changed

Lines changed: 71 additions & 7 deletions

File tree

‎compiler/syntax/src/res_core.ml‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5393,12 +5393,12 @@ and parse_type_representation ?current_type_name_path ?inline_types_context p =
53935393
in
53945394
let kind, attrs =
53955395
match Parser.peek p with
5396-
| Bar | Uident _ | DocComment _ ->
5396+
| Bar | Uident _ ->
53975397
(Parsetree.Ptype_variant (parse_type_constructor_declarations p), [])
5398-
| At -> (
5399-
(* Attributes can prefix either a variant (constructor list), a record, or an
5400-
open/extensible variant marker (`..`). Peek past attributes and any doc
5401-
comments to decide which kind it is. *)
5398+
| At | DocComment _ -> (
5399+
(* Attributes and doc comments can prefix either a variant (constructor
5400+
list), a record, or an open/extensible variant marker (`..`). Peek past
5401+
them to decide which kind it is. *)
54025402
let after_attrs =
54035403
Parser.lookahead p (fun state ->
54045404
ignore (parse_attributes state);
@@ -6074,9 +6074,15 @@ and parse_type_equation_and_representation ?current_type_name_path
60746074
in
60756075
(manifest, priv, kind, [])
60766076
| Private -> parse_private_eq_or_repr p
6077-
| Bar | DotDot | DocComment _ ->
6077+
| Bar | DotDot ->
60786078
let priv, kind, attrs = parse_type_representation p in
60796079
(None, priv, kind, attrs)
6080+
| DocComment _ ->
6081+
let priv, kind, attrs =
6082+
parse_type_representation ?current_type_name_path ?inline_types_context
6083+
p
6084+
in
6085+
(None, priv, kind, attrs)
60806086
| At -> (
60816087
(* Attributes can start a representation (variant/record/open variant) or a manifest.
60826088
Look ahead past attributes (and doc comments). If a representation-like token follows,

‎tests/syntax_tests/data/parsing/grammar/typedefinition/expected/typeRepresentation.res.txt‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,25 @@ type nonrec 'a t = {
5858
x: 'a }[@@attr ]
5959
type nonrec t = int
6060
and u = {
61-
x: int }[@@attr ]
61+
x: int }[@@attr ]
62+
type nonrec t = {
63+
x: int }[@@res.doc {js|doc before record|js}]
64+
type nonrec t = ..[@@res.doc {js|doc before open|js}]
65+
type nonrec t = {
66+
x: int }[@@res.doc {js|doc|js}][@@attr ]
67+
type nonrec t = ..[@@attr ][@@res.doc {js|doc|js}][@@attr2 ]
68+
type t.x = {
69+
y: int }
70+
and t = {
71+
x: t.x }[@@res.doc {js|doc|js}]
72+
type nonrec t = private {
73+
x: int }[@@res.doc {js|doc|js}]
74+
type nonrec t =
75+
| A [@res.doc {js|doc|js}]
76+
| B
77+
type nonrec t =
78+
| A [@res.doc {js|doc|js}]
79+
| B [@res.doc {js|doc2|js}]
80+
type nonrec t =
81+
| A [@res.doc {js|doc|js}][@attr ]
82+
| B

‎tests/syntax_tests/data/parsing/grammar/typedefinition/typeRepresentation.res‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ type t = @attr {x: {y: int}}
3636
type t = M.t = @attr {x: int}
3737
type t<'a> = @attr {x: 'a}
3838
type t = int and u = @attr {x: int}
39+
type t = /**doc before record*/ {x: int}
40+
type t = /**doc before open*/ ..
41+
type t = /**doc*/ @attr {x: int}
42+
type t = @attr /**doc*/ @attr2 ..
43+
type t = /**doc*/ {x: {y: int}}
44+
type t = private /**doc*/ {x: int}
45+
type t = /**doc*/ A | B
46+
type t = /**doc*/ | A | /**doc2*/ B
47+
type t = /**doc*/ @attr A | B

‎tests/syntax_tests/data/printer/typeDef/attributes.res‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ type t = @s.strict {
1818
}
1919
type t = @attr /** doc */ {x: int}
2020
type t = @attr ..
21+
type t = /** doc */ {x: int}
22+
type t = /** doc */ ..
23+
type t = /** doc */ @attr {x: int}
24+
type t = @attr /** doc */ @attr2 ..
25+
@outer
26+
type t = /* c1 */ /** doc */ /* c2 */ {x: int} // trail
27+
type t = /** doc */ A | B
28+
type t = | /** doc */ A | /** doc2 */ B

‎tests/syntax_tests/data/printer/typeDef/expected/attributes.res.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,23 @@ type t = {
2626
/** doc */
2727
@attr type t = {x: int}
2828
@attr type t = ..
29+
/** doc */
30+
type t = {x: int}
31+
/** doc */
32+
type t = ..
33+
/** doc */
34+
@attr type t = {x: int}
35+
/** doc */
36+
@attr @attr2 type t = ..
37+
/** doc */
38+
@outer
39+
type t = {/* c1 */ /* c2 */ x: int} // trail
40+
type t =
41+
/** doc */
42+
| A
43+
| B
44+
type t =
45+
/** doc */
46+
| A
47+
/** doc2 */
48+
| B

0 commit comments

Comments
 (0)