Skip to content

Commit 09974ff

Browse files
panagosg7facebook-github-bot
authored andcommitted
[flow] add a new IndexContext case to Type.enclosing_context
Summary: * Makes the context of `x` in `o[x]` a special kind of context (`IndexContext`). This flag is now passed alongside the existing conditional context(`cond` parameter), so that it can be used under natural inference to infer more precise types for primitive literals. * Propagates `cond` flag to a few more expressions in statement.ml (e.g. pattern match cases) Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D70676102 fbshipit-source-id: e29765b12473dbdbf7e33d5fb3f3d245b7787312
1 parent 501116a commit 09974ff

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

src/services/autocomplete/autocomplete_js.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module Inference = struct
162162
let type_of_match_member_pattern cx loc mem =
163163
Match_pattern.type_of_member_pattern
164164
cx
165-
~on_identifier:(Statement.identifier ~encl_ctx:Type.NoContext)
165+
~on_identifier:Statement.identifier
166166
~on_expression:Statement.expression
167167
(loc, mem)
168168
end

src/typing/match_pattern.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let rec member cx ~on_identifier ~on_expression mem =
9595
match base with
9696
| BaseIdentifier (loc, id) ->
9797
let exp = (loc, Ast.Expression.Identifier (loc, id)) in
98-
(exp, BaseIdentifier ((loc, on_identifier cx id loc), id))
98+
(exp, BaseIdentifier ((loc, on_identifier ~encl_ctx:Type.OtherTest cx id loc), id))
9999
| BaseMember mem ->
100100
let (exp, mem) = member cx ~on_identifier ~on_expression mem in
101101
(exp, BaseMember mem)
@@ -179,7 +179,7 @@ let rec pattern_ cx ~on_identifier ~on_expression ~on_binding ~in_or_pattern acc
179179
in
180180
AsPattern { AsPattern.pattern = p; target; comments }
181181
| IdentifierPattern (loc, x) ->
182-
let t = on_identifier cx x loc in
182+
let t = on_identifier ~encl_ctx:Type.OtherTest cx x loc in
183183
IdentifierPattern ((loc, t), x)
184184
| BindingPattern x -> BindingPattern (binding_pattern cx ~on_binding ~in_or_pattern ~loc acc x)
185185
| WildcardPattern x -> WildcardPattern x

src/typing/match_pattern.mli

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ module Ast = Flow_ast
99

1010
val pattern :
1111
Context.t ->
12-
on_identifier:(Context.t -> ALoc.t Ast.Identifier.t' -> ALoc.t -> Type.t) ->
12+
on_identifier:
13+
(encl_ctx:Type.enclosing_context -> Context.t -> ALoc.t Ast.Identifier.t' -> ALoc.t -> Type.t) ->
1314
on_expression:
1415
(Context.t -> (ALoc.t, ALoc.t) Ast.Expression.t -> (ALoc.t, ALoc.t * Type.t) Ast.Expression.t) ->
1516
on_binding:
@@ -20,7 +21,8 @@ val pattern :
2021

2122
val type_of_member_pattern :
2223
Context.t ->
23-
on_identifier:(Context.t -> ALoc.t Ast.Identifier.t' -> ALoc.t -> Type.t) ->
24+
on_identifier:
25+
(encl_ctx:Type.enclosing_context -> Context.t -> ALoc.t Ast.Identifier.t' -> ALoc.t -> Type.t) ->
2426
on_expression:
2527
(Context.t -> (ALoc.t, ALoc.t) Ast.Expression.t -> (ALoc.t, ALoc.t * Type.t) Ast.Expression.t) ->
2628
(ALoc.t, ALoc.t) Ast.MatchPattern.MemberPattern.t ->

src/typing/statement.ml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,8 @@ module Make
12211221
Flow_ast.Expression.Identifier (Flow_ast_utils.match_root_ident case_loc)
12221222
)
12231223
pattern
1224-
~on_identifier:(fun cx -> identifier cx empty_syntactic_flags)
1224+
~on_identifier:(fun ~encl_ctx cx ->
1225+
identifier cx { empty_syntactic_flags with Primitive_literal.encl_ctx })
12251226
~on_expression:expression
12261227
~on_binding:(fun ~use_op ~name_loc ~kind name t ->
12271228
init_var kind cx ~use_op t name_loc;
@@ -2493,7 +2494,7 @@ module Make
24932494
shorthand;
24942495
}
24952496
) ->
2496-
let (((_, kt), _) as k') = expression cx k in
2497+
let (((_, kt), _) as k') = expression cx ~encl_ctx:IndexContext k in
24972498
let (((_, vt), _) as v') = expression cx ~as_const v in
24982499
let computed = mk_computed k kt vt in
24992500
( ObjectExpressionAcc.add_computed cx computed acc,
@@ -2516,7 +2517,7 @@ module Make
25162517
value = (fn_loc, fn);
25172518
}
25182519
) ->
2519-
let (((_, kt), _) as k') = expression cx k in
2520+
let (((_, kt), _) as k') = expression cx ~encl_ctx:IndexContext k in
25202521
let ((_, vt), v') = expression cx (fn_loc, Ast.Expression.Function fn) in
25212522
let fn =
25222523
match v' with
@@ -2946,7 +2947,8 @@ module Make
29462947
cx
29472948
(case_loc, Identifier (Flow_ast_utils.match_root_ident case_loc))
29482949
pattern
2949-
~on_identifier:(fun cx -> identifier cx empty_syntactic_flags)
2950+
~on_identifier:(fun ~encl_ctx cx ->
2951+
identifier cx { empty_syntactic_flags with Primitive_literal.encl_ctx })
29502952
~on_expression:expression
29512953
~on_binding:(fun ~use_op ~name_loc ~kind name t ->
29522954
init_var kind cx ~use_op t name_loc;
@@ -4539,7 +4541,7 @@ module Make
45394541
)
45404542
in
45414543
let eval_index () =
4542-
let (((_, tind), _) as index) = expression cx index in
4544+
let (((_, tind), _) as index) = expression ~encl_ctx:IndexContext cx index in
45434545
(tind, index)
45444546
in
45454547
let conf =
@@ -4836,7 +4838,7 @@ module Make
48364838
)
48374839
in
48384840
let eval_args_and_expr () =
4839-
let (((_, elem_t), _) as expr) = expression cx expr in
4841+
let (((_, elem_t), _) as expr) = expression ~encl_ctx:IndexContext cx expr in
48404842
let (argts, arguments_ast) = arg_list cx arguments in
48414843
((argts, elem_t), (arguments_ast, expr))
48424844
in
@@ -4973,7 +4975,8 @@ module Make
49734975
| SwitchTest _
49744976
| OtherTest ->
49754977
BoolModuleT.at loc
4976-
| NoContext ->
4978+
| NoContext
4979+
| IndexContext ->
49774980
let reason = mk_reason (RUnaryOperator ("not", desc_of_t arg)) loc in
49784981
Operators.unary_not cx reason arg
49794982
in
@@ -5074,7 +5077,9 @@ module Make
50745077
and visit_eq_test cx ~encl_ctx loc left right =
50755078
let check ~encl_ctx = expression cx ~encl_ctx in
50765079
match encl_ctx with
5077-
| NoContext -> check ~encl_ctx:NoContext
5080+
| NoContext
5081+
| IndexContext ->
5082+
check ~encl_ctx
50785083
| SwitchTest _
50795084
| OtherTest ->
50805085
let reconstruct_ast = check ~encl_ctx:OtherTest in
@@ -5142,7 +5147,9 @@ module Make
51425147
let (((_, t2), _) as right) = reconstruct_ast right in
51435148
begin
51445149
match encl_ctx with
5145-
| NoContext -> ()
5150+
| NoContext
5151+
| IndexContext ->
5152+
()
51465153
| SwitchTest _
51475154
| OtherTest ->
51485155
matching_prop_check cx left right;
@@ -5452,7 +5459,7 @@ module Make
54525459
let reason = mk_reason (RPropertyAssignment None) lhs_loc in
54535460
let lhs_reason = mk_expression_reason _object in
54545461
let (o, _object) = typecheck_object _object in
5455-
let (((_, i), _) as index) = expression cx index in
5462+
let (((_, i), _) as index) = expression ~encl_ctx:IndexContext cx index in
54565463
let use_op = make_op ~lhs:reason ~prop:(mk_reason (desc_of_reason lhs_prop_reason) iloc) in
54575464
let upper = maybe_chain lhs_reason (SetElemT (use_op, reason, i, mode, t, None)) in
54585465
Flow.flow cx (o, upper);
@@ -6471,7 +6478,8 @@ module Make
64716478
| SwitchTest _
64726479
| OtherTest ->
64736480
OptTestPropT (use_op, reason, id, mk_named_prop ~reason:prop_reason prop_name, hint)
6474-
| NoContext ->
6481+
| NoContext
6482+
| IndexContext ->
64756483
OptGetPropT
64766484
{
64776485
use_op;

src/typing/type.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,8 @@ module rec TypeTerm : sig
897897
}
898898
(* `if(C){}`, `while(C){}`, `C?e1:e2`, `invariant(C)`, ... *)
899899
| OtherTest
900+
(* `o[C]`, `o[C]=e`, `{[C]:e}`, `o[C]()` *)
901+
| IndexContext
900902

901903
(* Bindings created from destructuring annotations should themselves act like
902904
* annotations. That is, `var {p}: {p: string}; p = 0` should be an error,

src/typing/type_operation_utils.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ module Operators = struct
256256
explanation = None;
257257
}
258258
| NoContext
259+
| IndexContext
259260
| OtherTest ->
260261
let reasons = FlowError.ordered_reasons (reason_of_t l, reason_of_t r) in
261262
Error_message.EComparison reasons)
@@ -282,6 +283,7 @@ module Operators = struct
282283
match encl_ctx with
283284
| SwitchTest _ -> Some (Lazy.force comparison_error)
284285
| NoContext
286+
| IndexContext
285287
| OtherTest ->
286288
None
287289
end

0 commit comments

Comments
 (0)