Skip to content

Example of complex pattern for untagged variant. #6240

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

Merged
merged 5 commits into from
Jun 22, 2023
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 @@ -14,6 +14,7 @@

#### :rocket: New Feature
- Untagged variants: consider regexp as an object type. https://github.com/rescript-lang/rescript-compiler/pull/6296
- Semantic-based optimization of code generated for untagged variants https://github.com/rescript-lang/rescript-compiler/issues/6108

# 11.0.0-beta.2

Expand Down
36 changes: 36 additions & 0 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,33 @@ let bin ?comment (op : J.binop) (e0 : t) (e1 : t) : t =
be careful for side effect
*)

let rec filter_bool (e: t) ~j ~b = match e.expression_desc with
| Bin (And, e1, e2) ->
(match (filter_bool e1 ~j ~b, filter_bool e2 ~j ~b) with
| None, None -> None
| Some e, None
| None, Some e -> Some e
| Some e1, Some e2 ->
Some {e with expression_desc = Bin (And, e1, e2)} )
| Bin (Or, e1, e2) ->
(match (filter_bool e1 ~j ~b, filter_bool e2 ~j ~b) with
| None, _ | _, None ->
None
| Some e1, Some e2 ->
Some {e with expression_desc = Bin (Or, e1, e2)} )
| Bin
( NotEqEq,
{expression_desc = Typeof {expression_desc = Var i}},
{expression_desc = Str {txt}}) when Js_op_util.same_vident i j ->
if txt <> "bool"
then None
else assert false
| Js_not {expression_desc =
Call ({expression_desc = Str {txt = "Array.isArray"}},
[{expression_desc = Var i}], _)} when Js_op_util.same_vident i j ->
None
| _ -> Some e

let and_ ?comment (e1 : t) (e2 : t) : t =
match (e1.expression_desc, e2.expression_desc) with
| Var i, Var j when Js_op_util.same_vident i j -> e1
Expand All @@ -634,6 +661,15 @@ let and_ ?comment (e1 : t) (e2 : t) : t =
{ expression_desc = Str _ | Number _ } ) )
when Js_op_util.same_vident i j ->
e2
| ( _,
Bin
( EqEqEq,
{ expression_desc = Var j },
{ expression_desc = Bool b } )
) ->
(match filter_bool e1 ~j ~b with
| None -> e2
| Some e1 -> { expression_desc = Bin (And, e1, e2); comment })
| _, _ -> { expression_desc = Bin (And, e1, e2); comment }

let or_ ?comment (e1 : t) (e2 : t) =
Expand Down
75 changes: 75 additions & 0 deletions jscomp/test/UntaggedVariants.js

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

30 changes: 30 additions & 0 deletions jscomp/test/UntaggedVariants.res
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,34 @@ module TestFunctionCase = {
}

let ff = Function((. x) => x+1)
}

module ComplexPattern = {
@unboxed
type rec t =
| @as(undefined) Missing
| @as(false) False
| @as(true) True
| @as(null) Null
| String(string)
| Number(float)
| Object(Js.Dict.t<t>)
| Array(array<t>)

type tagged_t =
| JSONFalse
| JSONTrue
| JSONNull
| JSONString(string)
| JSONNumber(float)
| JSONObject(Js.Dict.t<t>)
| JSONArray(array<t>)

let someJson: t = %raw(`'[{"name": "Haan"}, {"name": "Mr"}, false]'`)->Obj.magic

let check = s =>
switch s {
| Array([True, False, Array([String("My name is"), Number(10.)])]) => Js.log("yup")
| _ => Js.log("Nope...")
}
}