Commit bb2f267
[name_resolver] Avoiding creating phi nodes when merging with havoc if the variable is initialized
Summary:
Currently, we only have this behavior when the variable declaration is annotated, but we still create phi nodes if the variable is initialized. There is no good reason for such split, except that we want to maintain the behavior with the inference before new environment.
However, this decision can cause serious perf issues under LTI, when the union type won't just merge together in a single type, but remained to be unions. Consider the following example:
```
let data1: Array<string> = [];
let data = data1
if (true) data = data.filter(s => s.substring(0));
if (true) data = data.filter(s => s.substring(0));
// repeat n times
if (true) data = data.filter(s => s.substring(0));
(data: Array<string>);
```
At the point of final cast, the name_resolver will tell us the read of `data` is phi node, and the type will be obtained by flowing n types into a result tvar. Pre-LTI, these n types will coalesce into one type, so the perf is not terrible, but under LTI, the result will be a size-n union type. The issue also exists for each if branch, where the size of union type is linearly increasing. Under LTI, we will also do contextual typing for `s`, so we will do work in each item in the if-chain proportional to the size of the union type, which leads to a quadratic blowup.
To fix it, I extend the avoiding phi-node logic to include initialized variables as well. This also fits in the LTI story, since under LTI every expressions, like annotations, cannot have their type widened.
Changelog: [errors] When a variable is initialized at declaration site, subsequent assignment in a conditional branch will no longer cause later reads to get a union type. This is already the case if the variable is annotated. As a result of this change, you might get new type errors. Example: https://flow.org/try/#0DYUwLgBAJghmMQLwQEQEYUG4BQBLAZhABRgBOAriAJTRwLJo6zyYQD0bEADqSAG64A9uQDOwAJ4RckAO65gwCAAsYfEBDDiu6gHbkAtgCMQpCAB8IIsrh0BzADQQdgmVNnzFKtRq3qrpG1tsIA
Reviewed By: panagosg7
Differential Revision:
D43134095 (d4f94ee)
------------------------------------------------------------------------
(from ad59e13768a9567e487b642b9a54ac6b6b34027e)
fbshipit-source-id: 48f15ff0e74cd98880e89160b918212a5f4197491 parent cec5df5 commit bb2f267
File tree
13 files changed
+108
-329
lines changed- src/analysis/env_builder
- __tests__
- tests
- badly_positioned_flow_use_op
- new_env
- refinements
- switch
- type_at_pos_types
13 files changed
+108
-329
lines changedLines changed: 29 additions & 65 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1537 | 1537 | | |
1538 | 1538 | | |
1539 | 1539 | | |
1540 | | - | |
1541 | | - | |
1542 | | - | |
| 1540 | + | |
1543 | 1541 | | |
1544 | 1542 | | |
1545 | 1543 | | |
| |||
1664 | 1662 | | |
1665 | 1663 | | |
1666 | 1664 | | |
1667 | | - | |
1668 | | - | |
| 1665 | + | |
1669 | 1666 | | |
1670 | 1667 | | |
1671 | 1668 | | |
| |||
1937 | 1934 | | |
1938 | 1935 | | |
1939 | 1936 | | |
1940 | | - | |
1941 | | - | |
1942 | | - | |
| 1937 | + | |
1943 | 1938 | | |
1944 | 1939 | | |
1945 | | - | |
1946 | | - | |
| 1940 | + | |
1947 | 1941 | | |
1948 | 1942 | | |
1949 | 1943 | | |
| |||
1978 | 1972 | | |
1979 | 1973 | | |
1980 | 1974 | | |
1981 | | - | |
| 1975 | + | |
1982 | 1976 | | |
1983 | 1977 | | |
1984 | 1978 | | |
| |||
2042 | 2036 | | |
2043 | 2037 | | |
2044 | 2038 | | |
2045 | | - | |
2046 | | - | |
2047 | | - | |
| 2039 | + | |
2048 | 2040 | | |
2049 | 2041 | | |
2050 | 2042 | | |
| |||
2074 | 2066 | | |
2075 | 2067 | | |
2076 | 2068 | | |
2077 | | - | |
| 2069 | + | |
2078 | 2070 | | |
2079 | 2071 | | |
2080 | 2072 | | |
| |||
2160 | 2152 | | |
2161 | 2153 | | |
2162 | 2154 | | |
2163 | | - | |
2164 | | - | |
| 2155 | + | |
2165 | 2156 | | |
2166 | 2157 | | |
2167 | 2158 | | |
2168 | 2159 | | |
2169 | 2160 | | |
2170 | | - | |
| 2161 | + | |
2171 | 2162 | | |
2172 | 2163 | | |
2173 | 2164 | | |
| |||
2320 | 2311 | | |
2321 | 2312 | | |
2322 | 2313 | | |
2323 | | - | |
2324 | | - | |
2325 | | - | |
| 2314 | + | |
2326 | 2315 | | |
2327 | 2316 | | |
2328 | | - | |
2329 | | - | |
| 2317 | + | |
2330 | 2318 | | |
2331 | 2319 | | |
2332 | 2320 | | |
| |||
2361 | 2349 | | |
2362 | 2350 | | |
2363 | 2351 | | |
2364 | | - | |
| 2352 | + | |
2365 | 2353 | | |
2366 | 2354 | | |
2367 | 2355 | | |
| |||
2411 | 2399 | | |
2412 | 2400 | | |
2413 | 2401 | | |
2414 | | - | |
2415 | | - | |
2416 | | - | |
| 2402 | + | |
2417 | 2403 | | |
2418 | 2404 | | |
2419 | 2405 | | |
| |||
2443 | 2429 | | |
2444 | 2430 | | |
2445 | 2431 | | |
2446 | | - | |
2447 | | - | |
2448 | | - | |
| 2432 | + | |
2449 | 2433 | | |
2450 | 2434 | | |
2451 | 2435 | | |
| |||
2524 | 2508 | | |
2525 | 2509 | | |
2526 | 2510 | | |
2527 | | - | |
2528 | | - | |
2529 | | - | |
| 2511 | + | |
2530 | 2512 | | |
2531 | 2513 | | |
2532 | 2514 | | |
| |||
2604 | 2586 | | |
2605 | 2587 | | |
2606 | 2588 | | |
2607 | | - | |
2608 | | - | |
2609 | | - | |
| 2589 | + | |
2610 | 2590 | | |
2611 | 2591 | | |
2612 | 2592 | | |
| |||
2846 | 2826 | | |
2847 | 2827 | | |
2848 | 2828 | | |
2849 | | - | |
2850 | | - | |
2851 | | - | |
| 2829 | + | |
2852 | 2830 | | |
2853 | 2831 | | |
2854 | 2832 | | |
| |||
2880 | 2858 | | |
2881 | 2859 | | |
2882 | 2860 | | |
2883 | | - | |
2884 | | - | |
| 2861 | + | |
2885 | 2862 | | |
2886 | 2863 | | |
2887 | 2864 | | |
| |||
2920 | 2897 | | |
2921 | 2898 | | |
2922 | 2899 | | |
2923 | | - | |
2924 | | - | |
2925 | | - | |
| 2900 | + | |
2926 | 2901 | | |
2927 | 2902 | | |
2928 | 2903 | | |
| |||
2937 | 2912 | | |
2938 | 2913 | | |
2939 | 2914 | | |
2940 | | - | |
2941 | | - | |
| 2915 | + | |
2942 | 2916 | | |
2943 | 2917 | | |
2944 | 2918 | | |
| |||
3057 | 3031 | | |
3058 | 3032 | | |
3059 | 3033 | | |
3060 | | - | |
3061 | | - | |
3062 | | - | |
| 3034 | + | |
3063 | 3035 | | |
3064 | 3036 | | |
3065 | 3037 | | |
| |||
3091 | 3063 | | |
3092 | 3064 | | |
3093 | 3065 | | |
3094 | | - | |
3095 | | - | |
| 3066 | + | |
3096 | 3067 | | |
3097 | 3068 | | |
3098 | 3069 | | |
| |||
3131 | 3102 | | |
3132 | 3103 | | |
3133 | 3104 | | |
3134 | | - | |
3135 | | - | |
3136 | | - | |
| 3105 | + | |
3137 | 3106 | | |
3138 | 3107 | | |
3139 | 3108 | | |
| |||
3148 | 3117 | | |
3149 | 3118 | | |
3150 | 3119 | | |
3151 | | - | |
3152 | | - | |
| 3120 | + | |
3153 | 3121 | | |
3154 | 3122 | | |
3155 | 3123 | | |
| |||
3254 | 3222 | | |
3255 | 3223 | | |
3256 | 3224 | | |
3257 | | - | |
3258 | | - | |
| 3225 | + | |
3259 | 3226 | | |
3260 | 3227 | | |
3261 | 3228 | | |
| |||
4149 | 4116 | | |
4150 | 4117 | | |
4151 | 4118 | | |
4152 | | - | |
4153 | | - | |
| 4119 | + | |
4154 | 4120 | | |
4155 | 4121 | | |
4156 | 4122 | | |
| |||
4175 | 4141 | | |
4176 | 4142 | | |
4177 | 4143 | | |
4178 | | - | |
4179 | | - | |
| 4144 | + | |
4180 | 4145 | | |
4181 | 4146 | | |
4182 | 4147 | | |
| |||
4211 | 4176 | | |
4212 | 4177 | | |
4213 | 4178 | | |
4214 | | - | |
4215 | | - | |
| 4179 | + | |
4216 | 4180 | | |
4217 | 4181 | | |
4218 | 4182 | | |
| |||
4276 | 4240 | | |
4277 | 4241 | | |
4278 | 4242 | | |
4279 | | - | |
| 4243 | + | |
4280 | 4244 | | |
4281 | 4245 | | |
4282 | 4246 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1321 | 1321 | | |
1322 | 1322 | | |
1323 | 1323 | | |
1324 | | - | |
1325 | | - | |
1326 | | - | |
1327 | | - | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
| 1333 | + | |
| 1334 | + | |
| 1335 | + | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
1328 | 1341 | | |
1329 | 1342 | | |
1330 | 1343 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 10 | + | |
14 | 11 | | |
15 | 12 | | |
16 | 13 | | |
17 | | - | |
| 14 | + | |
18 | 15 | | |
19 | 16 | | |
20 | 17 | | |
21 | 18 | | |
22 | | - | |
| 19 | + | |
23 | 20 | | |
0 commit comments