Skip to content

Commit 2b8f4a5

Browse files
committed
Allow tuples in untagged variants
fixes #6544
1 parent 6dd6465 commit 2b8f4a5

10 files changed

+55
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- GenType: support `@deriving(accessors)` outputs. https://github.com/rescript-lang/rescript-compiler/pull/6537
1818
- 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
19+
- Allow tuples in untagged variants https://github.com/rescript-lang/rescript-compiler/pull/6550
1920

2021
#### :bug: Bug Fix
2122

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/UntaggedTupleAndArray.res:4:3-21
4+
5+
2 │ type t =
6+
3 │ | Array(array<int>)
7+
4 │ | Tuple((int, int))
8+
5 │
9+
10+
This untagged variant definition is invalid: At most one case can be an array or tuple type.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

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

55
1 │ @unboxed
6-
2 │ type t = Tuple((float, string)) | Float(float)
6+
2 │ type t = List(list<float>) | Float(float)
77
3 │
88

9-
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.
9+
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 numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@unboxed
2+
type t =
3+
| Array(array<int>)
4+
| Tuple((int, int))
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
@unboxed
2-
type t = Tuple((float, string)) | Float(float)
2+
type t = List(list<float>) | Float(float)

jscomp/ml/ast_untagged_variants.ml

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ let report_error ppf =
4747
(match untaggedVariant with
4848
| 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."
4949
| AtMostOneObject -> "At most one case can be an object type."
50+
| AtMostOneInstance Array -> "At most one case can be an array or tuple type."
5051
| AtMostOneInstance i -> "At most one case can be a " ^ (Instance.to_string i) ^ " type."
5152
| AtMostOneFunction -> "At most one case can be a function type."
5253
| AtMostOneString -> "At most one case can be a string type."
@@ -183,6 +184,7 @@ let get_block_type_from_typ ~env (t: Types.type_expr) : block_type option =
183184
(match type_to_instanceof_backed_obj t with
184185
| None -> None
185186
| Some instanceType -> Some (InstanceType instanceType))
187+
| {desc = Ttuple _} -> Some (InstanceType Array)
186188
| _ -> None
187189

188190
let get_block_type ~env (cstr : Types.constructor_declaration) :

jscomp/test/UntaggedVariants.js

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/variantsMatching.gen.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ export type MyNullable_t<a> = null | undefined | a;
1616
export type MyNullableExtended_t<a> = null | undefined | "WhyNotAnotherOne" | a;
1717

1818
export type UntaggedWithBool_t = string | number | boolean | string;
19+
20+
export type UntaggedWithTuple_t = string | [number, number, string];

jscomp/test/variantsMatching.js

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/variantsMatching.res

+11
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,14 @@ module UntaggedWithBool = {
286286
| Object({name}) => "Object" ++ name
287287
}
288288
}
289+
290+
module UntaggedWithTuple = {
291+
@unboxed @genType
292+
type t = String(string) | Tuple((int, float, string))
293+
294+
let classify = x =>
295+
switch x {
296+
| String(_) => "string"
297+
| Tuple(_) => "tuple"
298+
}
299+
}

0 commit comments

Comments
 (0)