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

Don't parse Int token with suffices as hash ident for poly variants #408

Merged
merged 5 commits into from
May 13, 2021
Merged
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
19 changes: 16 additions & 3 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Explanation: since records have a known, fixed shape, a spread like `{a, ...b}`
Explanation: lists are singly-linked list, where a node contains a value and points to the next node. `list[a, ...bc]` efficiently creates a new item and links `bc` as its next nodes. `[...bc, a]` would be expensive, as it'd need to traverse `bc` and prepend each item to `a` one by one. We therefore disallow such syntax sugar.\n\
Solution: directly use `concat`."

let variantIdent = "A polymorphic variant (e.g. #id) must start with an alphabetical letter."
let variantIdent = "A polymorphic variant (e.g. #id) must start with an alphabetical letter or be a number (e.g. #742)"

let experimentalIfLet expr =
let switchExpr = {expr with Parsetree.pexp_attributes = []} in
Expand Down Expand Up @@ -124,6 +124,9 @@ Solution: directly use `concat`."

let sameTypeSpread =
"You're using a ... spread without extra fields. This is the same type."

let polyVarIntWithSuffix number =
"A numeric polymorphic variant cannot be followed by a letter. Did you mean `#" ^ number ^ "`?"
end


Expand Down Expand Up @@ -696,7 +699,12 @@ let parseHashIdent ~startPos p =
Parser.next p;
let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in
(text, mkLoc startPos p.prevEndPos)
| Int {i} ->
| Int {i; suffix} ->
let () = match suffix with
| Some _ ->
Parser.err p (Diagnostics.message (ErrorMessages.polyVarIntWithSuffix i))
| None -> ()
in
Parser.next p;
(i, mkLoc startPos p.prevEndPos)
| _ ->
Expand Down Expand Up @@ -1202,7 +1210,12 @@ let rec parsePattern ?(alias=true) ?(or_=true) p =
Parser.next p;
let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in
(text, mkLoc startPos p.prevEndPos)
| Int {i} ->
| Int {i; suffix} ->
let () = match suffix with
| Some _ ->
Parser.err p (Diagnostics.message (ErrorMessages.polyVarIntWithSuffix i))
| None -> ()
in
Parser.next p;
(i, mkLoc startPos p.prevEndPos)
| _ ->
Expand Down
37 changes: 37 additions & 0 deletions tests/parsing/errors/other/expected/hashIdent.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Syntax error!
tests/parsing/errors/other/hashIdent.res:1:10-12

1 │ let x = #10s
2 │
3 │ type t = [ #red | #10s ]

A numeric polymorphic variant cannot be followed by a letter. Did you mean `#10`?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bloodyowl what do you think of this new message? Specialized it to give a clear suggestion on what the user could do in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that looks better 👍



Syntax error!
tests/parsing/errors/other/hashIdent.res:3:20-22

1 │ let x = #10s
2 │
3 │ type t = [ #red | #10s ]
4 │
5 │ switch x {

A numeric polymorphic variant cannot be followed by a letter. Did you mean `#10`?


Syntax error!
tests/parsing/errors/other/hashIdent.res:6:4-6

4 │
5 │ switch x {
6 │ | #10s => ()
7 │ }
8 │

A numeric polymorphic variant cannot be followed by a letter. Did you mean `#10`?

let x = `10
type nonrec t = [ `red | `10 ]
;;match x with | `10 -> ()
7 changes: 7 additions & 0 deletions tests/parsing/errors/other/hashIdent.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let x = #10s

type t = [ #red | #10s ]

switch x {
| #10s => ()
}
3 changes: 3 additions & 0 deletions tests/printer/typexpr/expected/variant.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ type t = [
| #1(string)
| #2(int, string)
]

// don't pick int with suffix as numeric polyvar
type t = [#"10s" | #"20t"]
3 changes: 3 additions & 0 deletions tests/printer/typexpr/variant.res
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,6 @@ type t = [
| #1(string)
| #2(int, string)
]

// don't pick int with suffix as numeric polyvar
type t = [#"10s" | #"20t" ]