Skip to content

Commit

Permalink
[flow] consider RArrayPatternRestProp fresh
Browse files Browse the repository at this point in the history
Summary:
Before this diff,
```
declare var arr :  [number];
[...arr] = [1 as 1];
```
was an error. The reason has to do with the type we would infer for an array type emerging from the rest operation. This type was checked against the defined type of `arr`.

The former type was an array type with `1` as its element, and `RArrayPatternRestProp` as the reason on the array.

`RArrayPatternRestProp` is not a "literal" reason, meaning that it will not short-circuit an Array-to-Array check to a co-variant check on their element types.

The invariant check the follows would cause an error on the side of `number <: 1`.

This diff fixes this by treating the inferred type as a freshly generated array literal, which is consistent with other source of array literal freshness. Specifically, it does so by considering the relevant reason `RArrayPatternRestProp` a literal reason.

Changelog: [fix] fixed a bug that caused spurious errors on rest array assignments (e.g. [try-Flow](https://flow.org/try/#1N4Igxg9gdgZglgcxALlAIwIZoKYBsD6uEEAztvhgE6UYCe+JADpdhgCYowa5kA0I2KAFcAtiRQAXSkOz9sADwxgJ+NPTbYuQ3BMnTZA+Y2yU4IwRO4A6SFBIrGVDGM7c+h46fNRLuKxJIGWh8MeT0ZfhYlCStpHzNsFBAMIQkIEQwJODAQfiEyfBE4eWw2fDgofDBMsAALfAA3KjgsXGxxZC4eAw0G-GhcWn9aY3wWZldu-g1mbGqJUoBaCRHEzrcDEgBrbAk62kXhXFxJ923d-cPRHEpTgyEoMDaqZdW7vKgoOfaSKgOKpqmDA+d4gB5fMA-P6LCCMLLQbiLOoYCqgh6-GDYRYIXYLSgkRZkCR4jpddwPfJLZjpOBkO4AX34kA0SQ0Tyo2AABIDOVRKMhOQBtYQiG4AH3spigCAAugBuAA6UEFVlVfJlnIAvEKAIzyzkAegNnIA7nBjpyoBAJJycLyoJyTJQIJR7bROSIXdglbkQA0TCQ4NAkg0AAxWABMADYdZGQPSgA)).

Reviewed By: SamChou19815

Differential Revision: D69941196

fbshipit-source-id: 0fc9ca8e2bb5c0d30c4e3c1a29372fd5961b2337
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Feb 21, 2025
1 parent 24ea287 commit e89fad4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/typing/subtyping_kit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,8 @@ module Make (Flow : INPUT) : OUTPUT = struct
| RArrayLit
| REmptyArrayLit
| RRestArrayLit _
| RReactChildren ->
| RReactChildren
| RArrayPatternRestProp ->
true
| _ -> false
in
Expand Down
4 changes: 4 additions & 0 deletions tests/destructuring/array_rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ let d = zs[1]; // run off the end
(d: void); // error: number|string|boolean ~> void

let [...e] = 0;

declare var arr : [number|string];
[...arr] = [1]; // okay
[...arr] = [true]; // error boolean ~> number|string
24 changes: 23 additions & 1 deletion tests/destructuring/destructuring.exp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,28 @@ References:
^ [1]


Error ---------------------------------------------------------------------------------------------- array_rest.js:16:12

Cannot assign array literal to `arr` because in index 0: [incompatible-type]
- Either boolean [1] is incompatible with number [2].
- Or boolean [1] is incompatible with string [3].

array_rest.js:16:12
16| [...arr] = [true]; // error boolean ~> number|string
^^^^^^

References:
array_rest.js:16:13
16| [...arr] = [true]; // error boolean ~> number|string
^^^^ [1]
array_rest.js:14:21
14| declare var arr : [number|string];
^^^^^^ [2]
array_rest.js:14:28
14| declare var arr : [number|string];
^^^^^^ [3]


Error -------------------------------------------------------------------------------------------------- computed.js:2:2

Cannot cast `val1` to undefined because string [1] is incompatible with undefined [2]. [incompatible-cast]
Expand Down Expand Up @@ -1581,7 +1603,7 @@ References:



Found 111 errors
Found 112 errors

Only showing the most relevant union/intersection branches.
To see all branches, re-run Flow with --show-all-branches

0 comments on commit e89fad4

Please sign in to comment.