Skip to content

Commit

Permalink
[flow] make EMethodUnbinding defered_in_speculation
Browse files Browse the repository at this point in the history
Summary:
The difference in behavior can be seen in the example:
```
interface MethLike { m(): number }
interface PropLike { m: () => string; }

declare const obj: MethLike & PropLike;
```
Before this change, calling `obj.m` and calling `obj.m()` had a weird inconsistency:
```
obj.m as () => string; // no error
obj.m() as number; // no error
```
as Flow would pick `.m` from different definitions each time.

Accessing `obj.m` would cause a [method-unbinding] on `MethLike`, so in the first line we would pick `.m` off of `PropLike`. In the second case, we wouldn't get a [method-unbinding] error and so we would eventually get a number.

With this change the following
```
obj.m as () => number;
obj.m() as number;
```
has no type incompatibility, but we still raise the method-unbinding error in the first case.

Changelog: [errors] method-unbinding errors will not affect choice over intersection members. (e.g. [try-Flow](https://flow.org/try/#1N4Igxg9gdgZglgcxALlAIwIZoKYBsD6uEEAztvhgE6UYCe+JADpdhgCYowa5kA0I2KAFcAtiRQAXSkOz9sADwxgJ+NPTbYuQ3BMnTZA+Y2yU4IwRO4A6SFBIrGVDGM7c+h46fNRLuKxJIGWh8MeT0ZfhYlCStpHzNsFBAMIQkIEQwJODAQfiEyfBE4eWw2fDgofDBMsAALfAA3KjgsXGxxZC4eAw0G-GhcWn9aY3wWZldu-g1mbGqJUoBaCRHEzrcDEgBrbAk62kXhXFxJ923d-cPRHEpTgyEoMDaqZdW7vKgoOfaSKgOKpqmDA+d4gB5fMA-P6LCCMLLQbiLOoYCqgh6-GDYRYIXYLSgkRZkCR4jpddwPfJLZjpOBkO4AX34kA0SQAOlAKniuJCAAQAWV2tQAMnAdjzgDyRAAKACUyB5whENx59PZnJM3OwPIACpRYSKxRKRPLZTyALwAPh59lMUAQAG4Vez2RonlQtbZ7DyIGgAFbygUSYWirUAMh1esYBuw9udUB9vqsIh5GBIPNNloV1xMjoA9LmeQB3ODHBUQQs8nApyWCiBsRYPNAVNgVBA8kx6yjshNJ02prNKnPs3IgBomEhwaBJBoABisACYAKwAdisM5A9KAA))

Reviewed By: SamChou19815

Differential Revision: D67529686

fbshipit-source-id: 3a4184cda40d1ddf1baec68d92b00fb5c55fec07
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Dec 23, 2024
1 parent 7113340 commit 4804b12
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/typing/errors/error_message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2915,6 +2915,7 @@ let friendly_message_of_msg = function

let defered_in_speculation = function
| EUntypedTypeImport _
| EMethodUnbinding _
| EUntypedImport _
| ENonstrictImport _
| EUnclearType _
Expand Down
4 changes: 2 additions & 2 deletions tests/method_unbinding/complex_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ declare var c : I & K;
declare var c2 : K & I;
declare var d : I | K;

c.m; // ok
c.m; // error -- picking I.m
c2.m; // ok
d.m; // err

let {m : mc} = c; // ok
let {m : mc} = c; // err -- picking I.m
let {m : mc2} = c2; // ok
let {m : md} = d; // err
55 changes: 36 additions & 19 deletions tests/method_unbinding/method_unbinding.exp
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
Error -------------------------------------------------------------------------------------------- complex_types.js:12:1
Error -------------------------------------------------------------------------------------------- complex_types.js:12:3

Cannot get `a.m` because: [incompatible-use]
- Either property `m` [1] cannot be unbound from the context [2] where it was defined.
- Or property `m` [1] cannot be unbound from the context [3] where it was defined.

complex_types.js:12:1
12| a.m; // err
^^^
Cannot get `a.m` because property `m` [1] cannot be unbound from the context [2] where it was defined.
[method-unbinding]

References:
complex_types.js:12:3
12| a.m; // err
^ [1]

References:
complex_types.js:2:5
2| m() : void
^^^^^^^^^^ [2]
complex_types.js:6:5
6| m() : void
^^^^^^^^^^ [3]


Error -------------------------------------------------------------------------------------------- complex_types.js:13:3
Expand Down Expand Up @@ -52,9 +45,7 @@ References:

Error -------------------------------------------------------------------------------------------- complex_types.js:14:6

All branches are incompatible: [incompatible-use]
- Either property `m` [1] cannot be unbound from the context [2] where it was defined.
- Or property `m` [1] cannot be unbound from the context [3] where it was defined.
property `m` [1] cannot be unbound from the context [2] where it was defined. [method-unbinding]

complex_types.js:14:6
14| let {m : ma} = a; // err
Expand All @@ -64,9 +55,6 @@ References:
complex_types.js:2:5
2| m() : void
^^^^^^^^^^ [2]
complex_types.js:6:5
6| m() : void
^^^^^^^^^^ [3]


Error -------------------------------------------------------------------------------------------- complex_types.js:15:6
Expand Down Expand Up @@ -97,6 +85,21 @@ References:
^^^^^^^^^^ [2]


Error -------------------------------------------------------------------------------------------- complex_types.js:25:3

Cannot get `c.m` because property `m` [1] cannot be unbound from the context [2] where it was defined.
[method-unbinding]

complex_types.js:25:3
25| c.m; // error -- picking I.m
^ [1]

References:
complex_types.js:2:5
2| m() : void
^^^^^^^^^^ [2]


Error -------------------------------------------------------------------------------------------- complex_types.js:27:3

Cannot get `d.m` because property `m` [1] cannot be unbound from the context [2] where it was defined.
Expand All @@ -112,6 +115,20 @@ References:
^^^^^^^^^^ [2]


Error -------------------------------------------------------------------------------------------- complex_types.js:29:6

property `m` [1] cannot be unbound from the context [2] where it was defined. [method-unbinding]

complex_types.js:29:6
29| let {m : mc} = c; // err -- picking I.m
^ [1]

References:
complex_types.js:2:5
2| m() : void
^^^^^^^^^^ [2]


Error -------------------------------------------------------------------------------------------- complex_types.js:31:6

property `m` [1] cannot be unbound from the context [2] where it was defined. [method-unbinding]
Expand Down Expand Up @@ -447,4 +464,4 @@ Cannot assign function to `b.m` because property `m` is not writable. [cannot-wr



Found 30 errors
Found 32 errors

0 comments on commit 4804b12

Please sign in to comment.