Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 1897a72

Browse files
author
Iwan
committed
Give good error when a type declaration has an inline record type when not permitted
1 parent 8bf2e17 commit 1897a72

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/res_core.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,6 +4024,14 @@ and parseStringFieldDeclaration p =
40244024
Parser.expect ~grammar:Grammar.TypeExpression Colon p;
40254025
let typ = parsePolyTypeExpr p in
40264026
Some(Parsetree.Otag (fieldName, attrs, typ))
4027+
| Lident name ->
4028+
let nameLoc = mkLoc p.startPos p.endPos in
4029+
Parser.err p (Diagnostics.message "An inline record type declaration is only allowed in a variant constructor's declaration");
4030+
Parser.next p;
4031+
let fieldName = Location.mkloc name nameLoc in
4032+
Parser.expect ~grammar:Grammar.TypeExpression Colon p;
4033+
let typ = parsePolyTypeExpr p in
4034+
Some(Parsetree.Otag (fieldName, attrs, typ))
40274035
| _token ->
40284036
None
40294037

src/res_grammar.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ let isParameterStart = function
209209

210210
(* TODO: overparse Uident ? *)
211211
let isStringFieldDeclStart = function
212-
| Token.String _ | At -> true
212+
| Token.String _ | Lident _ | At -> true
213213
| _ -> false
214214

215215
(* TODO: overparse Uident ? *)

tests/parsing/errors/typeDef/__snapshots__/parse.spec.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ type nonrec record = {
3535
A record needs at least one field
3636
3737
38+
========================================================"
39+
`;
40+
41+
exports[`inlineRecord.res 1`] = `
42+
"=====Parsetree==========================================
43+
type nonrec entity =
44+
| Director
45+
| Student of
46+
{
47+
name: string ;
48+
reportCard: < passing: bool ;score: int > Js.t }
49+
=====Errors=============================================
50+
51+
Syntax error!
52+
parsing/errors/typeDef/inlineRecord.res 6:9-15
53+
4 ┆ name: string,
54+
5 ┆ reportCard: {
55+
6passing: bool,
56+
7score: int
57+
8}
58+
59+
An inline record type declaration is only allowed in a variant constructor's declaration
60+
61+
3862
========================================================"
3963
`;
4064

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type entity =
2+
| Director
3+
| Student({
4+
name: string,
5+
reportCard: {
6+
passing: bool,
7+
score: int
8+
}
9+
})

0 commit comments

Comments
 (0)