Skip to content

Commit

Permalink
[flow][tuples] Allow tuple type spread of tuple-like arrays
Browse files Browse the repository at this point in the history
Summary:
Changelog: [internal]
Allow tuple type spread of tuple-like arrays. This is required for the next diff to work - reversal of tuple type spread.

Reviewed By: SamChou19815

Differential Revision: D55721534

fbshipit-source-id: e856bf2965ade26bc201782fe8da5d62cacc4d8b
  • Loading branch information
gkz authored and facebook-github-bot committed Apr 4, 2024
1 parent 4cd054e commit 2fd9e51
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,7 @@ struct
| DefT (_, ArrT arrtype) ->
(* Arrays *)
(match (rrt_resolve_to, arrtype) with
| (ResolveSpreadsToTupleType _, (ArrayAT _ | ROArrayAT _)) ->
| (ResolveSpreadsToTupleType _, (ArrayAT { tuple_view = None; _ } | ROArrayAT _)) ->
(* Only tuples can be spread into tuple types. *)
add_output
cx
Expand Down
12 changes: 12 additions & 0 deletions tests/tuples/spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type ArraySpread = [...Array<mixed>]; // ERROR
declare const x: ArraySpread;
x as empty; // OK
}
type ROArraySpread = [...$ReadOnlyArray<mixed>]; // ERROR
{
declare const x: ROArraySpread;
x as empty; // OK
}

// Spreading `any`
type AnySpread = [1, 2, ...any]; // OK
Expand All @@ -69,6 +74,13 @@ type EmptySpread = [1, 2, ...empty]; // OK
x as empty; // OK
}

// Tuple-like array spread
{
const x = [1, 2];
type TupleArrSpread = [0, ...typeof x]; // OK
[0, 1, 2] as TupleArrSpread; // OK
}

// Generics
type WithTail<T> = [1, ...T];
[1, 2, 3] as WithTail<[2, 3]>; // OK
Expand Down
40 changes: 27 additions & 13 deletions tests/tuples/tuples.exp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,36 +1182,50 @@ References:
^^^^^^^^^^^^ [1]


Error --------------------------------------------------------------------------------------------------- spread.js:75:8
Error -------------------------------------------------------------------------------------------------- spread.js:57:22

Cannot spread non-tuple (read-only array type [1]) into tuple type. [tuple-invalid-type-spread]

spread.js:57:22
57| type ROArraySpread = [...$ReadOnlyArray<mixed>]; // ERROR
^^^^^^^^^^^^^^^^^^^^^^^^^^

References:
spread.js:57:26
57| type ROArraySpread = [...$ReadOnlyArray<mixed>]; // ERROR
^^^^^^^^^^^^^^^^^^^^^ [1]


Error --------------------------------------------------------------------------------------------------- spread.js:87:8

Cannot cast array literal to `WithTail` because number [1] is incompatible with number literal `3` [2] in index 2.
[incompatible-cast]

spread.js:75:8
75| [1, 2, 66] as WithTail<[2, 3]>; // ERROR
spread.js:87:8
87| [1, 2, 66] as WithTail<[2, 3]>; // ERROR
^^ [1]

References:
spread.js:75:28
75| [1, 2, 66] as WithTail<[2, 3]>; // ERROR
spread.js:87:28
87| [1, 2, 66] as WithTail<[2, 3]>; // ERROR
^ [2]


Error --------------------------------------------------------------------------------------------------- spread.js:78:1
Error --------------------------------------------------------------------------------------------------- spread.js:90:1

Cannot call `f` because `T` [1] is underconstrained by call of `f` [2]. Either add explicit type arguments or cast the
expression to your expected type. [underconstrained-implicit-instantiation]

spread.js:78:1
78| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented
spread.js:90:1
90| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented
^

References:
spread.js:77:20
77| declare function f<T>(xs: [1, ...T]): void;
spread.js:89:20
89| declare function f<T>(xs: [1, ...T]): void;
^ [1]
spread.js:78:1
78| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented
spread.js:90:1
90| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented
^^^^^^^^^^^^ [2]


Expand Down Expand Up @@ -1984,4 +1998,4 @@ References:



Found 124 errors
Found 125 errors

0 comments on commit 2fd9e51

Please sign in to comment.