Skip to content

Commit

Permalink
[flow] Drop support for $Compose and $ComposeReverse
Browse files Browse the repository at this point in the history
Summary:
I want to get rid of some of the $ types that it's not really used widely and reduces some custom functions that we don't always get right.

Changelog: [errors] Support for $Compose and $ComposeReverse types are removed. We recommend to use overloads to approximate their behavior instead. e.g.

```
declare export function compose(): <T>(a: T) => T;
declare export function compose<F: (...$ReadOnlyArray<empty>) => mixed>(
  f: F,
): F;
declare export function compose<A, T: $ReadOnlyArray<any>, R>(
  f1: (a: A) => R,
  f2: (...T) => A,
): (...T) => R;
declare export function compose<A, B, T: $ReadOnlyArray<any>, R>(
  f1: (b: B) => R,
  f2: (a: A) => B,
  f3: (...T) => A,
): (...T) => R;
declare export function compose<A, B, C, T: $ReadOnlyArray<any>, R>(
  f1: (c: C) => R,
  f2: (b: B) => C,
  f3: (a: A) => B,
  f4: (...T) => A,
): (...T) => R;
declare export function compose<R>(
  f1: (b: any) => R,
  ...funcs: $ReadOnlyArray<(...$ReadOnlyArray<empty>) => mixed>
): (...$ReadOnlyArray<any>) => R;
declare export function compose<R>(
  ...funcs: $ReadOnlyArray<(...$ReadOnlyArray<empty>) => mixed>
): (...$ReadOnlyArray<any>) => R;
```

Reviewed By: panagosg7

Differential Revision: D55048197

fbshipit-source-id: c8f15f6f274a4d9b8e3acafec0a8bd30e5dd7529
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Mar 20, 2024
1 parent e3a6d28 commit 505d1ab
Show file tree
Hide file tree
Showing 24 changed files with 32 additions and 382 deletions.
2 changes: 0 additions & 2 deletions src/parser_utils/type_sig/type_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,6 @@ type ('loc, 'a) annot =
| Object_assign of 'loc
| Object_getPrototypeOf of 'loc
| Object_setPrototypeOf of 'loc
| Compose of 'loc
| ComposeReverse of 'loc
| ReactAbstractComponent of {
loc: 'loc;
config: 'a;
Expand Down
10 changes: 0 additions & 10 deletions src/parser_utils/type_sig/type_sig_parse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2520,16 +2520,6 @@ and maybe_special_unqualified_generic opts scope tbls xs loc targs ref_loc =
Annot (ReactElementRef (loc, t))
| _ -> Err (loc, CheckError)
end
| "$Compose" -> begin
match targs with
| None -> Annot (Compose loc)
| _ -> Err (loc, CheckError)
end
| "$ComposeReverse" -> begin
match targs with
| None -> Annot (ComposeReverse loc)
| _ -> Err (loc, CheckError)
end
| "$Flow$DebugPrint" -> begin
match targs with
| None -> Annot (FlowDebugPrint loc)
Expand Down
97 changes: 0 additions & 97 deletions src/typing/custom_fun_kit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,106 +26,9 @@ end

module Kit (Flow : Flow_common.S) : CUSTOM_FUN = struct
include Flow
module PinTypes = Implicit_instantiation.PinTypes (Flow)

(* Creates the appropriate constraints for the compose() function and its
* reversed variant. *)
let rec run_compose cx trace ~use_op reason_op reverse fns spread_fn tin tout =
match (reverse, fns, spread_fn) with
(* Call the tail functions in our array first and call our head function
* last after that. *)
| (false, fn :: fns, _) ->
let reason = replace_desc_reason (RCustom "compose intermediate value") (reason_of_t fn) in
let tvar =
Tvar.mk_no_wrap_where cx reason (fun tvar ->
run_compose cx trace ~use_op reason_op reverse fns spread_fn tin tvar
)
in
rec_flow
cx
trace
( fn,
CallT
{
use_op;
reason;
call_action =
Funcalltype
(mk_functioncalltype ~call_kind:RegularCallKind reason_op None [Arg tvar] tout);
return_hint = Type.hint_unavailable;
}
)
(* If the compose function is reversed then we want to call the tail
* functions in our array after we call the head function. *)
| (true, fn :: fns, _) ->
let reason = replace_desc_reason (RCustom "compose intermediate value") (reason_of_t fn) in
let tvar = Tvar.mk_no_wrap cx reason in
rec_flow
cx
trace
( fn,
CallT
{
use_op;
reason;
call_action =
Funcalltype
(mk_functioncalltype
~call_kind:RegularCallKind
reason_op
None
[Arg (OpenT tin)]
(reason, tvar)
);
return_hint = Type.hint_unavailable;
}
);
run_compose cx trace ~use_op reason_op reverse fns spread_fn (reason, tvar) tout
(* If there are no functions and no spread function then we are an identity
* function. *)
| (_, [], None) -> rec_flow_t ~use_op:unknown_use cx trace (OpenT tin, OpenT tout)
| (_, [], Some spread_fn) ->
Flow_js_utils.add_output
cx
Error_message.(
EUnsupportedSyntax (spread_fn |> TypeUtil.reason_of_t |> loc_of_reason, SpreadArgument)
);
rec_flow_t cx ~use_op:unknown_use trace (AnyT.error reason_op, OpenT tin);
rec_flow_t cx ~use_op:unknown_use trace (AnyT.error reason_op, OpenT tout)

let run cx trace ~use_op ~return_hint reason_op kind args spread_arg tout =
match kind with
| Compose reverse ->
(* Drop the specific argument reasons since run_compose will emit CallTs
* with completely unrelated argument reasons. *)
let use_op =
match use_op with
| Op (FunCall { op; fn; args = _; local }) -> Op (FunCall { op; fn; args = []; local })
| Op (FunCallMethod { op; fn; prop; args = _; local }) ->
Op (FunCallMethod { op; fn; prop; args = []; local })
| _ -> use_op
in
let tin = (reason_op, Tvar.mk_no_wrap cx reason_op) in
let tvar = (reason_op, Tvar.mk_no_wrap cx reason_op) in
run_compose cx trace ~use_op reason_op reverse args spread_arg tin tvar;
let tin =
let tin' = (reason_op, Tvar.mk_no_wrap cx reason_op) in
unify cx (OpenT tin') (PinTypes.pin_type cx ~use_op:unknown_use reason_op (OpenT tin));
tin'
in
let funt =
FunT
( dummy_static reason_op,
mk_functiontype
reason_op
[OpenT tin]
~rest_param:None
~def_reason:reason_op
~predicate:None
(OpenT tvar)
)
in
rec_flow_t ~use_op:unknown_use cx trace (DefT (reason_op, funt), tout)
| ReactCreateElement ->
(match args with
(* React.createElement(component) *)
Expand Down
2 changes: 0 additions & 2 deletions src/typing/debug_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ let rec dump_t_ (depth, tvars) cx t =
| ObjectAssign -> "ObjectAssign"
| ObjectGetPrototypeOf -> "ObjectGetPrototypeOf"
| ObjectSetPrototypeOf -> "ObjectSetPrototypeOf"
| Compose false -> "Compose"
| Compose true -> "ComposeReverse"
| ReactCreateElement -> "ReactCreateElement"
| ReactCloneElement -> "ReactCloneElement"
| ReactElementFactory _ -> "ReactElementFactory"
Expand Down
10 changes: 2 additions & 8 deletions src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3318,7 +3318,7 @@ struct
| ( CustomFunT
( _,
( ObjectAssign | ObjectGetPrototypeOf | ObjectSetPrototypeOf | DebugPrint
| DebugThrow | DebugSleep | Compose _ | ReactCreateElement | ReactCloneElement
| DebugThrow | DebugSleep | ReactCreateElement | ReactCloneElement
| ReactElementFactory _ )
),
CallT { use_op; call_action = ConcretizeCallee tout; _ }
Expand Down Expand Up @@ -3404,11 +3404,7 @@ struct
rec_flow cx trace (t, DebugSleepT reason_op);
rec_flow_t cx ~use_op trace (VoidT.why reason_op, OpenT call_tout)
| ( CustomFunT
( lreason,
( (Compose _ | ReactCreateElement | ReactCloneElement | ReactElementFactory _) as
kind
)
),
(lreason, ((ReactCreateElement | ReactCloneElement | ReactElementFactory _) as kind)),
CallT { use_op; reason = reason_op; call_action = Funcalltype calltype; return_hint }
) ->
let {
Expand Down Expand Up @@ -6815,7 +6811,6 @@ struct
| UseT (_, CustomFunT (_, ObjectAssign))
| UseT (_, CustomFunT (_, ObjectGetPrototypeOf))
| UseT (_, CustomFunT (_, ObjectSetPrototypeOf))
| UseT (_, CustomFunT (_, Compose _))
| UseT (_, CustomFunT (_, ReactCreateElement))
| UseT (_, CustomFunT (_, ReactCloneElement))
| UseT (_, CustomFunT (_, DebugPrint))
Expand Down Expand Up @@ -6912,7 +6907,6 @@ struct
| CustomFunT (_, ObjectAssign)
| CustomFunT (_, ObjectGetPrototypeOf)
| CustomFunT (_, ObjectSetPrototypeOf)
| CustomFunT (_, Compose _)
| CustomFunT (_, ReactCreateElement)
| CustomFunT (_, ReactCloneElement)
| CustomFunT (_, DebugThrow)
Expand Down
8 changes: 2 additions & 6 deletions src/typing/ty_normalizer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ module Make (I : INPUT) : S = struct
in
generic_talias opaque_symbol targs

and custom_fun_expanded ~env =
and custom_fun_expanded =
Type.(
function
(* Object.assign: (target: any, ...sources: Array<any>): any *)
Expand Down Expand Up @@ -1498,8 +1498,6 @@ module Make (I : INPUT) : S = struct
let f2 = mk_fun ~tparams ~params (ReturnType reactElement) in
mk_inter (f1, [f2])
)
(* Fallback *)
| t -> custom_fun_short ~env t
)

and subst_name ~env loc t bound name =
Expand Down Expand Up @@ -1558,8 +1556,6 @@ module Make (I : INPUT) : S = struct
| ObjectAssign -> return (builtin_t (Reason.OrdinaryName "Object$Assign"))
| ObjectGetPrototypeOf -> return (builtin_t (Reason.OrdinaryName "Object$GetPrototypeOf"))
| ObjectSetPrototypeOf -> return (builtin_t (Reason.OrdinaryName "Object$SetPrototypeOf"))
| Compose false -> return (builtin_t (Reason.OrdinaryName "$Compose"))
| Compose true -> return (builtin_t (Reason.OrdinaryName "$ComposeReverse"))
| ReactCreateElement -> return (builtin_t (Reason.OrdinaryName "React$CreateElement"))
| ReactCloneElement -> return (builtin_t (Reason.OrdinaryName "React$CloneElement"))
| ReactElementFactory t ->
Expand All @@ -1572,7 +1568,7 @@ module Make (I : INPUT) : S = struct

and custom_fun ~env t =
if Env.expand_internal_types env then
custom_fun_expanded ~env t
custom_fun_expanded t
else
custom_fun_short ~env t

Expand Down
2 changes: 0 additions & 2 deletions src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1734,8 +1734,6 @@ module rec TypeTerm : sig
| ObjectAssign
| ObjectGetPrototypeOf
| ObjectSetPrototypeOf
(* common community functions *)
| Compose of bool
(* 3rd party libs *)
| ReactCreateElement
| ReactCloneElement
Expand Down
2 changes: 0 additions & 2 deletions src/typing/type_annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1216,8 +1216,6 @@ module Make (ConsGen : Type_annotation_sig.ConsGen) (Statement : Statement_sig.S
| "Object$Assign" -> mk_custom_fun cx loc t_ast targs ident ObjectAssign
| "Object$GetPrototypeOf" -> mk_custom_fun cx loc t_ast targs ident ObjectGetPrototypeOf
| "Object$SetPrototypeOf" -> mk_custom_fun cx loc t_ast targs ident ObjectSetPrototypeOf
| "$Compose" -> mk_custom_fun cx loc t_ast targs ident (Compose false)
| "$ComposeReverse" -> mk_custom_fun cx loc t_ast targs ident (Compose true)
| "React$AbstractComponent" ->
let reason = mk_reason (RCustom "AbstractComponent") loc in
(match targs with
Expand Down
1 change: 0 additions & 1 deletion src/typing/type_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,6 @@ class virtual ['a] t =
| ObjectAssign
| ObjectGetPrototypeOf
| ObjectSetPrototypeOf
| Compose _
| ReactCreateElement
| ReactCloneElement
| DebugPrint
Expand Down
6 changes: 0 additions & 6 deletions src/typing/type_sig_merge.ml
Original file line number Diff line number Diff line change
Expand Up @@ -891,12 +891,6 @@ and merge_annot env file = function
| Object_setPrototypeOf loc ->
let reason = Reason.(mk_reason RFunctionType loc) in
Type.CustomFunT (reason, Type.ObjectSetPrototypeOf)
| Compose loc ->
let reason = Reason.(mk_reason RFunctionType loc) in
Type.CustomFunT (reason, Type.Compose false)
| ComposeReverse loc ->
let reason = Reason.(mk_reason RFunctionType loc) in
Type.CustomFunT (reason, Type.Compose true)
| ReactAbstractComponent { loc; config; instance; renders } ->
let reason = Reason.(mk_reason (RCustom "AbstractComponent") loc) in
let config = merge env file config in
Expand Down
1 change: 0 additions & 1 deletion src/typing/type_visitor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ class ['a] t =
| ObjectAssign
| ObjectGetPrototypeOf
| ObjectSetPrototypeOf
| Compose _
| ReactCreateElement
| ReactCloneElement
| DebugPrint
Expand Down
28 changes: 14 additions & 14 deletions tests/any/any.exp
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ References:
^^^^^^ [2]


Error ---------------------------------------------------------------------------------------- ex_special_cases.js:39:21
Error ---------------------------------------------------------------------------------------- ex_special_cases.js:36:21

Cannot assign `array[receiver.name]` to `test1` because number [1] is incompatible with string [2]. [incompatible-type]

ex_special_cases.js:39:21
39| var test1: string = array[receiver.name]; // if result of receiver.getprop is any, this will fail, but no error if it's empty.
ex_special_cases.js:36:21
36| var test1: string = array[receiver.name]; // if result of receiver.getprop is any, this will fail, but no error if it's empty.
^^^^^^^^^^^^^^^^^^^^

References:
ex_special_cases.js:38:26
38| declare var array: Array<number>;
ex_special_cases.js:35:26
35| declare var array: Array<number>;
^^^^^^ [1]
ex_special_cases.js:39:12
39| var test1: string = array[receiver.name]; // if result of receiver.getprop is any, this will fail, but no error if it's empty.
ex_special_cases.js:36:12
36| var test1: string = array[receiver.name]; // if result of receiver.getprop is any, this will fail, but no error if it's empty.
^^^^^^ [2]


Error ---------------------------------------------------------------------------------------- ex_special_cases.js:40:21
Error ---------------------------------------------------------------------------------------- ex_special_cases.js:37:21

Cannot assign `array[receiver.name()]` to `test2` because number [1] is incompatible with string [2].
[incompatible-type]

ex_special_cases.js:40:21
40| var test2: string = array[receiver.name()] // likewise
ex_special_cases.js:37:21
37| var test2: string = array[receiver.name()] // likewise
^^^^^^^^^^^^^^^^^^^^^^

References:
ex_special_cases.js:38:26
38| declare var array: Array<number>;
ex_special_cases.js:35:26
35| declare var array: Array<number>;
^^^^^^ [1]
ex_special_cases.js:40:12
40| var test2: string = array[receiver.name()] // likewise
ex_special_cases.js:37:12
37| var test2: string = array[receiver.name()] // likewise
^^^^^^ [2]


Expand Down
3 changes: 0 additions & 3 deletions tests/any/ex_special_cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// any ~> functionlike
class C { }
var C_any: any = C;
declare var cmp: $Compose;
var cmp_cany: any = cmp;
declare var proto_apply: Function$Prototype$Apply;
declare var proto_bind: Function$Prototype$Bind;
declare var proto_call: Function$Prototype$Call;
Expand All @@ -17,7 +15,6 @@ var f_any: any = f;
// functionlike ~> any
declare var a: any;
var C_from_any: Class<C> = a;
var cmp_from_any: $Compose = a;
var proto_apply_from_any: Function$Prototype$Apply = a;
var proto_bind_from_any: Function$Prototype$Bind = a;
var proto_call_from_any: Function$Prototype$Call = a;
Expand Down
30 changes: 1 addition & 29 deletions tests/babel_loose_array_spread/babel_loose_array_spread.exp
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,6 @@ References:
^ [2]


Error --------------------------------------------------------------------------------------------------- compose.js:6:2

A spread argument is unsupported here. [unsupported-syntax]

v-------
6| (compose(
7| ...fns1, // Error
8| )(42));
^


Error --------------------------------------------------------------------------------------------------- compose.js:7:6

`$Iterable` [1] is incompatible with `$ReadOnlyArray` [2]. [incompatible-type]

compose.js:7:6
7| ...fns1, // Error
^^^^

References:
compose.js:4:19
4| declare var fns1: Iterable<(number) => number>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [1]
<BUILTINS>/core.js:698:15
698| declare class $ReadOnlyArray<+T> {
^^^^^^^^^^^^^^ [2]


Error ------------------------------------------------------------------------------------------------- iterables.js:2:5

`$Iterable` [1] is incompatible with `$ReadOnlyArray` [2]. [incompatible-type]
Expand Down Expand Up @@ -219,4 +191,4 @@ References:



Found 13 errors
Found 11 errors
14 changes: 0 additions & 14 deletions tests/babel_loose_array_spread/compose.js

This file was deleted.

Loading

0 comments on commit 505d1ab

Please sign in to comment.