Skip to content

Commit c83d2e8

Browse files
arthaudfacebook-github-bot
authored andcommitted
Add origins to await expressions
Summary: We also create implicit await expressions during preprocessing and control flow graph building, thus those need origins. Reviewed By: tianhan0 Differential Revision: D74737704 fbshipit-source-id: 8ff7d68d1f0d011884c8748d3e2b70d05e7684a2
1 parent cb07752 commit c83d2e8

24 files changed

+261
-147
lines changed

source/analysis/attributeResolution.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,8 +3425,8 @@ class base ~queries:(Queries.{ controls; get_class_summary; class_hierarchy; _ }
34253425
in
34263426
let order = self#full_order ~cycle_detections in
34273427
match Node.value expression with
3428-
| Expression.Await expression ->
3429-
self#resolve_literal ~cycle_detections expression ~scoped_type_variables
3428+
| Expression.Await { Await.operand; origin = _ } ->
3429+
self#resolve_literal ~cycle_detections operand ~scoped_type_variables
34303430
|> Type.awaitable_value
34313431
|> Option.value ~default:Type.Any
34323432
| BooleanOperator { BooleanOperator.left; right; _ } ->

source/analysis/decoratorPreprocessing.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,21 +374,22 @@ let convert_parameter_to_argument ~location { Node.value = { Parameter.name; _ }
374374

375375

376376
let create_function_call ~should_await ~location ~callee_name arguments =
377+
let origin = Some (Origin.create ~location Origin.DecoratorInlining) in
377378
let call =
378379
Expression.Call
379380
{
380381
callee =
381382
Expression.Name
382-
(create_name_from_reference
383-
~location
384-
~create_origin:(fun _ -> Some (Origin.create ~location Origin.DecoratorInlining))
385-
callee_name)
383+
(create_name_from_reference ~location ~create_origin:(fun _ -> origin) callee_name)
386384
|> Node.create ~location;
387385
arguments;
388-
origin = Some (Origin.create ~location Origin.DecoratorInlining);
386+
origin;
389387
}
390388
in
391-
if should_await then Expression.Await (Node.create ~location call) else call
389+
if should_await then
390+
Expression.Await { Await.operand = Node.create ~location call; origin }
391+
else
392+
call
392393

393394

394395
let create_function_call_to ~location ~callee_name { Define.Signature.parameters; async; _ } =

source/analysis/globalLeakCheck.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ module State (Context : Context) = struct
318318
| Expression.Constant _
319319
| Yield None ->
320320
empty_result
321-
| Await expression
321+
| Await { Await.operand = expression; origin = _ }
322322
| Yield (Some expression)
323323
| YieldFrom expression
324324
| UnaryOperator { operand = expression; _ }

source/analysis/preprocessing.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,9 @@ module Qualify = struct
587587
scope, List.rev reversed_generators
588588
in
589589
match value with
590-
| Expression.Await expression ->
591-
Expression.Await (qualify_expression ~qualify_strings ~scope expression)
590+
| Expression.Await { Await.operand; origin } ->
591+
Expression.Await
592+
{ Await.operand = qualify_expression ~qualify_strings ~scope operand; origin }
592593
| BinaryOperator { BinaryOperator.left; operator; right; origin } ->
593594
BinaryOperator
594595
{
@@ -3242,7 +3243,7 @@ module AccessCollector = struct
32423243
(* For attribute access, only count the base *)
32433244
from_expression collected base
32443245
(* The rest is boilerplates to make sure that expressions are visited recursively *)
3245-
| Await await -> from_expression collected await
3246+
| Await { Await.operand; origin = _ } -> from_expression collected operand
32463247
| BinaryOperator { BinaryOperator.left; right; _ }
32473248
| BooleanOperator { BooleanOperator.left; right; _ }
32483249
| ComparisonOperator { ComparisonOperator.left; right; _ } ->

source/analysis/scope.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module Binding = struct
8080
let sofar = of_expression sofar value in
8181
of_unannotated_target ~kind:Kind.WalrusTarget sofar target
8282
(* Boilerplates to make sure all expressions are visited. *)
83-
| Expression.Await expression
83+
| Expression.Await { Await.operand = expression; origin = _ }
8484
| Expression.Name (Name.Attribute { Name.Attribute.base = expression; _ })
8585
| Expression.UnaryOperator { UnaryOperator.operand = expression; _ }
8686
| Expression.Starred (Starred.Once expression | Starred.Twice expression)

source/analysis/typeCheck.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ module State (Context : Context) = struct
20882088
in
20892089
let resolved =
20902090
match value with
2091-
| Await expression -> (
2091+
| Await { Await.operand = expression; origin = _ } -> (
20922092
let { Resolved.resolution; resolved; errors; _ } =
20932093
forward_expression ~resolution expression
20942094
in

source/analysis/unawaitedAwaitableCheck.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,13 @@ module State (Context : Context) = struct
665665
Return the new state along with nested awaitable expressions. *)
666666
and forward_expression ~resolution ~(state : t) ~expression:{ Node.value; location } =
667667
match value with
668-
| Await ({ Node.value = Name name; _ } as expression) when is_simple_name name ->
668+
| Await { Await.operand = { Node.value = Name name; _ } as expression; origin = _ }
669+
when is_simple_name name ->
669670
let { state; nested_awaitable_expressions } =
670671
forward_expression ~resolution ~state ~expression
671672
in
672673
{ state = mark_name_as_awaited state ~name; nested_awaitable_expressions }
673-
| Await ({ Node.location; _ } as expression) ->
674+
| Await { Await.operand = { Node.location; _ } as expression; origin = _ } ->
674675
let { state; nested_awaitable_expressions } =
675676
forward_expression ~resolution ~state ~expression
676677
in

source/analysis/uninitializedLocalCheck.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module AccessCollector = struct
6363
| Name (Name.Identifier _) -> collected
6464
| _ -> from_expression collected base)
6565
(* The rest is boilerplates to make sure that expressions are visited recursively *)
66-
| Await await -> from_expression collected await
66+
| Await { Await.operand; _ } -> from_expression collected operand
6767
| BinaryOperator { BinaryOperator.left; right; _ }
6868
| BooleanOperator { BooleanOperator.left; right; _ }
6969
| ComparisonOperator { ComparisonOperator.left; right; _ } ->

source/ast/expression.ml

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,28 @@ end = struct
262262
| _ -> List.compare Argument.location_insensitive_compare left.arguments right.arguments
263263
end
264264

265+
and Await : sig
266+
type t = {
267+
operand: Expression.t;
268+
(* If this AST node was created from lowering down another AST node (for instance, `a + b` is
269+
turned into `a.__add__(b)`), `origin` stores the location of the original AST node and the
270+
reason for lowering it. *)
271+
origin: Origin.t option;
272+
}
273+
[@@deriving equal, compare, sexp, show, hash, to_yojson]
274+
275+
val location_insensitive_compare : t -> t -> int
276+
end = struct
277+
type t = {
278+
operand: Expression.t;
279+
origin: Origin.t option;
280+
}
281+
[@@deriving equal, compare, sexp, show, hash, to_yojson]
282+
283+
let location_insensitive_compare left right =
284+
Expression.location_insensitive_compare left.operand right.operand
285+
end
286+
265287
and ComparisonOperator : sig
266288
type operator =
267289
| Equals
@@ -1147,8 +1169,10 @@ and Origin : sig
11471169
| SubscriptGetItem (* `d[a]` is turned into `d.__getitem__(a)` *)
11481170
| ForIter (* `for e in l:` is turned into `l.__iter__().__next__()` *)
11491171
| ForNext (* `for e in l:` is turned into `l.__iter__().__next__()` *)
1172+
| ForAwait (* `for e in l:` might be turned into `await l.__iter__().__next__()` *)
11501173
| GeneratorIter (* `(e for e in l)` is turned into `l.__iter__().__next__()` *)
11511174
| GeneratorNext (* `(e for e in l)` is turned into `l.__iter__().__next__()` *)
1175+
| GeneratorAwait (* `(e for e in l)` might be turned into `await l.__iter__().__next__()` *)
11521176
| With (* `with e1 as e2` is turned into `e2 = e1.__enter__()` *)
11531177
| InContains (* `e in l` can be turned into `l.__contains__(e)` *)
11541178
| InIter (* `e in l` can be turned into `l.__iter__().__next__().__eq__(e)` *)
@@ -1253,8 +1277,10 @@ end = struct
12531277
| SubscriptGetItem
12541278
| ForIter
12551279
| ForNext
1280+
| ForAwait
12561281
| GeneratorIter
12571282
| GeneratorNext
1283+
| GeneratorAwait
12581284
| With
12591285
| InContains
12601286
| InIter
@@ -1368,7 +1394,7 @@ end
13681394

13691395
and Expression : sig
13701396
type expression =
1371-
| Await of t
1397+
| Await of Await.t
13721398
| BinaryOperator of BinaryOperator.t
13731399
| BooleanOperator of BooleanOperator.t
13741400
| Call of Call.t
@@ -1407,7 +1433,7 @@ and Expression : sig
14071433
val pp_type_param_list : Format.formatter -> TypeParam.t list -> unit
14081434
end = struct
14091435
type expression =
1410-
| Await of t
1436+
| Await of Await.t
14111437
| BinaryOperator of BinaryOperator.t
14121438
| BooleanOperator of BooleanOperator.t
14131439
| Call of Call.t
@@ -1439,7 +1465,7 @@ end = struct
14391465

14401466
let rec location_insensitive_compare_expression left right =
14411467
match left, right with
1442-
| Await left, Await right -> location_insensitive_compare left right
1468+
| Await left, Await right -> Await.location_insensitive_compare left right
14431469
| BinaryOperator left, BinaryOperator right ->
14441470
BinaryOperator.location_insensitive_compare left right
14451471
| BooleanOperator left, BooleanOperator right ->
@@ -1700,7 +1726,7 @@ end = struct
17001726

17011727
and pp_expression formatter expression =
17021728
match expression with
1703-
| Await expression -> Format.fprintf formatter "await %a" pp_expression_t expression
1729+
| Await { Await.operand; _ } -> Format.fprintf formatter "await %a" pp_expression_t operand
17041730
| BinaryOperator { BinaryOperator.left; operator; right; origin = _ } ->
17051731
Format.fprintf
17061732
formatter
@@ -1823,7 +1849,7 @@ end
18231849

18241850
module Mapper = struct
18251851
type 'a t = {
1826-
map_await: mapper:'a t -> location:Location.t -> Expression.t -> 'a;
1852+
map_await: mapper:'a t -> location:Location.t -> Await.t -> 'a;
18271853
map_binary_operator: mapper:'a t -> location:Location.t -> BinaryOperator.t -> 'a;
18281854
map_boolean_operator: mapper:'a t -> location:Location.t -> BooleanOperator.t -> 'a;
18291855
map_call: mapper:'a t -> location:Location.t -> Call.t -> 'a;
@@ -1884,7 +1910,7 @@ module Mapper = struct
18841910
{ Node.value; location }
18851911
=
18861912
match value with
1887-
| Expression.Await expression -> map_await ~mapper ~location expression
1913+
| Expression.Await await -> map_await ~mapper ~location await
18881914
| Expression.BinaryOperator binary_operator ->
18891915
map_binary_operator ~mapper ~location binary_operator
18901916
| Expression.BooleanOperator boolean_operator ->
@@ -2006,7 +2032,9 @@ module Mapper = struct
20062032
default_map_substrings_with_location ~mapper ~map_location:Fn.id substrings
20072033

20082034

2009-
let default_map_await ~mapper awaited = map ~mapper awaited
2035+
let default_map_await ~mapper { Await.operand; origin } =
2036+
{ Await.operand = map ~mapper operand; origin }
2037+
20102038

20112039
let default_map_await_node ~mapper ~location awaited =
20122040
Node.create ~location (Expression.Await (default_map_await ~mapper awaited))
@@ -2530,7 +2558,7 @@ end
25302558

25312559
module Folder = struct
25322560
type 'a t = {
2533-
fold_await: folder:'a t -> state:'a -> location:Location.t -> Expression.t -> 'a;
2561+
fold_await: folder:'a t -> state:'a -> location:Location.t -> Await.t -> 'a;
25342562
fold_binary_operator: folder:'a t -> state:'a -> location:Location.t -> BinaryOperator.t -> 'a;
25352563
fold_boolean_operator:
25362564
folder:'a t -> state:'a -> location:Location.t -> BooleanOperator.t -> 'a;
@@ -2600,7 +2628,7 @@ module Folder = struct
26002628
{ Node.value; location }
26012629
=
26022630
match value with
2603-
| Expression.Await expression -> fold_await ~folder ~state ~location expression
2631+
| Expression.Await await -> fold_await ~folder ~state ~location await
26042632
| Expression.BinaryOperator binary_operator ->
26052633
fold_binary_operator ~folder ~state ~location binary_operator
26062634
| Expression.BooleanOperator boolean_operator ->
@@ -2740,7 +2768,7 @@ module Folder = struct
27402768
substrings
27412769

27422770

2743-
let default_fold_await ~folder ~state awaited = fold ~folder ~state awaited
2771+
let default_fold_await ~folder ~state { Await.operand; origin = _ } = fold ~folder ~state operand
27442772

27452773
let default_fold_binary_operator
27462774
~folder
@@ -2997,6 +3025,7 @@ let origin { Node.value; _ } =
29973025
| Expression.Subscript { Subscript.origin; _ } -> origin
29983026
| Expression.WalrusOperator { WalrusOperator.origin; _ } -> origin
29993027
| Expression.Slice { Slice.origin; _ } -> origin
3028+
| Expression.Await { Await.origin; _ } -> origin
30003029
| _ -> None
30013030

30023031

@@ -3560,6 +3589,9 @@ let remove_origins expression =
35603589
origin = None;
35613590
}
35623591
in
3592+
let map_await ~mapper { Await.operand; origin = _ } =
3593+
{ Await.operand = Mapper.map ~mapper operand; origin = None }
3594+
in
35633595
Mapper.map
35643596
~mapper:
35653597
(Mapper.create_transformer
@@ -3572,5 +3604,6 @@ let remove_origins expression =
35723604
~map_subscript
35733605
~map_walrus_operator
35743606
~map_slice
3607+
~map_await
35753608
())
35763609
expression

source/ast/expression.mli

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ and Call : sig
103103
val location_insensitive_compare : t -> t -> int
104104
end
105105

106+
and Await : sig
107+
type t = {
108+
operand: Expression.t;
109+
origin: Origin.t option;
110+
}
111+
[@@deriving equal, compare, sexp, show, hash, to_yojson]
112+
113+
val location_insensitive_compare : t -> t -> int
114+
end
115+
106116
and ComparisonOperator : sig
107117
type operator =
108118
| Equals
@@ -408,8 +418,10 @@ and Origin : sig
408418
| SubscriptGetItem (* `d[a]` is turned into `d.__getitem__(a)` *)
409419
| ForIter (* `for e in l:` is turned into `l.__iter__().__next__()` *)
410420
| ForNext (* `for e in l:` is turned into `l.__iter__().__next__()` *)
421+
| ForAwait (* `for e in l:` might be turned into `await l.__iter__().__next__()` *)
411422
| GeneratorIter (* `(e for e in l)` is turned into `l.__iter__().__next__()` *)
412423
| GeneratorNext (* `(e for e in l)` is turned into `l.__iter__().__next__()` *)
424+
| GeneratorAwait (* `(e for e in l)` might be turned into `await l.__iter__().__next__()` *)
413425
| With (* `with e1 as e2` is turned into `e2 = e1.__enter__()` *)
414426
| InContains (* `e in l` can be turned into `l.__contains__(e)` *)
415427
| InIter (* `e in l` can be turned into `l.__iter__().__next__().__eq__(e)` *)
@@ -507,7 +519,7 @@ end
507519

508520
and Expression : sig
509521
type expression =
510-
| Await of t
522+
| Await of Await.t
511523
| BinaryOperator of BinaryOperator.t
512524
| BooleanOperator of BooleanOperator.t
513525
| Call of Call.t
@@ -548,7 +560,7 @@ module Mapper : sig
548560
val map_option : mapper:'a t -> Expression.t option -> 'a option
549561

550562
val create
551-
: map_await:(mapper:'a t -> location:Location.t -> Expression.t -> 'a) ->
563+
: map_await:(mapper:'a t -> location:Location.t -> Await.t -> 'a) ->
552564
map_binary_operator:(mapper:'a t -> location:Location.t -> BinaryOperator.t -> 'a) ->
553565
map_boolean_operator:(mapper:'a t -> location:Location.t -> BooleanOperator.t -> 'a) ->
554566
map_call:(mapper:'a t -> location:Location.t -> Call.t -> 'a) ->
@@ -579,7 +591,7 @@ module Mapper : sig
579591
'a t
580592

581593
val create_default
582-
: ?map_await:(mapper:Expression.t t -> location:Location.t -> Expression.t -> Expression.t) ->
594+
: ?map_await:(mapper:Expression.t t -> location:Location.t -> Await.t -> Expression.t) ->
583595
?map_binary_operator:
584596
(mapper:Expression.t t -> location:Location.t -> BinaryOperator.t -> Expression.t) ->
585597
?map_boolean_operator:
@@ -621,7 +633,7 @@ module Mapper : sig
621633
Expression.t t
622634

623635
val create_transformer
624-
: ?map_await:(mapper:Expression.t t -> Expression.t -> Expression.t) ->
636+
: ?map_await:(mapper:Expression.t t -> Await.t -> Await.t) ->
625637
?map_binary_operator:(mapper:Expression.t t -> BinaryOperator.t -> BinaryOperator.t) ->
626638
?map_boolean_operator:(mapper:Expression.t t -> BooleanOperator.t -> BooleanOperator.t) ->
627639
?map_call:(mapper:Expression.t t -> Call.t -> Call.t) ->
@@ -667,7 +679,7 @@ module Folder : sig
667679
val fold_option : folder:'a t -> state:'a -> Expression.t option -> 'a
668680

669681
val create
670-
: ?fold_await:(folder:'a t -> state:'a -> location:Location.t -> Expression.t -> 'a) ->
682+
: ?fold_await:(folder:'a t -> state:'a -> location:Location.t -> Await.t -> 'a) ->
671683
?fold_binary_operator:(folder:'a t -> state:'a -> location:Location.t -> BinaryOperator.t -> 'a) ->
672684
?fold_boolean_operator:
673685
(folder:'a t -> state:'a -> location:Location.t -> BooleanOperator.t -> 'a) ->
@@ -706,7 +718,7 @@ module Folder : sig
706718
'a t
707719

708720
val create_with_uniform_location_fold
709-
: ?fold_await:(folder:'a t -> state:'a -> Expression.t -> 'a) ->
721+
: ?fold_await:(folder:'a t -> state:'a -> Await.t -> 'a) ->
710722
?fold_binary_operator:(folder:'a t -> state:'a -> BinaryOperator.t -> 'a) ->
711723
?fold_boolean_operator:(folder:'a t -> state:'a -> BooleanOperator.t -> 'a) ->
712724
?fold_call:(folder:'a t -> state:'a -> Call.t -> 'a) ->

0 commit comments

Comments
 (0)