Skip to content

Commit

Permalink
[flow] Fix incorrect ObjT fast path logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Apr 10, 2024
1 parent 8942236 commit c6ce34d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/typing/subtyping_kit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1666,13 +1666,13 @@ module Make (Flow : INPUT) : OUTPUT = struct
(*********************************************)

(* ObjT -> ObjT *)
| ( DefT (lreason, ObjT ({ props_tmap = lflds; _ } as l_obj)),
DefT (ureason, ObjT ({ props_tmap = uflds; _ } as u_obj))
| ( DefT (lreason, ObjT ({ props_tmap = lflds; call_t = l_call_id; _ } as l_obj)),
DefT (ureason, ObjT ({ props_tmap = uflds; call_t = r_call_id; _ } as u_obj))
) ->
let u_deft = u in
Type_inference_hooks_js.dispatch_obj_to_obj_hook cx l u_deft;
let print_fast_path = Context.is_verbose cx in
if Properties.equal_id lflds uflds then (
if Properties.equal_id lflds uflds && l_call_id = r_call_id then (
if print_fast_path then prerr_endline "ObjT ~> ObjT fast path: yes"
) else (
if print_fast_path then prerr_endline "ObjT ~> ObjT fast path: no";
Expand Down
8 changes: 8 additions & 0 deletions tests/objects/different_call_prop_no_fast_path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This is a regression test for incorrect fast-path for objects
// Previously, we enable ObjT ~> ObjT fast path if props_ids are the same,
// which causes us to incorrectly enter the fast path when the call prop has changed.

type ThunkType<R> = { (): R };
type MyThunkType<R> = ThunkType<R>;
declare const MyThunk: MyThunkType<number>;
MyThunk as ThunkType<string>; // error: number ~> string
20 changes: 19 additions & 1 deletion tests/objects/objects.exp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ References:
^^^^^^ [1]


Error -------------------------------------------------------------------------- different_call_prop_no_fast_path.js:8:1

Cannot cast `MyThunk` to `ThunkType` because number [1] is incompatible with string [2] in the return value.
[incompatible-cast]

different_call_prop_no_fast_path.js:8:1
8| MyThunk as ThunkType<string>; // error: number ~> string
^^^^^^^

References:
different_call_prop_no_fast_path.js:7:36
7| declare const MyThunk: MyThunkType<number>;
^^^^^^ [1]
different_call_prop_no_fast_path.js:8:22
8| MyThunk as ThunkType<string>; // error: number ~> string
^^^^^^ [2]


Error --------------------------------------------------------------------------------------------------- objects.js:4:3

Cannot get `x.foo` because property `foo` is missing in object type [1]. [prop-missing]
Expand Down Expand Up @@ -364,4 +382,4 @@ References:



Found 23 errors
Found 24 errors

0 comments on commit c6ce34d

Please sign in to comment.