Skip to content

Commit

Permalink
[flow][match] Fix match refinement of arrays with tuple views
Browse files Browse the repository at this point in the history
Summary:
One day natural inference will mean we don't need to think about this, but until then we need to handle it.

Array literals initialized with some values act as a tuple. Handle them like we handle tuples.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D67778713

fbshipit-source-id: a2b7bad3c8a02874027f82e2712cf797adc19275
  • Loading branch information
gkz authored and facebook-github-bot committed Jan 7, 2025
1 parent e40b83f commit 447a252
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/typing/type_filter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,12 @@ let not_array t =

let array_length ~sense ~op ~n t =
match t with
| DefT (_, ArrT (TupleAT { arity = (num_req, num_total); inexact; _ })) ->
| DefT (_, ArrT (TupleAT { arity = (num_req, num_total); inexact; _ }))
| DefT
( _,
ArrT
(ArrayAT { tuple_view = Some (TupleView { arity = (num_req, num_total); inexact; _ }); _ })
) ->
(* `None` represents "maybe" a match *)
let matches =
match op with
Expand Down
9 changes: 9 additions & 0 deletions tests/match/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,12 @@
_: 0,
};
}

// Array pattern applied to array literal
{
const x = [1, 'foo'];

const out = match (x) {
[_, const b]: b as string, // OK
};
}

0 comments on commit 447a252

Please sign in to comment.