diff --git a/src/res_core.ml b/src/res_core.ml index 211e4479..eaa92635 100644 --- a/src/res_core.ml +++ b/src/res_core.ml @@ -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 @@ -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 @@ -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) | _ -> @@ -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) | _ -> diff --git a/tests/parsing/errors/other/expected/hashIdent.res.txt b/tests/parsing/errors/other/expected/hashIdent.res.txt new file mode 100644 index 00000000..7d410d53 --- /dev/null +++ b/tests/parsing/errors/other/expected/hashIdent.res.txt @@ -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`? + + + 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 -> () \ No newline at end of file diff --git a/tests/parsing/errors/other/hashIdent.res b/tests/parsing/errors/other/hashIdent.res new file mode 100644 index 00000000..f7c78e24 --- /dev/null +++ b/tests/parsing/errors/other/hashIdent.res @@ -0,0 +1,7 @@ +let x = #10s + +type t = [ #red | #10s ] + +switch x { +| #10s => () +} diff --git a/tests/printer/typexpr/expected/variant.res.txt b/tests/printer/typexpr/expected/variant.res.txt index d9623d84..cff7a76d 100644 --- a/tests/printer/typexpr/expected/variant.res.txt +++ b/tests/printer/typexpr/expected/variant.res.txt @@ -112,3 +112,6 @@ type t = [ | #1(string) | #2(int, string) ] + +// don't pick int with suffix as numeric polyvar +type t = [#"10s" | #"20t"] diff --git a/tests/printer/typexpr/variant.res b/tests/printer/typexpr/variant.res index 353338d7..26c8247b 100644 --- a/tests/printer/typexpr/variant.res +++ b/tests/printer/typexpr/variant.res @@ -106,3 +106,6 @@ type t = [ | #1(string) | #2(int, string) ] + +// don't pick int with suffix as numeric polyvar +type t = [#"10s" | #"20t" ]