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

Commit bef5628

Browse files
Give good error when a type declaration has an inline record type when not permitted (#114)
1 parent 8bf2e17 commit bef5628

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,56 @@ 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+
type nonrec user =
50+
{
51+
name: string ;
52+
address: < street: string ;country: string > Js.t }
53+
let make (props : < handleClick: Click.t -> unit ;value: string > Js.t) =
54+
render props
55+
=====Errors=============================================
56+
57+
Syntax error!
58+
parsing/errors/typeDef/inlineRecord.res 6:9-15
59+
4 ┆ name: string,
60+
5 ┆ reportCard: {
61+
6passing: bool,
62+
7score: int
63+
8}
64+
65+
An inline record type declaration is only allowed in a variant constructor's declaration
66+
67+
Syntax error!
68+
parsing/errors/typeDef/inlineRecord.res 14:5-10
69+
12 ┆ name: string,
70+
13 ┆ address: {
71+
14street: string,
72+
15country: string,
73+
16}
74+
75+
An inline record type declaration is only allowed in a variant constructor's declaration
76+
77+
Syntax error!
78+
parsing/errors/typeDef/inlineRecord.res 19:21-31
79+
17 │ }
80+
18 │
81+
19 │ let make = (props: {handleClick: Click.t => unit, value: string}) => r
82+
ender(props)
83+
20 │
84+
85+
An inline record type declaration is only allowed in a variant constructor's declaration
86+
87+
3888
========================================================"
3989
`;
4090

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type entity =
2+
| Director
3+
| Student({
4+
name: string,
5+
reportCard: {
6+
passing: bool,
7+
score: int
8+
}
9+
})
10+
11+
type user = {
12+
name: string,
13+
address: {
14+
street: string,
15+
country: string,
16+
}
17+
}
18+
19+
let make = (props: {handleClick: Click.t => unit, value: string}) => render(props)

0 commit comments

Comments
 (0)