@@ -134,10 +134,6 @@ module ErrorMessages = struct
134
134
let stringInterpolationInPattern =
135
135
" String interpolation is not supported in pattern matching."
136
136
137
- let spreadInRecordDeclaration =
138
- " A record type declaration doesn't support the ... spread. Only an object \
139
- (with quoted field names) does."
140
-
141
137
let objectQuotedFieldName name =
142
138
" An object type declaration needs quoted field names. Did you mean \" "
143
139
^ name ^ " \" ?"
@@ -482,6 +478,11 @@ let lidentOfPath longident =
482
478
| [] -> " "
483
479
| ident :: _ -> ident
484
480
481
+ let makeRecordDotField ~dotdotdotStart ~dotdotdotEnd ~loc typ =
482
+ Ast_helper.Type. field ~loc
483
+ {txt = " ..." ; loc = mkLoc dotdotdotStart dotdotdotEnd}
484
+ typ
485
+
485
486
let makeNewtypes ~attrs ~loc newtypes exp =
486
487
let expr =
487
488
List. fold_right
@@ -4579,40 +4580,37 @@ and parseConstrDeclArgs p =
4579
4580
(* start of object type spreading, e.g. `User({...a, "u": int})` *)
4580
4581
Parser. next p;
4581
4582
let typ = parseTypExpr p in
4582
- let () =
4583
- match p.token with
4584
- | Rbrace ->
4585
- (* {...x}, spread without extra fields *)
4586
- Parser. next p
4587
- | _ -> Parser. expect Comma p
4588
- in
4589
- let () =
4590
- match p.token with
4591
- | Lident _ ->
4592
- Parser. err ~start Pos:dotdotdotStart ~end Pos:dotdotdotEnd p
4593
- (Diagnostics. message ErrorMessages. spreadInRecordDeclaration)
4594
- | _ -> ()
4595
- in
4596
- let fields =
4597
- Parsetree. Oinherit typ
4598
- :: parseCommaDelimitedRegion
4599
- ~grammar: Grammar. StringFieldDeclarations ~closing: Rbrace
4600
- ~f: parseStringFieldDeclaration p
4601
- in
4602
- Parser. expect Rbrace p;
4603
- let loc = mkLoc startPos p.prevEndPos in
4604
- let typ =
4605
- Ast_helper.Typ. object_ ~loc fields Asttypes. Closed
4606
- |> parseTypeAlias p
4607
- in
4608
- let typ = parseArrowTypeRest ~es6 Arrow:true ~start Pos typ p in
4609
- Parser. optional p Comma |> ignore;
4610
- let moreArgs =
4611
- parseCommaDelimitedRegion ~grammar: Grammar. TypExprList
4612
- ~closing: Rparen ~f: parseTypExprRegion p
4613
- in
4614
- Parser. expect Rparen p;
4615
- Parsetree. Pcstr_tuple (typ :: moreArgs)
4583
+ (* always treat single spreads as records *)
4584
+ let isSingleSpread = p.token = Rbrace in
4585
+ if isSingleSpread then (
4586
+ Parser. expect Rbrace p;
4587
+ let loc = mkLoc startPos p.prevEndPos in
4588
+ let dotField =
4589
+ typ |> makeRecordDotField ~loc ~dotdotdot Start ~dotdotdot End
4590
+ in
4591
+ Parser. expect Rparen p;
4592
+ Parsetree. Pcstr_record [dotField])
4593
+ else
4594
+ let fields =
4595
+ Parsetree. Oinherit typ
4596
+ :: parseCommaDelimitedRegion
4597
+ ~grammar: Grammar. StringFieldDeclarations ~closing: Rbrace
4598
+ ~f: parseStringFieldDeclaration p
4599
+ in
4600
+ Parser. expect Rbrace p;
4601
+ let loc = mkLoc startPos p.prevEndPos in
4602
+ let typ =
4603
+ Ast_helper.Typ. object_ ~loc fields Asttypes. Closed
4604
+ |> parseTypeAlias p
4605
+ in
4606
+ let typ = parseArrowTypeRest ~es6 Arrow:true ~start Pos typ p in
4607
+ Parser. optional p Comma |> ignore;
4608
+ let moreArgs =
4609
+ parseCommaDelimitedRegion ~grammar: Grammar. TypExprList
4610
+ ~closing: Rparen ~f: parseTypExprRegion p
4611
+ in
4612
+ Parser. expect Rparen p;
4613
+ Parsetree. Pcstr_tuple (typ :: moreArgs)
4616
4614
| _ -> (
4617
4615
let attrs = parseAttributes p in
4618
4616
match p.Parser. token with
@@ -5016,19 +5014,15 @@ and parseRecordOrObjectDecl p =
5016
5014
Parser. next p;
5017
5015
let loc = mkLoc startPos p.prevEndPos in
5018
5016
let dotField =
5019
- Ast_helper.Type. field ~loc
5020
- {txt = " ..." ; loc = mkLoc dotdotdotStart dotdotdotEnd}
5021
- typ
5017
+ typ |> makeRecordDotField ~loc ~dotdotdot Start ~dotdotdot End
5022
5018
in
5023
5019
let kind = Parsetree. Ptype_record [dotField] in
5024
5020
(None , Public , kind)
5025
5021
| _ ->
5026
5022
Parser. expect Comma p;
5027
5023
let loc = mkLoc startPos p.prevEndPos in
5028
5024
let dotField =
5029
- Ast_helper.Type. field ~loc
5030
- {txt = " ..." ; loc = mkLoc dotdotdotStart dotdotdotEnd}
5031
- typ
5025
+ typ |> makeRecordDotField ~loc ~dotdotdot Start ~dotdotdot End
5032
5026
in
5033
5027
let foundObjectField = ref false in
5034
5028
let fields =
0 commit comments