Skip to content

Commit

Permalink
[flow] Kill UnionRep.specialized_reason
Browse files Browse the repository at this point in the history
Summary:
I don't think the reason patching here creates any more value. Most of the time, we patch it to be `literal union`, which is probably less helpful than the underlying reasons.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D56642933

fbshipit-source-id: 37f922037190d2b5ce5feb5729219ef0125328b8
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Apr 30, 2024
1 parent 1b8141c commit 959ed79
Show file tree
Hide file tree
Showing 28 changed files with 174 additions and 197 deletions.
16 changes: 5 additions & 11 deletions src/typing/speculation_kit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -838,12 +838,7 @@ module Make (Flow : INPUT) : OUTPUT = struct
cx
~trace
(Error_message.EIncompatibleWithUseOp
{
reason_lower = TypeUtil.reason_of_t l;
reason_upper =
UnionRep.specialized_reason ~reason_of_t:TypeUtil.reason_of_t reason_op rep;
use_op;
}
{ reason_lower = TypeUtil.reason_of_t l; reason_upper = reason_op; use_op }
);
true
| DefT (_, ObjT _)
Expand All @@ -856,27 +851,26 @@ module Make (Flow : INPUT) : OUTPUT = struct

and shortcut_enum cx trace reason_op use_op l rep =
let quick_subtype = TypeUtil.quick_subtype in
quick_mem_result cx trace reason_op use_op l rep @@ UnionRep.quick_mem_enum ~quick_subtype l rep
quick_mem_result cx trace reason_op use_op l @@ UnionRep.quick_mem_enum ~quick_subtype l rep

and shortcut_disjoint_union cx trace reason_op use_op l rep =
let quick_subtype = TypeUtil.quick_subtype in
quick_mem_result cx trace reason_op use_op l rep
quick_mem_result cx trace reason_op use_op l
@@ UnionRep.quick_mem_disjoint_union
~quick_subtype
l
rep
~find_resolved:(Context.find_resolved cx)
~find_props:(Context.find_props cx)

and quick_mem_result cx trace reason_op use_op l rep = function
and quick_mem_result cx trace reason_op use_op l = function
| UnionRep.Yes ->
(* membership check succeeded *)
true
(* Our work here is done, so no need to continue. *)
| UnionRep.No ->
(* membership check failed *)
let r = UnionRep.specialized_reason ~reason_of_t:TypeUtil.reason_of_t reason_op rep in
rec_flow cx trace (l, UseT (use_op, DefT (r, EmptyT)));
rec_flow cx trace (l, UseT (use_op, DefT (reason_op, EmptyT)));
true
(* Our work here is done, so no need to continue. *)
| UnionRep.Conditional t ->
Expand Down
7 changes: 1 addition & 6 deletions src/typing/subtyping_kit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -986,12 +986,7 @@ module Make (Flow : INPUT) : OUTPUT = struct
cx
~trace
(Error_message.EIncompatibleWithUseOp
{
reason_lower = reason_l;
reason_upper =
UnionRep.specialized_reason ~reason_of_t:TypeUtil.reason_of_t reason_u rep;
use_op;
}
{ reason_lower = reason_l; reason_upper = reason_u; use_op }
);
true
| _ -> false ->
Expand Down
10 changes: 0 additions & 10 deletions src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2381,9 +2381,6 @@ and UnionRep : sig
(** build a rep from list of members *)
val make : ?source_aloc:ALoc.id -> TypeTerm.t -> TypeTerm.t -> TypeTerm.t list -> t

(** replace reason with specialized desc, if any *)
val specialized_reason : reason_of_t:(TypeTerm.t -> reason) -> reason -> t -> reason

(** members in declaration order *)
val members : t -> TypeTerm.t list

Expand Down Expand Up @@ -2584,13 +2581,6 @@ end = struct
else
rep

let specialized_reason ~reason_of_t r { specialization; _ } =
match !specialization with
| Some Empty -> replace_desc_reason REmpty r
| Some (Singleton t) -> reason_of_t t
| Some (EnumUnion _) -> replace_desc_reason RUnionEnum r
| _ -> r

(********** Optimizations **********)

let is_optimized_finally { specialization; _ } = !specialization <> None
Expand Down
8 changes: 4 additions & 4 deletions tests/bigint/bigint.exp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ Unsupported key in object literal. Only identifier, string literal, and number l

Error --------------------------------------------------------------------------------------------------- unions.js:5:10

Cannot return `x` because number literal `1` [1] is incompatible with literal union [2]. [incompatible-return]
Cannot return `x` because number literal `1` [1] is incompatible with `bigints` [2]. [incompatible-return]

unions.js:5:10
5| return x; // fast path error
Expand All @@ -361,7 +361,7 @@ References:

Error --------------------------------------------------------------------------------------------------- unions.js:5:10

Cannot return `x` because number literal `2` [1] is incompatible with literal union [2]. [incompatible-return]
Cannot return `x` because number literal `2` [1] is incompatible with `bigints` [2]. [incompatible-return]

unions.js:5:10
5| return x; // fast path error
Expand All @@ -378,7 +378,7 @@ References:

Error --------------------------------------------------------------------------------------------------- unions.js:9:10

Cannot return `x` because bigint literal `1n` [1] is incompatible with literal union [2]. [incompatible-return]
Cannot return `x` because bigint literal `1n` [1] is incompatible with `nums` [2]. [incompatible-return]

unions.js:9:10
9| return x; // fast path error
Expand All @@ -395,7 +395,7 @@ References:

Error --------------------------------------------------------------------------------------------------- unions.js:9:10

Cannot return `x` because bigint literal `2n` [1] is incompatible with literal union [2]. [incompatible-return]
Cannot return `x` because bigint literal `2n` [1] is incompatible with `nums` [2]. [incompatible-return]

unions.js:9:10
9| return x; // fast path error
Expand Down
4 changes: 2 additions & 2 deletions tests/conditional_type/conditional_type.exp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ References:

Error --------------------------------------------------------------------------------------------- distributive.js:26:4

Cannot cast `2` to `one_or_three` because number [1] is incompatible with literal union [2]. [incompatible-cast]
Cannot cast `2` to `one_or_three` because number [1] is incompatible with union type [2]. [incompatible-cast]

distributive.js:26:4
26| (2: one_or_three); // error: 2 ~> 1|3
Expand All @@ -155,7 +155,7 @@ References:

Error --------------------------------------------------------------------------------------------- distributive.js:28:4

Cannot cast `4` to `one_or_three` because number [1] is incompatible with literal union [2]. [incompatible-cast]
Cannot cast `4` to `one_or_three` because number [1] is incompatible with union type [2]. [incompatible-cast]

distributive.js:28:4
28| (4: one_or_three); // error: 4 ~> 1|3
Expand Down
18 changes: 9 additions & 9 deletions tests/dom/dom.exp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ References:

Error ------------------------------------------------------------------------------------------ ClipboardEvent.js:10:45

Cannot call `ClipboardEvent` with `'invalid'` bound to `type` because string [1] is incompatible with literal union [2].
[incompatible-call]
Cannot call `ClipboardEvent` with `'invalid'` bound to `type` because string [1] is incompatible with
`ClipboardEventTypes` [2]. [incompatible-call]

ClipboardEvent.js:10:45
10| const invalidEvent = new ClipboardEvent('invalid'); // invalid
Expand Down Expand Up @@ -95,7 +95,7 @@ References:
Error ------------------------------------------------------------------------------------------------- Element.js:14:28

Cannot call `element.scrollIntoView` with object literal bound to `arg` because: [incompatible-call]
- Either string [1] is incompatible with literal union [2] in property `behavior`.
- Either string [1] is incompatible with union type [2] in property `behavior`.
- Or object literal [3] is incompatible with boolean [4].

Element.js:14:28
Expand All @@ -117,7 +117,7 @@ References:
Error ------------------------------------------------------------------------------------------------- Element.js:15:28

Cannot call `element.scrollIntoView` with object literal bound to `arg` because: [incompatible-call]
- Either string [1] is incompatible with literal union [2] in property `block`.
- Either string [1] is incompatible with union type [2] in property `block`.
- Or object literal [3] is incompatible with boolean [4].

Element.js:15:28
Expand Down Expand Up @@ -206,7 +206,7 @@ References:
Error --------------------------------------------------------------------------------------------- HTMLElement.js:22:28

Cannot call `element.scrollIntoView` with object literal bound to `arg` because: [incompatible-call]
- Either string [1] is incompatible with literal union [2] in property `behavior`.
- Either string [1] is incompatible with union type [2] in property `behavior`.
- Or object literal [3] is incompatible with boolean [4].

HTMLElement.js:22:28
Expand All @@ -228,7 +228,7 @@ References:
Error --------------------------------------------------------------------------------------------- HTMLElement.js:23:28

Cannot call `element.scrollIntoView` with object literal bound to `arg` because: [incompatible-call]
- Either string [1] is incompatible with literal union [2] in property `block`.
- Either string [1] is incompatible with union type [2] in property `block`.
- Or object literal [3] is incompatible with boolean [4].

HTMLElement.js:23:28
Expand Down Expand Up @@ -536,7 +536,7 @@ References:
Error ----------------------------------------------------------------------------------------- HTMLInputElement.js:10:8

Cannot call `el.setRangeText` because: [incompatible-call]
- Either string [1] is incompatible with literal union [2].
- Either string [1] is incompatible with `SelectionMode` [2].
- Or number [3] is incompatible with undefined [4].

HTMLInputElement.js:10:8
Expand Down Expand Up @@ -1011,7 +1011,7 @@ References:
Error ---------------------------------------------------------------------------------------------- traversal.js:188:14

Cannot call `document.createNodeIterator` because: [incompatible-call]
- Either string [1] is incompatible with literal union [2] in the return value of property `acceptNode`.
- Either string [1] is incompatible with union type [2] in the return value of property `acceptNode`.
- Or number [3] is incompatible with number literal `133` [4].

traversal.js:188:14
Expand Down Expand Up @@ -1387,7 +1387,7 @@ References:
Error ---------------------------------------------------------------------------------------------- traversal.js:195:14

Cannot call `document.createTreeWalker` because: [incompatible-call]
- Either string [1] is incompatible with literal union [2] in the return value of property `acceptNode`.
- Either string [1] is incompatible with union type [2] in the return value of property `acceptNode`.
- Or number [3] is incompatible with number literal `133` [4].

traversal.js:195:14
Expand Down
2 changes: 1 addition & 1 deletion tests/enforce_union_optimized/enforce_union_optimized.exp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Cannot use type without exactly 1 type argument. [nonpolymorphic-type-app]

Error ---------------------------------------------------------------------------------------------------- import.js:4:1

Cannot cast `3` to `T1` because number [1] is incompatible with literal union [2]. [incompatible-cast]
Cannot cast `3` to `T1` because number [1] is incompatible with `T1` [2]. [incompatible-cast]

import.js:4:1
4| 3 as T1; // error 3 ~> 1 | 2
Expand Down
2 changes: 1 addition & 1 deletion tests/enum_perf/enum_perf.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Error ----------------------------------------------------------------------------------------------------- yuge.js:23:5

Cannot cast empty string to `ErrorCode` because string [1] is incompatible with literal union [2]. [incompatible-cast]
Cannot cast empty string to `ErrorCode` because string [1] is incompatible with `ErrorCode` [2]. [incompatible-cast]

yuge.js:23:5
23| '' as ErrorCode; // also, error pos omits this line completely
Expand Down
22 changes: 11 additions & 11 deletions tests/fetch/fetch.exp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ References:

Error -------------------------------------------------------------------------------------------------- request.js:6:35

Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with literal union [2] in property
Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with `CacheType` [2] in property
`cache`. This property is invariantly typed. See
https://flow.org/en/docs/faq/#why-cant-i-pass-a-string-to-a-function-that-takes-a-string-number. [incompatible-call]

Expand All @@ -337,8 +337,8 @@ References:

Error -------------------------------------------------------------------------------------------------- request.js:6:35

Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with literal union [2] in property
`credentials`. This property is invariantly typed. See
Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with `CredentialsType` [2] in
property `credentials`. This property is invariantly typed. See
https://flow.org/en/docs/faq/#why-cant-i-pass-a-string-to-a-function-that-takes-a-string-number. [incompatible-call]

request.js:6:35
Expand Down Expand Up @@ -451,7 +451,7 @@ References:

Error -------------------------------------------------------------------------------------------------- request.js:6:35

Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with literal union [2] in property
Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with `ModeType` [2] in property
`mode`. This property is invariantly typed. See
https://flow.org/en/docs/faq/#why-cant-i-pass-a-string-to-a-function-that-takes-a-string-number. [incompatible-call]

Expand All @@ -470,7 +470,7 @@ References:

Error -------------------------------------------------------------------------------------------------- request.js:6:35

Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with literal union [2] in property
Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with `RedirectType` [2] in property
`redirect`. This property is invariantly typed. See
https://flow.org/en/docs/faq/#why-cant-i-pass-a-string-to-a-function-that-takes-a-string-number. [incompatible-call]

Expand Down Expand Up @@ -508,8 +508,8 @@ References:

Error -------------------------------------------------------------------------------------------------- request.js:6:35

Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with literal union [2] in property
`referrerPolicy`. This property is invariantly typed. See
Cannot call `Request` with `c` bound to `init` because undefined [1] is incompatible with `ReferrerPolicyType` [2] in
property `referrerPolicy`. This property is invariantly typed. See
https://flow.org/en/docs/faq/#why-cant-i-pass-a-string-to-a-function-that-takes-a-string-number. [incompatible-call]

request.js:6:35
Expand Down Expand Up @@ -548,7 +548,7 @@ Error --------------------------------------------------------------------------

Cannot call `h.text().then` because: [incompatible-call]
- Either `Buffer` [1] is incompatible with string [2] in the first parameter.
- Or function [3] is incompatible with literal union [4].
- Or function [3] is incompatible with union type [4].

request.js:24:10
24| h.text().then((t: Buffer) => t); // incorrect
Expand All @@ -573,7 +573,7 @@ Error --------------------------------------------------------------------------

Cannot call `h.arrayBuffer().then` because: [incompatible-call]
- Either `ArrayBuffer` [1] is incompatible with `Buffer` [2] in the first parameter.
- Or function [3] is incompatible with literal union [4].
- Or function [3] is incompatible with union type [4].

request.js:26:17
26| h.arrayBuffer().then((ab: Buffer) => ab); // incorrect
Expand Down Expand Up @@ -742,7 +742,7 @@ Error --------------------------------------------------------------------------

Cannot call `h.text().then` because: [incompatible-call]
- Either `Buffer` [1] is incompatible with string [2] in the first parameter.
- Or function [3] is incompatible with literal union [4].
- Or function [3] is incompatible with union type [4].

response.js:42:10
42| h.text().then((t: Buffer) => t); // incorrect
Expand All @@ -767,7 +767,7 @@ Error --------------------------------------------------------------------------

Cannot call `h.arrayBuffer().then` because: [incompatible-call]
- Either `ArrayBuffer` [1] is incompatible with `Buffer` [2] in the first parameter.
- Or function [3] is incompatible with literal union [4].
- Or function [3] is incompatible with union type [4].

response.js:44:17
44| h.arrayBuffer().then((ab: Buffer) => ab); // incorrect
Expand Down
8 changes: 4 additions & 4 deletions tests/interface/interface.exp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Multiple indexers are not supported. [unsupported-syntax]

Error -------------------------------------------------------------------------------------------------- indexer.js:40:3

Cannot get `x['xxx']` because string `xxx` [1] is incompatible with literal union [2]. [incompatible-type]
Cannot get `x['xxx']` because string `xxx` [1] is incompatible with `Keys` [2]. [incompatible-type]

indexer.js:40:3
40| x['xxx']; // ERROR
Expand All @@ -81,7 +81,7 @@ Cannot assign `1` to `x['a']` because property `a` is not writable. [cannot-writ

Error -------------------------------------------------------------------------------------------------- indexer.js:46:3

Cannot get `x['xxx']` because string `xxx` [1] is incompatible with literal union [2]. [incompatible-type]
Cannot get `x['xxx']` because string `xxx` [1] is incompatible with `Keys` [2]. [incompatible-type]

indexer.js:46:3
46| x['xxx']; // ERROR
Expand All @@ -103,7 +103,7 @@ Cannot get `x['a']` because an index signature declaring the expected key / valu

Error -------------------------------------------------------------------------------------------------- indexer.js:52:3

Cannot assign `1` to `x['xxx']` because string `xxx` [1] is incompatible with literal union [2]. [incompatible-type]
Cannot assign `1` to `x['xxx']` because string `xxx` [1] is incompatible with `Keys` [2]. [incompatible-type]

indexer.js:52:3
52| x['xxx'] = 1; // ERROR
Expand Down Expand Up @@ -324,7 +324,7 @@ Cannot assign `1` to `x['a']` because property `a` is not writable. [cannot-writ

Error ------------------------------------------------------------------------------------------------- indexer.js:179:5

Cannot get `x[true]` because boolean [1] is incompatible with literal union [2]. [incompatible-type]
Cannot get `x[true]` because boolean [1] is incompatible with `Keys` [2]. [incompatible-type]

indexer.js:179:5
179| x[true]; // ERROR
Expand Down
Loading

0 comments on commit 959ed79

Please sign in to comment.