Skip to content

Commit

Permalink
[flow][tuples] Refactor: update ETupleArityMismatch error to use inli…
Browse files Browse the repository at this point in the history
…ne record

Summary:
Update ETupleArityMismatch error to use inline record.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D56713881

fbshipit-source-id: c96188e31575256deeb36958423001a13f0e0b77
  • Loading branch information
gkz authored and facebook-github-bot committed Apr 29, 2024
1 parent 98ff96c commit 579754f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 26 deletions.
12 changes: 9 additions & 3 deletions src/typing/debug_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1393,11 +1393,17 @@ let dump_error_message =
| ENonStrictEqualityComparison (reason1, reason2) ->
spf "ENonStrictEqualityComparison (%s, %s)" (dump_reason cx reason1) (dump_reason cx reason2)
| ETupleArityMismatch
((reason1, reason2), (num_req1, num_total1), (num_req2, num_total2), use_op) ->
{
use_op;
lower_reason;
lower_arity = (num_req1, num_total1);
upper_reason;
upper_arity = (num_req2, num_total2);
} ->
spf
"ETupleArityMismatch (%s, %s, %d-%d, %d-%d, %s)"
(dump_reason cx reason1)
(dump_reason cx reason2)
(dump_reason cx lower_reason)
(dump_reason cx upper_reason)
num_req1
num_total1
num_req2
Expand Down
32 changes: 24 additions & 8 deletions src/typing/errors/error_message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ and 'loc t' =
}
| EComparison of ('loc virtual_reason * 'loc virtual_reason)
| ENonStrictEqualityComparison of ('loc virtual_reason * 'loc virtual_reason)
| ETupleArityMismatch of
('loc virtual_reason * 'loc virtual_reason) * (int * int) * (int * int) * 'loc virtual_use_op
| ETupleArityMismatch of {
use_op: 'loc virtual_use_op;
lower_reason: 'loc virtual_reason;
lower_arity: int * int;
upper_reason: 'loc virtual_reason;
upper_arity: int * int;
}
| ENonLitArrayToTuple of ('loc virtual_reason * 'loc virtual_reason) * 'loc virtual_use_op
| ETupleOutOfBounds of {
use_op: 'loc virtual_use_op;
Expand Down Expand Up @@ -829,8 +834,15 @@ let rec map_loc_of_error_message (f : 'a -> 'b) : 'a t' -> 'b t' =
EPrivateLookupFailed ((map_reason r1, map_reason r2), x, map_use_op op)
| EPlatformSpecificImplementationModuleLookupFailed { loc; name } ->
EPlatformSpecificImplementationModuleLookupFailed { loc = f loc; name }
| ETupleArityMismatch ((r1, r2), l, i, op) ->
ETupleArityMismatch ((map_reason r1, map_reason r2), l, i, map_use_op op)
| ETupleArityMismatch { use_op; lower_reason; lower_arity; upper_reason; upper_arity } ->
ETupleArityMismatch
{
use_op = map_use_op use_op;
lower_reason = map_reason lower_reason;
lower_arity;
upper_reason = map_reason upper_reason;
upper_arity;
}
| ENonLitArrayToTuple ((r1, r2), op) ->
ENonLitArrayToTuple ((map_reason r1, map_reason r2), map_use_op op)
| ETupleOutOfBounds { use_op; reason; reason_op; length; index } ->
Expand Down Expand Up @@ -1346,7 +1358,10 @@ let util_use_op_of_msg nope util = function
| EPropPolarityMismatch (rs, p, ps, op) ->
util op (fun op -> EPropPolarityMismatch (rs, p, ps, op))
| EPrivateLookupFailed (rs, x, op) -> util op (fun op -> EPrivateLookupFailed (rs, x, op))
| ETupleArityMismatch (rs, x, y, op) -> util op (fun op -> ETupleArityMismatch (rs, x, y, op))
| ETupleArityMismatch { use_op; lower_reason; lower_arity; upper_reason; upper_arity } ->
util use_op (fun use_op ->
ETupleArityMismatch { use_op; lower_reason; lower_arity; upper_reason; upper_arity }
)
| ENonLitArrayToTuple (rs, op) -> util op (fun op -> ENonLitArrayToTuple (rs, op))
| ETupleOutOfBounds { use_op; reason; reason_op; length; index } ->
util use_op (fun use_op -> ETupleOutOfBounds { use_op; reason; reason_op; length; index })
Expand Down Expand Up @@ -2136,11 +2151,12 @@ let friendly_message_of_msg = function
| EComparison (lower, upper) -> Normal (MessageCannotCompare { lower; upper })
| ENonStrictEqualityComparison (lower, upper) ->
Normal (MessageCannotCompareNonStrict { lower; upper })
| ETupleArityMismatch ((lower, upper), lower_arity, upper_arity, use_op) ->
| ETupleArityMismatch { use_op; lower_reason; lower_arity; upper_reason; upper_arity } ->
UseOp
{
loc = loc_of_reason lower;
message = MessageIncompatibleTupleArity { lower; lower_arity; upper; upper_arity };
loc = loc_of_reason lower_reason;
message =
MessageIncompatibleTupleArity { lower_reason; lower_arity; upper_reason; upper_arity };
use_op;
explanation = None;
}
Expand Down
6 changes: 3 additions & 3 deletions src/typing/errors/flow_intermediate_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2701,7 +2701,7 @@ let to_printable_error :
text " of ";
ref upper;
]
| MessageIncompatibleTupleArity { lower; lower_arity; upper; upper_arity } ->
| MessageIncompatibleTupleArity { lower_reason; lower_arity; upper_reason; upper_arity } ->
let str_of_arity (num_req, num_total) =
if num_req = num_total then
if num_total = 1 then
Expand All @@ -2712,11 +2712,11 @@ let to_printable_error :
spf "%d-%d elements" num_req num_total
in
[
ref lower;
ref lower_reason;
text " has ";
text (str_of_arity lower_arity);
text " but ";
ref upper;
ref upper_reason;
text " has ";
text (str_of_arity upper_arity);
]
Expand Down
4 changes: 2 additions & 2 deletions src/typing/errors/flow_intermediate_error_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,9 @@ type 'loc message =
upper_arity: int;
}
| MessageIncompatibleTupleArity of {
lower: 'loc virtual_reason;
lower_reason: 'loc virtual_reason;
lower_arity: int * int;
upper: 'loc virtual_reason;
upper_reason: 'loc virtual_reason;
upper_arity: int * int;
}
| MessageIncompatibleImplicitReturn of {
Expand Down
14 changes: 9 additions & 5 deletions src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9423,20 +9423,24 @@ struct
array_unify cx trace ~use_op (ts1, t1, ts2, t2)
| ( DefT
( r1,
ArrT (TupleAT { elem_t = _; elements = elements1; arity = arity1; react_dro = _ })
ArrT
(TupleAT { elem_t = _; elements = elements1; arity = lower_arity; react_dro = _ })
),
DefT
( r2,
ArrT (TupleAT { elem_t = _; elements = elements2; arity = arity2; react_dro = _ })
ArrT
(TupleAT { elem_t = _; elements = elements2; arity = upper_arity; react_dro = _ })
)
) ->
let (num_req1, num_total1) = arity1 in
let (num_req2, num_total2) = arity2 in
let (num_req1, num_total1) = lower_arity in
let (num_req2, num_total2) = upper_arity in
if num_req1 <> num_req2 || num_total1 <> num_total2 then
add_output
cx
~trace
(Error_message.ETupleArityMismatch ((r1, r2), arity1, arity2, use_op));
(Error_message.ETupleArityMismatch
{ use_op; lower_reason = r1; lower_arity; upper_reason = r2; upper_arity }
);
let n = ref 0 in
iter2opt
(fun t1 t2 ->
Expand Down
21 changes: 16 additions & 5 deletions src/typing/subtyping_kit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1848,8 +1848,14 @@ module Make (Flow : INPUT) : OUTPUT = struct
in
array_flow cx trace use_op lit1 r1 (ts1, t1, ts2, t2)
(* Tuples can flow to tuples with the same arity *)
| ( DefT (r1, ArrT (TupleAT { elem_t = _; elements = elements1; arity = arity1; react_dro = _ })),
DefT (r2, ArrT (TupleAT { elem_t = _; elements = elements2; arity = arity2; react_dro = _ }))
| ( DefT
( r1,
ArrT (TupleAT { elem_t = _; elements = elements1; arity = lower_arity; react_dro = _ })
),
DefT
( r2,
ArrT (TupleAT { elem_t = _; elements = elements2; arity = upper_arity; react_dro = _ })
)
) ->
let fresh =
match desc_of_reason r1 with
Expand All @@ -1860,12 +1866,17 @@ module Make (Flow : INPUT) : OUTPUT = struct
true
| _ -> false
in
let (num_req1, num_total1) = arity1 in
let (num_req2, num_total2) = arity2 in
let (num_req1, num_total1) = lower_arity in
let (num_req2, num_total2) = upper_arity in
(* Arity range LHS is within arity range RHS *)
let arities_are_valid = num_req1 >= num_req2 && num_total1 <= num_total2 in
if not arities_are_valid then
add_output cx ~trace (Error_message.ETupleArityMismatch ((r1, r2), arity1, arity2, use_op))
add_output
cx
~trace
(Error_message.ETupleArityMismatch
{ use_op; lower_reason = r1; lower_arity; upper_reason = r2; upper_arity }
)
else
let n = ref 0 in
let tuple_element_compat t1 t2 p1 p2 optional1 optional2 =
Expand Down

0 comments on commit 579754f

Please sign in to comment.