Skip to content

Commit bc9e90b

Browse files
cknittclaude
andcommitted
Keep attributes in front of a record or open type representation
The parser accepts attributes and doc comments between `=` and a record declaration or `..` (since #7787) but threw them away. They now go on the type declaration, after its leading attributes, so PPXs and the type checker see them; the formatter prints them in front of `type`. The attributed path also passes the inline-record context on, so nested record types inside such a record parse. Example (rescript): @Schema type t = @s.strict { email: string, name: string, } type u = @attr {x: {y: int}} Before (rescript): @Schema type t = { email: string, name: string, } // type u: "An inline record type declaration is only allowed in a // variant constructor's declaration or nested inside of a record type // declaration" After (rescript): @Schema @s.strict type t = { email: string, name: string, } @attr type u = {x: {y: int}} Review: Why the declaration and not an error? The issue asks for the attribute to reach the PPX, and the declaration is the only parsetree node a record or open type kind has. An attribute in front of a constructor still attaches to that constructor. Area: syntax tests Output: unchanged Formatting: changed `type t = @A {...}` and `type t = @A ..` now print as `@a type t = ...` instead of dropping `@a` 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/parsing/grammar/typedefinition/expected/recordDeclaration.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 58da277 commit bc9e90b

7 files changed

Lines changed: 104 additions & 58 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Keep attributes and doc comments written in front of a record or `..` type representation, attaching them to the type declaration instead of dropping them.

‎compiler/syntax/src/res_core.ml‎

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5391,10 +5391,10 @@ and parse_type_representation ?current_type_name_path ?inline_types_context p =
53915391
if Parser.optional p Token.Private then Asttypes.Private
53925392
else Asttypes.Public
53935393
in
5394-
let kind =
5394+
let kind, attrs =
53955395
match Parser.peek p with
53965396
| Bar | Uident _ | DocComment _ ->
5397-
Parsetree.Ptype_variant (parse_type_constructor_declarations p)
5397+
(Parsetree.Ptype_variant (parse_type_constructor_declarations p), [])
53985398
| At -> (
53995399
(* Attributes can prefix either a variant (constructor list), a record, or an
54005400
open/extensible variant marker (`..`). Peek past attributes and any doc
@@ -5407,37 +5407,39 @@ and parse_type_representation ?current_type_name_path ?inline_types_context p =
54075407
in
54085408
match after_attrs with
54095409
| Lbrace ->
5410-
(* consume the attributes and any doc comments before the record *)
5411-
ignore (parse_attributes p);
5412-
skip_doc_comments p;
5413-
Parsetree.Ptype_record
5414-
(parse_record_declaration ?current_type_name_path
5415-
?inline_types_context p)
5410+
(* Attributes (and doc comments) before a record have no node of their
5411+
own: they are returned so the caller attaches them to the type
5412+
declaration. *)
5413+
let attrs = parse_attributes p in
5414+
( Parsetree.Ptype_record
5415+
(parse_record_declaration ?current_type_name_path
5416+
?inline_types_context p),
5417+
attrs )
54165418
| DotDot ->
5417-
(* attributes before an open variant marker; consume attrs/docs then handle `..` *)
5418-
ignore (parse_attributes p);
5419-
skip_doc_comments p;
5419+
(* Same for attributes before an open variant marker `..`. *)
5420+
let attrs = parse_attributes p in
54205421
Parser.next p;
54215422
(* consume DotDot *)
5422-
Ptype_open
5423+
(Ptype_open, attrs)
54235424
| _ ->
54245425
(* fall back to variant constructor declarations; leave attributes for the
54255426
constructor parsing so they attach to the first constructor. *)
5426-
Parsetree.Ptype_variant (parse_type_constructor_declarations p))
5427+
(Parsetree.Ptype_variant (parse_type_constructor_declarations p), []))
54275428
| Lbrace ->
5428-
Parsetree.Ptype_record
5429-
(parse_record_declaration ?current_type_name_path ?inline_types_context
5430-
p)
5429+
( Parsetree.Ptype_record
5430+
(parse_record_declaration ?current_type_name_path
5431+
?inline_types_context p),
5432+
[] )
54315433
| DotDot ->
54325434
Parser.next p;
5433-
Ptype_open
5435+
(Ptype_open, [])
54345436
| token ->
54355437
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
54365438
(* TODO: I have no idea if this is even remotely a good idea *)
5437-
Parsetree.Ptype_variant []
5439+
(Parsetree.Ptype_variant [], [])
54385440
in
54395441
Parser.eat_breadcrumb p;
5440-
(private_flag, kind)
5442+
(private_flag, kind, attrs)
54415443

54425444
(* type-param ::=
54435445
* | variance 'lident
@@ -5588,8 +5590,8 @@ and parse_type_equation_or_constr_decl p =
55885590
match Parser.peek p with
55895591
| Equal ->
55905592
Parser.next p;
5591-
let priv, kind = parse_type_representation p in
5592-
(Some typ, priv, kind)
5593+
let priv, kind, attrs = parse_type_representation p in
5594+
(Some typ, priv, kind, attrs)
55935595
| EqualGreater ->
55945596
Parser.next p;
55955597
let return_type = parse_typ_expr ~alias:false p in
@@ -5600,8 +5602,8 @@ and parse_type_equation_or_constr_decl p =
56005602
return_type
56015603
in
56025604
let typ = parse_type_alias p arrow_type in
5603-
(Some typ, Asttypes.Public, Parsetree.Ptype_abstract)
5604-
| _ -> (Some typ, Asttypes.Public, Parsetree.Ptype_abstract))
5605+
(Some typ, Asttypes.Public, Parsetree.Ptype_abstract, [])
5606+
| _ -> (Some typ, Asttypes.Public, Parsetree.Ptype_abstract, []))
56055607
| _ ->
56065608
let uident_end_pos = Parser.position p in
56075609
let args, res = parse_constr_decl_args p in
@@ -5615,12 +5617,12 @@ and parse_type_equation_or_constr_decl p =
56155617
in
56165618
( None,
56175619
Asttypes.Public,
5618-
Parsetree.Ptype_variant (parse_type_constructor_declarations p ?first)
5619-
))
5620+
Parsetree.Ptype_variant (parse_type_constructor_declarations p ?first),
5621+
[] ))
56205622
| t ->
56215623
Parser.err p (Diagnostics.uident t);
56225624
(* TODO: is this a good idea? *)
5623-
(None, Asttypes.Public, Parsetree.Ptype_abstract)
5625+
(None, Asttypes.Public, Parsetree.Ptype_abstract, [])
56245626

56255627
and parse_spread_tail_classified ?current_type_name_path ?inline_types_context
56265628
~start_pos ~spread_typ ~grammar p =
@@ -5830,18 +5832,18 @@ and parse_private_eq_or_repr p =
58305832
match Parser.peek p with
58315833
| Lbrace ->
58325834
let manifest, _, kind = parse_record_or_object_decl p in
5833-
(manifest, Asttypes.Private, kind)
5835+
(manifest, Asttypes.Private, kind, [])
58345836
| Uident _ ->
5835-
let manifest, _, kind = parse_type_equation_or_constr_decl p in
5836-
(manifest, Asttypes.Private, kind)
5837+
let manifest, _, kind, attrs = parse_type_equation_or_constr_decl p in
5838+
(manifest, Asttypes.Private, kind, attrs)
58375839
| Bar | DotDot ->
5838-
let _, kind = parse_type_representation p in
5839-
(None, Asttypes.Private, kind)
5840+
let _, kind, attrs = parse_type_representation p in
5841+
(None, Asttypes.Private, kind, attrs)
58405842
| t when Grammar.is_typ_expr_start t ->
5841-
(Some (parse_typ_expr p), Asttypes.Private, Parsetree.Ptype_abstract)
5843+
(Some (parse_typ_expr p), Asttypes.Private, Parsetree.Ptype_abstract, [])
58425844
| _ ->
5843-
let _, kind = parse_type_representation p in
5844-
(None, Asttypes.Private, kind)
5845+
let _, kind, attrs = parse_type_representation p in
5846+
(None, Asttypes.Private, kind, attrs)
58455847

58465848
(*
58475849
polymorphic-variant-type ::=
@@ -6066,12 +6068,15 @@ and parse_type_equation_and_representation ?current_type_name_path
60666068
match Parser.peek p with
60676069
| Uident _ -> parse_type_equation_or_constr_decl p
60686070
| Lbrace ->
6069-
parse_record_or_object_decl ?current_type_name_path ?inline_types_context
6070-
p
6071+
let manifest, priv, kind =
6072+
parse_record_or_object_decl ?current_type_name_path
6073+
?inline_types_context p
6074+
in
6075+
(manifest, priv, kind, [])
60716076
| Private -> parse_private_eq_or_repr p
60726077
| Bar | DotDot | DocComment _ ->
6073-
let priv, kind = parse_type_representation p in
6074-
(None, priv, kind)
6078+
let priv, kind, attrs = parse_type_representation p in
6079+
(None, priv, kind, attrs)
60756080
| At -> (
60766081
(* Attributes can start a representation (variant/record/open variant) or a manifest.
60776082
Look ahead past attributes (and doc comments). If a representation-like token follows,
@@ -6109,31 +6114,34 @@ and parse_type_equation_and_representation ?current_type_name_path
61096114
| _ -> false)
61106115
in
61116116
if is_representation_after_attrs then
6112-
let priv, kind = parse_type_representation p in
6113-
(None, priv, kind)
6117+
let priv, kind, attrs =
6118+
parse_type_representation ?current_type_name_path
6119+
?inline_types_context p
6120+
in
6121+
(None, priv, kind, attrs)
61146122
else
61156123
let manifest = Some (parse_typ_expr p) in
61166124
match Parser.peek p with
61176125
| Equal ->
61186126
Parser.next p;
6119-
let priv, kind =
6127+
let priv, kind, attrs =
61206128
parse_type_representation ?current_type_name_path
61216129
?inline_types_context p
61226130
in
6123-
(manifest, priv, kind)
6124-
| _ -> (manifest, Public, Parsetree.Ptype_abstract))
6131+
(manifest, priv, kind, attrs)
6132+
| _ -> (manifest, Public, Parsetree.Ptype_abstract, []))
61256133
| _ -> (
61266134
let manifest = Some (parse_typ_expr p) in
61276135
match Parser.peek p with
61286136
| Equal ->
61296137
Parser.next p;
6130-
let priv, kind =
6138+
let priv, kind, attrs =
61316139
parse_type_representation ?current_type_name_path
61326140
?inline_types_context p
61336141
in
6134-
(manifest, priv, kind)
6135-
| _ -> (manifest, Public, Parsetree.Ptype_abstract)))
6136-
| _ -> (None, Public, Parsetree.Ptype_abstract)
6142+
(manifest, priv, kind, attrs)
6143+
| _ -> (manifest, Public, Parsetree.Ptype_abstract, [])))
6144+
| _ -> (None, Public, Parsetree.Ptype_abstract, [])
61376145

61386146
(* type-definition ::= type [rec] typedef { and typedef }
61396147
* typedef ::= typeconstr-name [type-params] type-information
@@ -6150,11 +6158,13 @@ and parse_type_def ~attrs ~start_pos p =
61506158
parse_type_params ~parent:constr_name p
61516159
in
61526160
let type_def =
6153-
let manifest, priv, kind = parse_type_equation_and_representation p in
6161+
let manifest, priv, kind, repr_attrs =
6162+
parse_type_equation_and_representation p
6163+
in
61546164
let cstrs = parse_type_constraints p in
61556165
let loc = mk_loc start_pos (Parser.position p) in
6156-
Ast_helper.Type.mk ~loc ~attrs ~priv ~kind ~params ~cstrs ?manifest
6157-
type_constr_name
6166+
Ast_helper.Type.mk ~loc ~attrs:(attrs @ repr_attrs) ~priv ~kind ~params
6167+
~cstrs ?manifest type_constr_name
61586168
in
61596169
Parser.eat_breadcrumb p;
61606170
type_def
@@ -6198,13 +6208,14 @@ and parse_type_extension ~params ~attrs ~name p =
61986208
and parse_type_definitions ~current_type_name_path ~inline_types_context ~attrs
61996209
~name ~params ~start_pos p =
62006210
let type_def =
6201-
let manifest, priv, kind =
6211+
let manifest, priv, kind, repr_attrs =
62026212
parse_type_equation_and_representation ~current_type_name_path
62036213
~inline_types_context p
62046214
in
62056215
let cstrs = parse_type_constraints p in
62066216
let loc = mk_loc start_pos (Parser.position p) in
6207-
Ast_helper.Type.mk ~loc ~attrs ~priv ~kind ~params ~cstrs ?manifest
6217+
Ast_helper.Type.mk ~loc ~attrs:(attrs @ repr_attrs) ~priv ~kind ~params
6218+
~cstrs ?manifest
62086219
{name with txt = lident_of_path name.Location.txt}
62096220
in
62106221
let rec loop p defs =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ type nonrec domProps =
5555
list: string [@optional ];
5656
loop: bool [@optional ]}
5757
type nonrec t = {
58-
x: int }
58+
x: int }[@@attr ]
5959
type nonrec t = {
60-
x: int }
60+
x: int }[@@attr ][@@res.doc {js|doc before record|js}]

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,19 @@ type nonrec t = private {
4343
x: int ;
4444
y: int }
4545
type nonrec t = {
46-
x: int }
46+
x: int }[@@attr ]
4747
type nonrec t = {
48-
x: int }
49-
type nonrec t = ..
50-
type nonrec t = ..
48+
x: int }[@@attr ][@@res.doc {js|doc before record|js}]
49+
type nonrec t = ..[@@attr ]
50+
type nonrec t = ..[@@attr ][@@res.doc {js|doc before open|js}]
51+
type t.x = {
52+
y: int }
53+
and t = {
54+
x: t.x }[@@attr ]
55+
type nonrec t = M.t = {
56+
x: int }[@@attr ]
57+
type nonrec 'a t = {
58+
x: 'a }[@@attr ]
59+
type nonrec t = int
60+
and u = {
61+
x: int }[@@attr ]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ type t = @attr {x: int}
3232
type t = @attr /**doc before record*/ {x: int}
3333
type t = @attr ..
3434
type t = @attr /**doc before open*/ ..
35+
type t = @attr {x: {y: int}}
36+
type t = M.t = @attr {x: int}
37+
type t<'a> = @attr {x: 'a}
38+
type t = int and u = @attr {x: int}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ type t = int
99
@attrWithSuperLongIdentifeirName @attrWithSuperLongIdentifeirName @attrWithSuperLongIdentifeirName@attrWithSuperLongIdentifeirName type t = string
1010
@attrWithSuperLongIdentifeirName @attrWithSuperLongIdentifeirName @attrWithSuperLongIdentifeirName@attrWithSuperLongIdentifeirName
1111
type t = string
12+
13+
// attributes in front of a record or `..` move to the declaration
14+
@schema
15+
type t = @s.strict {
16+
email: string,
17+
name: string,
18+
}
19+
type t = @attr /** doc */ {x: int}
20+
type t = @attr ..

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ type t = string
1616
@attrWithSuperLongIdentifeirName
1717
@attrWithSuperLongIdentifeirName
1818
type t = string
19+
20+
// attributes in front of a record or `..` move to the declaration
21+
@schema @s.strict
22+
type t = {
23+
email: string,
24+
name: string,
25+
}
26+
/** doc */
27+
@attr type t = {x: int}
28+
@attr type t = ..

0 commit comments

Comments
 (0)