Skip to content

Allow tuples in untagged variants #6550

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 1 commit into from
Jan 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 @@ -16,6 +16,7 @@

- GenType: support `@deriving(accessors)` outputs. https://github.com/rescript-lang/rescript-compiler/pull/6537
- Allow coercing ints and floats to unboxed variants that have a catch-all unboxed int or float case. https://github.com/rescript-lang/rescript-compiler/pull/6540
- Allow tuples in untagged variants https://github.com/rescript-lang/rescript-compiler/pull/6550

#### :bug: Bug Fix

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

We've found a bug for you!
/.../fixtures/UntaggedTupleAndArray.res:4:3-21

2 │ type t =
3 │ | Array(array<int>)
4 │ | Tuple((int, int))
5 │

This untagged variant definition is invalid: At most one case can be an array or tuple type.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

We've found a bug for you!
/.../fixtures/UntaggedUnknown.res:2:10-31
/.../fixtures/UntaggedUnknown.res:2:10-26

1 │ @unboxed
2 │ type t = Tuple((float, string)) | Float(float)
2 │ type t = List(list<float>) | Float(float)
3 │

This untagged variant definition is invalid: Case Tuple has a payload that is not of one of the recognized shapes (object, array, etc). Then it must be the only case with payloads.
This untagged variant definition is invalid: Case List has a payload that is not of one of the recognized shapes (object, array, etc). Then it must be the only case with payloads.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@unboxed
type t =
| Array(array<int>)
| Tuple((int, int))
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@unboxed
type t = Tuple((float, string)) | Float(float)
type t = List(list<float>) | Float(float)
2 changes: 2 additions & 0 deletions jscomp/ml/ast_untagged_variants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ let report_error ppf =
(match untaggedVariant with
| OnlyOneUnknown name -> "Case " ^ name ^ " has a payload that is not of one of the recognized shapes (object, array, etc). Then it must be the only case with payloads."
| AtMostOneObject -> "At most one case can be an object type."
| AtMostOneInstance Array -> "At most one case can be an array or tuple type."
| AtMostOneInstance i -> "At most one case can be a " ^ (Instance.to_string i) ^ " type."
| AtMostOneFunction -> "At most one case can be a function type."
| AtMostOneString -> "At most one case can be a string type."
Expand Down Expand Up @@ -183,6 +184,7 @@ let get_block_type_from_typ ~env (t: Types.type_expr) : block_type option =
(match type_to_instanceof_backed_obj t with
| None -> None
| Some instanceType -> Some (InstanceType instanceType))
| {desc = Ttuple _} -> Some (InstanceType Array)
| _ -> None

let get_block_type ~env (cstr : Types.constructor_declaration) :
Expand Down
16 changes: 8 additions & 8 deletions jscomp/test/UntaggedVariants.js

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

2 changes: 2 additions & 0 deletions jscomp/test/variantsMatching.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export type MyNullable_t<a> = null | undefined | a;
export type MyNullableExtended_t<a> = null | undefined | "WhyNotAnotherOne" | a;

export type UntaggedWithBool_t = string | number | boolean | string;

export type UntaggedWithTuple_t = string | [number, number, string];
13 changes: 13 additions & 0 deletions jscomp/test/variantsMatching.js

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

11 changes: 11 additions & 0 deletions jscomp/test/variantsMatching.res
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,14 @@ module UntaggedWithBool = {
| Object({name}) => "Object" ++ name
}
}

module UntaggedWithTuple = {
@unboxed @genType
type t = String(string) | Tuple((int, float, string))

let classify = x =>
switch x {
| String(_) => "string"
| Tuple(_) => "tuple"
}
}