forked from facebook/flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreason.ml
629 lines (568 loc) · 19.9 KB
/
reason.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
(**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "flow" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
(* This module defines a general notion of trace, which is used in modules
Type_inference_js and Flow_js to record how the typechecker reasons about
code, systematically collecting, simplifying, and solving constraints. This
is extremely useful, not only for debugging the typechecker but also to
really understand why an error is reported. *)
(* Eventually, trace information should be printed out only in verbose mode,
since Flow reports all errors it finds and the trace for every error can get
quite detailed dependening on how far apart the "source" and "sink" are and
how convoluted the flow between them is. *)
open Utils_js
open String_utils
let mk_id () = Ident.make ""
(* Reasons are included in types mainly for error reporting, but sometimes we
also use reasons in types to recover information on the source code that
caused those reasons to be created. Two examples of such secondary uses of
reasons are:
- strictness analysis: we use reasons to locate the origin of an object and
the origin of an operation on the object, and use such origins to determine
whether certain errors should be suppressed.
- termination analysis: we use reasons to limit instantiation of type
parameters in polymorphic types at particular locations, to prevent the type
checker from generating an unbounded number of constraints. The `pos` field
of reasons is sufficient to distinguish code locations, except that as an
implementation strategy for checking polymorphic definitions, we walk over
the same source code multiple times to check it with different instantiations
of type parameters, and to index "copies" of the reasons created in those
passes over the same source code, we use an additional `test_id` field.
*)
module TestID = struct
let _current = ref None
(* Get current test id. *)
let current() = !_current
(* Call f on a, installing new_test_id as the current test_id, and restoring
the current test_id when done. (See also the function mk_reason below.) *)
let run f a =
let test_id = current () in
_current := Some (mk_id ());
f a;
_current := test_id
end
type reason_desc =
| RNumber | RString | RBoolean | RMixed | REmpty | RAny | RVoid | RNull
| RStringLit of string
| RNumberLit of string
| RBooleanLit of bool
| RObject
| RObjectLit
| RObjectType
| RObjectClassName
| RArray
| RArrayLit
| REmptyArrayLit
| RArrayType
| RROArrayType
| RTupleType
| RTupleElement
| RTupleOutOfBoundsAccess
| RFunction of reason_desc_function
| RFunctionType
| RAbstractMethodType
| RFunctionBody
| RFunctionCall
| RFunctionUnusedArgument
| RJSXFunctionCall of string
| RJSXIdentifier of string * string
| RJSXElementProps of string
| RJSXElement of string option
| RJSXText
| RAnyObject
| RAnyFunction
| RUnknownString
| RStringEnum
| RNumberEnum
| RGetterSetterProperty
| RThis
| RThisType
| RExistential
| RTooFewArgs
| RTooFewArgsExpectedRest
| RUninitializedThis
| RConstructorReturn
| RNewObject
| RUnion
| RUnionType
| RIntersection
| RIntersectionType
| RKeySet
| RAnd
| RConditional
| RPrototype
| RDestructuring
| RConstructor
| RDefaultConstructor
| RConstructorCall
| RReturn
| RRegExp
| RSuper
| RNoSuper
| RDummyPrototype
| RDummyThis
| RTupleMap
| RObjectMap
| RObjectMapi
| RType of string
| RTypeParam of string * reason_desc
| RMethodCall of string option
| RParameter of string
| RRestParameter of string
| RIdentifier of string
| RIdentifierAssignment of string
| RPropertyAssignment of string
| RProperty of string option
| RShadowProperty of string
| RPropertyOf of string * reason_desc
| RPropertyIsAString of string
| RMissingProperty of string option
| RUnknownProperty of string option
| RSomeProperty
| RNameProperty of reason_desc
| RMissingAbstract of reason_desc
| RFieldInitializer of string
| RCustom of string
| RPolyType of reason_desc
| RClassType of reason_desc
| RExactType of reason_desc
| ROptional of reason_desc
| RMaybe of reason_desc
| RRestArray of reason_desc
| RAbstract of reason_desc
| RAbstracts of reason_desc
| RDummyAbstracts
| RTypeApp of reason_desc
| RThisTypeApp of reason_desc
| RExtends of reason_desc
| RStatics of reason_desc
| RSuperOf of reason_desc
| RFrozen of reason_desc
| RBound of reason_desc
| RVarianceCheck of reason_desc
| RPredicateOf of reason_desc
| RPredicateCall of reason_desc
| RPredicateCallNeg of reason_desc
| RIncompatibleInstantiation of string
| RSpreadOf of reason_desc
| RObjectPatternRestProp
| RArrayPatternRestProp
| RCommonJSExports of string
| RReactElement of string option
| RReactClass
| RReactComponent
| RReactStatics
| RReactDefaultProps
| RReactState
| RReactElementProps of string
| RReactPropTypes
and reason_desc_function =
| RAsync
| RGenerator
| RAsyncGenerator
| RNormal
type reason = {
test_id: int option;
derivable: bool;
desc: reason_desc;
loc: Loc.t;
def_loc_opt: Loc.t option;
}
type t = reason
let lexpos file line col = {
Lexing.pos_fname = file;
Lexing.pos_lnum = line;
Lexing.pos_bol = 0;
Lexing.pos_cnum = col;
}
let diff_range loc = Loc.(
let line1, line2 = loc.start.line, loc._end.line in
(* TODO: Get rid of +1 which is here to ensure same behavior as old code
using Pos.info_pos *)
let start, end_ = loc.start.column + 1, loc._end.column in
(line2 - line1, end_ - start)
)
let in_range loc range = Loc.(
let line, line1, line2 = loc.start.line, range.start.line, range._end.line in
(line1 < line || (line = line1 && range.start.column <= loc.start.column)) &&
(line < line2 || (line = line2 && loc._end.column <= range._end.column))
)
let rec patch ll offset lines = function
| [] -> ()
| (l,c,str)::insertions ->
let c = if l = ll then c + offset else c in
let del = try Some (int_of_string str) with _ -> None in
let line = lines.(l - 1) in
let shift = match del with
| Some n -> (* delete n chars at l, c *)
lines.(l - 1) <- spf "%s%s"
(string_before line c) (string_after line (c + n));
-n
| None -> (* insert str at l, c *)
lines.(l - 1) <- spf "%s%s%s"
(string_before line c) str (string_after line c);
String.length str
in
let offset = (if l = ll then offset else 0) + shift in
patch l offset lines insertions
let do_patch lines insertions =
let lines = Array.of_list lines in
patch 1 0 lines insertions;
String.concat "\n" (Array.to_list lines)
let string_of_source ?(strip_root=None) = Loc.(function
| Builtins -> "(builtins)"
| LibFile file ->
begin match strip_root with
| Some root ->
let root_str = spf "%s%s" (Path.to_string root) Filename.dir_sep in
if string_starts_with file root_str
then spf "[LIB] %s" (Files.relative_path root_str file)
else spf "[LIB] %s" (Filename.basename file)
| None -> file
end
| SourceFile file
| JsonFile file
| ResourceFile file ->
begin match strip_root with
| Some root ->
let root_str = spf "%s%s" (Path.to_string root) Filename.dir_sep in
Files.relative_path root_str file
| None ->
file
end
)
let string_of_loc_pos loc = Loc.(
let line = loc.start.line in
let start = loc.start.column + 1 in
let end_ = loc._end.column in
if line <= 0 then
"0:0"
else if line = loc._end.line && start = end_ then
spf "%d:%d" line start
else if line != loc._end.line then
spf "%d:%d,%d:%d" line start loc._end.line end_
else
spf "%d:%d-%d" line start end_
)
let string_of_loc ?(strip_root=None) loc = Loc.(
match loc.source with
| None
| Some Builtins -> ""
| Some file ->
spf "%s:%s" (string_of_source ~strip_root file) (string_of_loc_pos loc)
)
let json_of_loc ?(strip_root=None) loc = Hh_json.(Loc.(
JSON_Object [
"source", (
match loc.source with
| Some x -> JSON_String (string_of_source ~strip_root x)
| None -> JSON_Null
);
"type", (match loc.source with
| Some LibFile _ -> JSON_String "LibFile"
| Some SourceFile _ -> JSON_String "SourceFile"
| Some JsonFile _ -> JSON_String "JsonFile"
| Some ResourceFile _ -> JSON_String "ResourceFile"
| Some Builtins -> JSON_String "Builtins"
| None -> JSON_Null);
"start", JSON_Object [
"line", int_ loc.start.line;
(* It's not ideal that we use a different column numbering system here
* versus other places (like the estree translator) *)
"column", int_ (loc.start.column + 1);
"offset", int_ loc.start.offset;
];
"end", JSON_Object [
"line", int_ loc._end.line;
"column", int_ loc._end.column;
"offset", int_ loc._end.offset;
];
]
))
(* reason constructors, accessors, etc. *)
let mk_reason_with_test_id test_id desc loc def_loc_opt = {
test_id;
derivable = false;
desc;
loc;
def_loc_opt;
}
(* The current test_id is included in every new reason. *)
let mk_reason desc loc =
mk_reason_with_test_id (TestID.current()) desc loc None
(* Lift a string to a reason. Usually used as a dummy reason. *)
let locationless_reason desc =
mk_reason_with_test_id None desc Loc.none None
let func_reason {Ast.Function.async; generator; _} =
let func_desc = match async, generator with
| true, true -> RAsyncGenerator
| true, false -> RAsync
| false, true -> RGenerator
| false, false -> RNormal
in
mk_reason (RFunction func_desc)
let loc_of_reason r = r.loc
let def_loc_of_reason r =
match r.def_loc_opt with
| Some loc -> loc
| None -> loc_of_reason r
let function_desc_prefix = function
| RAsync -> "async "
| RGenerator -> "generator "
| RAsyncGenerator -> "async generator "
| RNormal -> ""
let rec string_of_desc = function
| RNumber -> "number"
| RString -> "string"
| RBoolean -> "boolean"
| RMixed -> "mixed"
| REmpty -> "empty"
| RAny -> "any"
| RVoid -> "undefined"
| RNull -> "null"
| RStringLit x -> spf "string literal `%s`" x
| RNumberLit x -> spf "number literal `%s`" x
| RBooleanLit b -> spf "boolean literal `%s`" (string_of_bool b)
| RObject -> "object"
| RObjectLit -> "object literal"
| RObjectType -> "object type"
| RObjectClassName -> "Object"
| RArray -> "array"
| RArrayLit -> "array literal"
| REmptyArrayLit -> "empty array literal"
| RArrayType -> "array type"
| RROArrayType -> "read-only array type"
| RTupleType -> "tuple type"
| RTupleElement -> "tuple element"
| RTupleOutOfBoundsAccess -> "undefined (out of bounds tuple access)"
| RFunction func -> spf "%sfunction" (function_desc_prefix func)
| RFunctionType -> "function type"
| RAbstractMethodType -> "abstract method type"
| RFunctionBody -> "function body"
| RFunctionCall -> "function call"
| RFunctionUnusedArgument -> "unused function argument"
| RJSXFunctionCall raw_jsx -> spf "JSX desugared to `%s(...)`" raw_jsx
| RJSXIdentifier (raw_jsx, name) ->
spf "JSX desugared to `%s(...)`. identifier %s" raw_jsx name
| RJSXElement x ->
(match x with
| Some x -> spf "JSX element `%s`" x
| None -> "JSX element")
| RJSXElementProps x -> spf "props of JSX element `%s`" x
| RJSXText -> spf "JSX text"
| RAnyObject -> "any object"
| RAnyFunction -> "any function"
| RUnknownString -> "some string with unknown value"
| RStringEnum -> "string enum"
| RNumberEnum -> "number enum"
| RGetterSetterProperty -> "getter/setter property"
| RThis -> "this"
| RThisType -> "`this` type"
| RExistential -> "existential"
| RTooFewArgs -> "undefined (too few arguments)"
| RTooFewArgsExpectedRest ->
"undefined (too few arguments, expected default/rest parameters)"
| RUninitializedThis -> "uninitialized this (expected super constructor call)"
| RConstructorReturn -> "constructor return"
| RNewObject -> "new object"
| RUnion -> "union"
| RUnionType -> "union type"
| RIntersection -> "intersection"
| RIntersectionType -> "intersection type"
| RKeySet -> "key set"
| RAnd -> "and"
| RConditional -> "conditional"
| RPrototype -> "prototype"
| RDestructuring -> "destructuring"
| RConstructor -> "constructor"
| RDefaultConstructor -> "default constructor"
| RConstructorCall -> "constructor call"
| RReturn -> "return"
| RRegExp -> "regexp"
| RSuper -> "`super` pseudo-expression"
| RNoSuper -> "empty super object"
| RDummyPrototype -> "empty prototype object"
| RDummyThis -> "bound `this` in method"
| RTupleMap -> "tuple map"
| RObjectMap -> "object map"
| RObjectMapi -> "object mapi"
| RType x -> spf "type `%s`" x
| RTypeParam (x,d) -> spf "type parameter `%s` of %s" x (string_of_desc d)
| RIdentifier x -> spf "identifier `%s`" x
| RIdentifierAssignment x -> spf "assignment of identifier `%s`" x
| RMethodCall (Some x) -> spf "call of method `%s`" x
| RMethodCall None -> "call of computed property"
| RParameter x -> spf "parameter `%s`" x
| RRestParameter x -> spf "rest parameter `%s`" x
| RProperty (Some x) -> spf "property `%s`" x
| RProperty None -> "computed property"
| RPropertyAssignment x -> spf "assignment of property `%s`" x
| RShadowProperty x -> spf ".%s" x
| RPropertyOf (x, d) -> spf "property `%s` of %s" x (string_of_desc d)
| RPropertyIsAString x -> spf "property `%s` is a string" x
| RMissingProperty (Some x) -> spf "property `%s` does not exist" x
| RMissingProperty None -> "computed property does not exist"
| RUnknownProperty (Some x) -> spf "property `%s` of unknown type" x
| RUnknownProperty None -> "computed property of unknown type"
| RSomeProperty -> "some property"
| RNameProperty d -> spf "property `name` of %s" (string_of_desc d)
| RMissingAbstract d ->
spf "undefined. Did you forget to declare %s?" (string_of_desc d)
| RFieldInitializer x -> spf "field initializer for `%s`" x
| RCustom x -> x
| RPolyType d -> spf "polymorphic type: %s" (string_of_desc d)
| RClassType d -> spf "class type: %s" (string_of_desc d)
| RExactType d -> spf "exact type: %s" (string_of_desc d)
| ROptional d -> spf "optional %s" (string_of_desc d)
| RMaybe d -> spf "?%s" (string_of_desc d)
| RRestArray d -> spf "rest parameter array of %s" (string_of_desc d)
| RAbstract d -> spf "abstract %s" (string_of_desc d)
| RAbstracts d -> spf "abstracts of %s" (string_of_desc d)
| RDummyAbstracts -> "vacuous abstracts"
| RTypeApp d -> spf "type application of %s" (string_of_desc d)
| RThisTypeApp d -> spf "this instantiation of %s" (string_of_desc d)
| RExtends d -> spf "extends %s" (string_of_desc d)
| RStatics d -> spf "statics of %s" (string_of_desc d)
| RSuperOf d -> spf "super of %s" (string_of_desc d)
| RFrozen d -> spf "frozen %s" (string_of_desc d)
| RBound d -> spf "bound %s" (string_of_desc d)
| RVarianceCheck d -> spf "variance check: %s" (string_of_desc d)
| RPredicateOf d -> spf "predicate of %s" (string_of_desc d)
| RPredicateCall d -> spf "predicate call to %s" (string_of_desc d)
| RPredicateCallNeg d ->
spf "negation of predicate call to %s" (string_of_desc d)
| RIncompatibleInstantiation x -> spf "some incompatible instantiation of `%s`" x
| RSpreadOf d -> spf "spread of %s" (string_of_desc d)
| RObjectPatternRestProp -> "rest of object pattern"
| RArrayPatternRestProp -> "rest of array pattern"
| RCommonJSExports x -> spf "CommonJS exports of %S" x
| RReactElement x ->
(match x with
| Some x -> spf "React element `%s`" x
| None -> "React element")
| RReactClass -> "React class"
| RReactComponent -> "React component"
| RReactStatics -> "statics of React class"
| RReactDefaultProps -> "default props of React component"
| RReactState -> "state of React component"
| RReactElementProps x -> spf "props of React element `%s`" x
| RReactPropTypes -> "propTypes of React component"
let string_of_reason ?(strip_root=None) r =
let spos = string_of_loc ~strip_root (loc_of_reason r) in
let desc = string_of_desc r.desc in
if spos = ""
then desc
else (
if desc = ""
then spos
else spf "%s:\n%s" spos desc
)
let json_of_reason ?(strip_root=None) r = Hh_json.(
JSON_Object ([
"pos", json_of_loc ~strip_root (loc_of_reason r);
"desc", JSON_String (string_of_desc r.desc)
])
)
let dump_reason ?(strip_root=None) r =
spf "%s: %S%s"
(string_of_loc ~strip_root (loc_of_reason r))
(string_of_desc r.desc)
begin match r.test_id with
| Some n -> spf " (test %d)" n
| None -> ""
end
let desc_of_reason r =
r.desc
let internal_name name =
spf ".%s" name
let is_internal_name name =
String.length name >= 1 && name.[0] = '.'
let internal_module_name name =
spf ".$module__%s" name
let is_internal_module_name name =
string_starts_with name ".$module__"
let internal_pattern_name loc =
spf ".$pattern__%s" (string_of_loc loc)
(* Instantiable reasons identify tvars that are created for the purpose of
instantiation: they are fresh rather than shared, and should become types
that flow to them. We assume these characteristics when performing
speculative matching (even though we don't yet enforce them). *)
let is_instantiable_reason r =
match desc_of_reason r with
| RTypeParam _
| RThisType
| RExistential -> true
| _ -> false
(* TODO: Property accesses create unresolved tvars to hold results, even when
the object(s) on which the property accesses happen may be resolved. This can
and should be fixed, for various benefits including but not limited to more
precise type inference. But meanwhile we need to consider results of property
accesses that might result in sentinel property values as constants to decide
membership in disjoint unions, instead of asking for unnecessary annotations
to make progress. According to Facebook's style guide, constant properties
should have names like CONSTANT_PROPERTY, so we bet that when properties with
such names are accessed, their types have the 0->1 property.
As an example, suppose that we have an object `Tags` that stores tags of a
disjoint union, e.g. { ACTION_FOO: 'foo', ACTION_BAR: 'bar' }.
Then the types of Tags.ACTION_FOO and Tags.ACTION_BAR are assumed to be 0->1.
*)
let is_constant_property_reason r =
match desc_of_reason r with
| RProperty (Some x)
| RPropertyOf (x,_)
| RPropertyIsAString x ->
let len = String.length x in
if len = 0
then false
else is_not_lowercase x 0 (len - 1)
| _ -> false
let is_typemap_reason r =
match desc_of_reason r with
| RTupleMap
| RObjectMap
| RObjectMapi -> true
| _ -> false
let is_derivable_reason r =
r.derivable
let derivable_reason r =
{ r with derivable = true }
let builtin_reason desc =
mk_reason desc Loc.({ none with source = Some Builtins })
|> derivable_reason
let is_builtin_reason r =
Loc.(r.loc.source = Some Builtins)
let is_lib_reason r =
Loc.(match r.loc.source with
| Some LibFile _ -> true
| Some Builtins -> true
| Some SourceFile _ -> false
| Some JsonFile _ -> false
| Some ResourceFile _ -> false
| None -> false)
let is_blamable_reason r =
not Loc.(r.loc = none || is_lib_reason r)
let reasons_overlap r1 r2 =
Loc.(contains r1.loc r2.loc)
(* reason transformers: *)
(* returns reason with new description and position of original *)
let replace_reason f r =
mk_reason (f (desc_of_reason r)) (loc_of_reason r)
let replace_reason_const desc r =
mk_reason desc (loc_of_reason r)
(* returns reason with new location and description of original *)
let repos_reason loc reason =
let def_loc_opt =
let def_loc = def_loc_of_reason reason in
if loc = def_loc then None else Some def_loc
in
mk_reason_with_test_id reason.test_id (desc_of_reason reason) loc def_loc_opt
module ReasonMap = MyMap.Make(struct
type t = reason
let compare = Pervasives.compare
end)