Skip to content

Commit 8be18ab

Browse files
panagosg7facebook-github-bot
authored andcommitted
[flow] add experimental.natural_inference.local_primitive_literals option
Summary: Adds a flag that will enable the new behavior of natural inference on primitive literals and const-vars initialized with primitive literals. The valid values of the `experimental.natural_inference.local_primitive_literals` flowconfig option enable a two phase rollout for this behavior: * "partial" mode enables precise inference (singleton literal types) for literals/const-variables when necessary (e.g. in the presence of annotations, `as const`, etc.) * "full" mode additionally enables general types (`StrGeneralT`, etc.) to be inferred for literals/const-variables otherwise (instead of `StrT_UNSOUND`, etc. that is inferred today) Changelog: [internal] Reviewed By: gkz Differential Revision: D70712087 fbshipit-source-id: 757ff37a87669803a1a1cc817cabbb76a8927dcc
1 parent 5ad243c commit 8be18ab

File tree

10 files changed

+55
-0
lines changed

10 files changed

+55
-0
lines changed

src/commands/commandUtils.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,8 @@ let make_options
14641464
opt_strict_mode = strict_mode;
14651465
opt_merge_timeout;
14661466
opt_missing_module_generators = FlowConfig.missing_module_generators flowconfig;
1467+
opt_natural_inference_exports_primitive_const =
1468+
FlowConfig.natural_inference_local_primitive_literals flowconfig;
14671469
opt_no_unchecked_indexed_access = FlowConfig.no_unchecked_indexed_access flowconfig;
14681470
opt_saved_state_fetcher;
14691471
opt_saved_state_force_recheck = saved_state_options_flags.saved_state_force_recheck;

src/commands/config/flowConfig.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ module Opts = struct
113113
multi_platform_extension_group_mapping: (string * string list) list;
114114
multi_platform_ambient_supports_platform_directory_overrides: (string * string list) list;
115115
munge_underscores: bool;
116+
natural_inference_local_primitive_literals: Options.NaturalInferenceLevel.t;
116117
no_flowlib: bool;
117118
no_unchecked_indexed_access: bool;
118119
node_main_fields: string list;
@@ -250,6 +251,7 @@ module Opts = struct
250251
multi_platform_extension_group_mapping = [];
251252
multi_platform_ambient_supports_platform_directory_overrides = [];
252253
munge_underscores = false;
254+
natural_inference_local_primitive_literals = Options.NaturalInferenceLevel.Off;
253255
no_flowlib = false;
254256
no_unchecked_indexed_access = false;
255257
node_main_fields = ["main"];
@@ -1124,6 +1126,15 @@ module Opts = struct
11241126
("module.use_strict", boolean (fun opts v -> Ok { opts with modules_are_use_strict = v }));
11251127
("munge_underscores", boolean (fun opts v -> Ok { opts with munge_underscores = v }));
11261128
("name", root_name_parser);
1129+
( "experimental.natural_inference.local_primitive_literals",
1130+
enum
1131+
[
1132+
("off", Options.NaturalInferenceLevel.Off);
1133+
("partial", Options.NaturalInferenceLevel.Partial);
1134+
("full", Options.NaturalInferenceLevel.Full);
1135+
]
1136+
(fun opts v -> Ok { opts with natural_inference_local_primitive_literals = v })
1137+
);
11271138
("no_flowlib", boolean (fun opts v -> Ok { opts with no_flowlib = v }));
11281139
( "no_unchecked_indexed_access",
11291140
boolean (fun opts v -> Ok { opts with no_unchecked_indexed_access = v })
@@ -1894,6 +1905,9 @@ let multi_platform_ambient_supports_platform_directory_overrides c =
18941905

18951906
let munge_underscores c = c.options.Opts.munge_underscores
18961907

1908+
let natural_inference_local_primitive_literals c =
1909+
c.options.Opts.natural_inference_local_primitive_literals
1910+
18971911
let no_flowlib c = c.options.Opts.no_flowlib
18981912

18991913
let no_unchecked_indexed_access c = c.options.Opts.no_unchecked_indexed_access

src/commands/config/flowConfig.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ val multi_platform_ambient_supports_platform_directory_overrides :
208208

209209
val munge_underscores : config -> bool
210210

211+
val natural_inference_local_primitive_literals : config -> Options.NaturalInferenceLevel.t
212+
211213
val no_flowlib : config -> bool
212214

213215
val no_unchecked_indexed_access : config -> bool

src/common/options.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ type react_rules =
4242
| DeepReadOnlyHookReturns
4343
| RulesOfHooks
4444

45+
module NaturalInferenceLevel = struct
46+
type t =
47+
| Off
48+
| Partial
49+
| Full
50+
end
51+
4552
type format = {
4653
opt_bracket_spacing: bool;
4754
opt_single_quotes: bool;
@@ -128,6 +135,7 @@ type t = {
128135
opt_module_name_mappers: (Str.regexp * string) list;
129136
opt_modules_are_use_strict: bool;
130137
opt_munge_underscores: bool;
138+
opt_natural_inference_exports_primitive_const: NaturalInferenceLevel.t;
131139
opt_no_unchecked_indexed_access: bool;
132140
opt_node_main_fields: string list;
133141
opt_node_package_export_conditions: string list;
@@ -302,6 +310,9 @@ let module_system opts = opts.opt_module
302310

303311
let modules_are_use_strict opts = opts.opt_modules_are_use_strict
304312

313+
let natural_inference_local_primitive_literals opts =
314+
opts.opt_natural_inference_exports_primitive_const
315+
305316
let no_unchecked_indexed_access opts = opts.opt_no_unchecked_indexed_access
306317

307318
let node_main_fields opts = opts.opt_node_main_fields

src/flow_dot_js.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ let stub_metadata ~root ~checked =
125125
max_literal_length = 100;
126126
max_workers = 0;
127127
missing_module_generators = [];
128+
natural_inference_local_primitive_literals = Options.NaturalInferenceLevel.Off;
128129
no_unchecked_indexed_access = false;
129130
react_custom_jsx_typing = false;
130131
react_ref_as_prop = Options.ReactRefAsProp.PartialSupport;

src/services/code_action/__tests__/refactor_extract_utils_tests.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ let stub_metadata ~root ~checked =
5353
max_literal_length = 100;
5454
max_workers = 0;
5555
missing_module_generators = [];
56+
natural_inference_local_primitive_literals = Options.NaturalInferenceLevel.Off;
5657
no_unchecked_indexed_access = false;
5758
react_custom_jsx_typing = false;
5859
react_ref_as_prop = Options.ReactRefAsProp.PartialSupport;

src/typing/__tests__/type_hint_test.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ let metadata =
4848
max_literal_length = 100;
4949
max_workers = 0;
5050
missing_module_generators = [];
51+
natural_inference_local_primitive_literals = Options.NaturalInferenceLevel.Off;
5152
no_unchecked_indexed_access = false;
5253
react_custom_jsx_typing = false;
5354
react_ref_as_prop = Options.ReactRefAsProp.PartialSupport;

src/typing/__tests__/typed_ast_test.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ let metadata =
4646
max_literal_length = 100;
4747
max_workers = 0;
4848
missing_module_generators = [];
49+
natural_inference_local_primitive_literals = Options.NaturalInferenceLevel.Off;
4950
no_unchecked_indexed_access = false;
5051
react_custom_jsx_typing = false;
5152
react_ref_as_prop = Options.ReactRefAsProp.PartialSupport;

src/typing/context.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type metadata = {
5757
max_literal_length: int;
5858
max_workers: int;
5959
missing_module_generators: (Str.regexp * string) list;
60+
natural_inference_local_primitive_literals: Options.NaturalInferenceLevel.t;
6061
no_unchecked_indexed_access: bool;
6162
react_custom_jsx_typing: bool;
6263
react_ref_as_prop: Options.ReactRefAsProp.t;
@@ -290,6 +291,8 @@ let metadata_of_options options =
290291
max_literal_length = Options.max_literal_length options;
291292
max_workers = Options.max_workers options;
292293
missing_module_generators = Options.missing_module_generators options;
294+
natural_inference_local_primitive_literals =
295+
Options.natural_inference_local_primitive_literals options;
293296
no_unchecked_indexed_access = Options.no_unchecked_indexed_access options;
294297
react_custom_jsx_typing = Options.react_custom_jsx_typing options;
295298
react_ref_as_prop = Options.react_ref_as_prop options;
@@ -663,6 +666,20 @@ let max_workers cx = cx.metadata.max_workers
663666

664667
let missing_module_generators cx = cx.metadata.missing_module_generators
665668

669+
let natural_inference_local_primitive_literals_partial cx =
670+
match cx.metadata.natural_inference_local_primitive_literals with
671+
| Options.NaturalInferenceLevel.Off -> false
672+
| Options.NaturalInferenceLevel.Partial
673+
| Options.NaturalInferenceLevel.Full ->
674+
true
675+
676+
let natural_inference_local_primitive_literals_full cx =
677+
match cx.metadata.natural_inference_local_primitive_literals with
678+
| Options.NaturalInferenceLevel.Off
679+
| Options.NaturalInferenceLevel.Partial ->
680+
false
681+
| Options.NaturalInferenceLevel.Full -> true
682+
666683
let no_unchecked_indexed_access cx = cx.metadata.no_unchecked_indexed_access
667684

668685
let jsx cx = cx.metadata.jsx

src/typing/context.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ type metadata = {
107107
max_literal_length: int;
108108
max_workers: int;
109109
missing_module_generators: (Str.regexp * string) list;
110+
natural_inference_local_primitive_literals: Options.NaturalInferenceLevel.t;
110111
no_unchecked_indexed_access: bool;
111112
react_custom_jsx_typing: bool;
112113
react_ref_as_prop: Options.ReactRefAsProp.t;
@@ -317,6 +318,10 @@ val max_workers : t -> int
317318

318319
val missing_module_generators : t -> (Str.regexp * string) list
319320

321+
val natural_inference_local_primitive_literals_partial : t -> bool
322+
323+
val natural_inference_local_primitive_literals_full : t -> bool
324+
320325
val no_unchecked_indexed_access : t -> bool
321326

322327
val jsx : t -> Options.jsx_mode

0 commit comments

Comments
 (0)