Skip to content

Commit 16bc975

Browse files
cknittclaude
andcommitted
Keep a doc comment in front of a record or open type representation
Why: Only a doc comment after an attribute reached the declaration; one directly after `=` was parsed as the start of a constructor list and failed on `{` or `..`. It 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. AST modified (doc comment before a record representation): Example: type t = /** doc */ {x: int} Before (diagnostic): At this point, I'm looking for an uppercased name like `Array` or `Math` After (parse): type_declaration "t" attribute "res.doc" [ structure_item Pstr_eval expression Pexp_constant PConst_string (source=" doc ", semantic=" doc ") ] ptype_params = [] ptype_cstrs = [] ptype_kind = Ptype_record … Format modified (doc comment before a record or open representation): Example: type t = /** doc */ {x: int} type u = /** doc */ .. After (rescript): /** doc */ type t = {x: int} /** doc */ type u = .. 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 eac6206 commit 16bc975

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
@@ -5419,12 +5419,12 @@ and parse_type_representation ?current_type_name_path ?inline_types_context p =
54195419
in
54205420
let kind, attrs =
54215421
match Parser.peek p with
5422-
| Bar | Uident _ | DocComment _ ->
5422+
| Bar | Uident _ ->
54235423
(Parsetree.Ptype_variant (parse_type_constructor_declarations p), [])
5424-
| At -> (
5425-
(* Attributes can prefix either a variant (constructor list), a record, or an
5426-
open/extensible variant marker (`..`). Peek past attributes and any doc
5427-
comments to decide which kind it is. *)
5424+
| At | DocComment _ -> (
5425+
(* Attributes and doc comments can prefix either a variant (constructor
5426+
list), a record, or an open/extensible variant marker (`..`). Peek past
5427+
them to decide which kind it is. *)
54285428
let after_attrs =
54295429
Parser.lookahead p (fun state ->
54305430
ignore (parse_attributes state);
@@ -6100,9 +6100,15 @@ and parse_type_equation_and_representation ?current_type_name_path
61006100
in
61016101
(manifest, priv, kind, [])
61026102
| Private -> parse_private_eq_or_repr p
6103-
| Bar | DotDot | DocComment _ ->
6103+
| Bar | DotDot ->
61046104
let priv, kind, attrs = parse_type_representation p in
61056105
(None, priv, kind, attrs)
6106+
| DocComment _ ->
6107+
let priv, kind, attrs =
6108+
parse_type_representation ?current_type_name_path ?inline_types_context
6109+
p
6110+
in
6111+
(None, priv, kind, attrs)
61066112
| At -> (
61076113
(* Attributes can start a representation (variant/record/open variant) or a manifest.
61086114
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)