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

Better error message on capitalized type #424

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,14 @@ let parseValuePath p =
| Lident ident -> Longident.Lident ident
| Uident ident ->
Parser.next p;
Parser.expect Dot p;
aux p (Lident ident)
begin match p.token with
| Dot ->
Parser.next p;
aux p (Lident ident)
| _ ->
Parser.err p (Diagnostics.message "Identifier needs to be lower-cased.");
Longident.Lident ident
end
| token ->
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
Longident.Lident "_"
Expand Down Expand Up @@ -5223,7 +5229,21 @@ and parseTypeDefinitionOrExtension ~attrs p =
Asttypes.Nonrecursive
| _ -> Asttypes.Nonrecursive
in
let name = parseValuePath p in
let name = match p.token with
| Uident ident ->
let identStartPos = p.startPos in
let identEndPos = p.endPos in
Parser.next p;
begin match p.token with
| Dot ->
Parser.next p;
parseValuePathTail p identStartPos (Longident.Lident ident)
| _ ->
Parser.err p ~startPos:identStartPos ~endPos:identEndPos (Diagnostics.message "Type names need to start with a lower-cased letter.");
Location.mkloc (Longident.Lident ident) (mkLoc identStartPos identEndPos)
end
| _ -> parseValuePath p
in
let params = parseTypeParams ~parent:name p in
match p.Parser.token with
| PlusEqual ->
Expand Down
22 changes: 18 additions & 4 deletions tests/parsing/errors/typeDef/expected/typeDef.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,35 @@
10 │ // missing type
11 │ type state =
12 │
13 │ // name cannot be capitalized

Missing a type here


Syntax error!
tests/parsing/errors/typeDef/typeDef.res:12:1
tests/parsing/errors/typeDef/typeDef.res:14:1-4

10 │ // missing type
11 │ type state =
12 │
13 │ // name cannot be capitalized
14 │ type User = string
15 │

Missing a type here


Syntax error!
tests/parsing/errors/typeDef/typeDef.res:14:6-9

12 │
13 │ // name cannot be capitalized
14 │ type User = string
15 │

Type names need to start with a lower-cased letter.

type stack =
| Empty
type nonrec bar = string
type nonrec t = [%rescript.typehole ]
type nonrec state = [%rescript.typehole ]
type nonrec state = [%rescript.typehole ]
type nonrec User = string
3 changes: 3 additions & 0 deletions tests/parsing/errors/typeDef/typeDef.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ type t =

// missing type
type state =

// name cannot be capitalized
type User = string