Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested variant type spreads #7080

Merged
merged 4 commits into from
Oct 7, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- Fix attribute printing. https://github.com/rescript-lang/rescript-compiler/pull/7025
- Fix "rescript format" with many files. https://github.com/rescript-lang/rescript-compiler/pull/7081
- Fix bigint max, min https://github.com/rescript-lang/rescript-compiler/pull/7088
- Fix parsing issue with nested variant pattern type spreads. https://github.com/rescript-lang/rescript-compiler/pull/7080

#### :nail_care: Polish

Expand Down
3 changes: 2 additions & 1 deletion compiler/syntax/src/res_grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ let is_structure_item_start = function
let is_pattern_start = function
| Token.Int _ | Float _ | String _ | Codepoint _ | Backtick | True | False
| Minus | Plus | Lparen | Lbracket | Lbrace | List | Dict | Underscore
| Lident _ | Uident _ | Hash | Exception | Percent | Module | At ->
| DotDotDot | Lident _ | Uident _ | Hash | Exception | Percent | Module | At
->
true
| _ -> false

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
type nonrec a =
| One
| Two
| Three
type nonrec b =
| ... of a
| Four
| Five
type nonrec c =
| Six
| Seven
type nonrec d =
| ... of b
| ... of c
let doWithA =
((Function$
(fun (a : a) ->
((match a with
| One -> Js.log {js|aaa|js}
| Two -> Js.log {js|twwwoooo|js}
| Three -> Js.log {js|threeeee|js})
[@res.braces ])))
[@res.arity 1])
let doWithB =
((Function$
(fun (b : b) ->
((match b with
| One -> Js.log {js|aaa|js}
| _ -> Js.log {js|twwwoooo|js})
[@res.braces ])))
[@res.arity 1])
let lookup =
((Function$
(fun (b : b) ->
match b with
| ((#a)[@res.patVariantSpread ]) as a -> doWithA a
| Four -> Js.log {js|four|js}
| Five -> Js.log {js|five|js}))
[@res.arity 1])
let lookup2 =
((Function$
(fun (d : d) ->
match d with
| ((#a)[@res.patVariantSpread ]) as a -> doWithA a
| ((#b)[@res.patVariantSpread ]) as b -> doWithB b
| Six|Seven -> Js.log {js|Got rest of d|js}))
[@res.arity 1])
let lookupOpt =
((Function$
(fun (b : b option) ->
match b with
| Some (((#a)[@res.patVariantSpread ]) as a) -> doWithA a
| Some (Four) -> Js.log {js|four|js}
| Some (Five) -> Js.log {js|five|js}
| None -> Js.log {js|None|js}))
[@res.arity 1])
module Foo =
struct
type nonrec zz =
| First
| Second
type nonrec xx =
| ... of zz
| Third
end
let doWithZ =
((Function$
(fun (z : Foo.zz) ->
match z with
| First -> Js.log {js|First|js}
| Second -> Js.log {js|Second|js}))
[@res.arity 1])
let lookup3 =
((Function$
(fun (d : Foo.xx) ->
match d with
| ((#Foo.zz)[@res.patVariantSpread ]) as z -> Js.log z
| Third -> Js.log {js|Third|js}))
[@res.arity 1])
59 changes: 59 additions & 0 deletions compiler/syntax/tests/parsing/grammar/pattern/variantSpreads.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
type a = One | Two | Three
type b = | ...a | Four | Five
type c = Six | Seven
type d = | ...b | ...c

let doWithA = (a: a) => {
switch a {
| One => Js.log("aaa")
| Two => Js.log("twwwoooo")
| Three => Js.log("threeeee")
}
}

let doWithB = (b: b) => {
switch b {
| One => Js.log("aaa")
| _ => Js.log("twwwoooo")
}
}

let lookup = (b: b) =>
switch b {
| ...a as a => doWithA(a)
| Four => Js.log("four")
| Five => Js.log("five")
}

let lookup2 = (d: d) =>
switch d {
| ...a as a => doWithA(a)
| ...b as b => doWithB(b)
| Six | Seven => Js.log("Got rest of d")
}

let lookupOpt = (b: option<b>) =>
switch b {
| Some(...a as a) => doWithA(a)
| Some(Four) => Js.log("four")
| Some(Five) => Js.log("five")
| None => Js.log("None")
}


module Foo = {
type zz = First | Second
type xx = | ...zz | Third
}

let doWithZ = (z: Foo.zz) =>
switch z {
| First => Js.log("First")
| Second => Js.log("Second")
}

let lookup3 = (d: Foo.xx) =>
switch d {
| ...Foo.zz as z => Js.log(z)
| Third => Js.log("Third")
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@


Syntax error!
tests/parsing/recovery/pattern/list.res:4:13
tests/parsing/recovery/pattern/list.res:4:9-11

2 │ | list{} => ()
3 │ | list{1, list{} => ()
4 │ | list{}...1, ...list{3, 4} => ()
5 │ }
6 │

I'm not sure what to parse here when looking at ",".
Did you forget a `}` here?

;;match x with | [] -> () | 1::[]::[] -> () | [] -> 1
;;match x with | [] -> () | 1::[]::[] -> () | [] -> [%rescript.exprhole ]
;;1
;;[3; 4]
;;()
44 changes: 44 additions & 0 deletions tests/tests/src/VariantPatternMatchingSpreads.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/tests/src/VariantPatternMatchingSpreads.res
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,28 @@ let lookup2 = (d: d) =>
| ...b as b => doWithB(b)
| Six | Seven => Js.log("Got rest of d")
}

let lookupOpt = (b: option<b>) =>
switch b {
| Some(...a as a) => doWithA(a)
| Some(Four) => Js.log("four")
| Some(Five) => Js.log("five")
| None => Js.log("None")
}

module Foo = {
type zz = First | Second
type xx = | ...zz | Third
}

let doWithZ = (z: Foo.zz) =>
switch z {
| First => Js.log("First")
| Second => Js.log("Second")
}

let lookup3 = (d: Foo.xx) =>
switch d {
| ...Foo.zz as z => Js.log(z)
| Third => Js.log("Third")
}