Skip to content

Commit

Permalink
binary operator overrides
Browse files Browse the repository at this point in the history
Summary:
this diff updates some of the logic from previous diffs to account for operator overrides like we do for comparison operators

there is a unit test for comparison op overrides here:
https://www.internalfb.com/code/fbsource/[4898bc565af6456c52fb093d68004b70c3b1d49d]/fbcode/tools/pyre/source/ast/test/expressionTest.ml?lines=391

which I'll probably add for the binops in a later diff but it won't work now because we don't generate this AST node during parsing.

Reviewed By: arthaud

Differential Revision: D57627670

fbshipit-source-id: 804d3135e336f6a2a55ed337921609fbe0c9eb4d
  • Loading branch information
yangdanny97 authored and facebook-github-bot committed May 24, 2024
1 parent a6a437a commit 3899d4b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
8 changes: 5 additions & 3 deletions source/analysis/typeCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1896,9 +1896,11 @@ module State (Context : Context) = struct
in
{ resolution; resolved = Type.Any; errors; resolved_annotation = None; base = None }
))
(* TODO: T101299882 *)
| BinaryOperator _ ->
{ resolution; resolved = Type.Any; errors = []; resolved_annotation = None; base = None }
| BinaryOperator operator ->
let resolved =
forward_expression ~resolution (BinaryOperator.override ~location operator)
in
{ resolved with errors = resolved.errors }
| BooleanOperator { BooleanOperator.left; operator; right } -> (
let {
Resolved.resolution = resolution_left;
Expand Down
34 changes: 34 additions & 0 deletions source/ast/expression.ml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ and BinaryOperator : sig
val pp_binary_operator : Format.formatter -> operator -> unit

val location_insensitive_compare : t -> t -> int

val override : location:Location.t -> t -> Expression.t
end = struct
type operator =
| Add
Expand Down Expand Up @@ -448,6 +450,38 @@ end = struct
match [%compare: operator] left.operator right.operator with
| x when not (Int.equal x 0) -> x
| _ -> Expression.location_insensitive_compare left.right right.right)


let override ~location { left; operator; right } =
let operator =
match operator with
| Add -> "__add__"
| Sub -> "__sub__"
| Mult -> "__mul__"
| MatMult -> "__matmul__"
| Div -> "__truediv__"
| Mod -> "__mod__"
| Pow -> "__pow__"
| LShift -> "__lshift__"
| RShift -> "__rshift__"
| BitOr -> "__or__"
| BitXor -> "__xor__"
| BitAnd -> "__and__"
| FloorDiv -> "__floordiv__"
in
let arguments = [{ Call.Argument.name = None; value = right }] in
Expression.Call
{
Call.callee =
{
Node.location;
value =
Expression.Name
(Name.Attribute { Name.Attribute.base = left; attribute = operator; special = true });
};
arguments;
}
|> Node.create ~location
end

and Comprehension : sig
Expand Down
2 changes: 2 additions & 0 deletions source/ast/expression.mli
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ and BinaryOperator : sig
val pp_binary_operator : Format.formatter -> operator -> unit

val location_insensitive_compare : t -> t -> int

val override : location:Location.t -> t -> Expression.t
end

and Comprehension : sig
Expand Down
1 change: 1 addition & 0 deletions source/interprocedural/callGraph.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,7 @@ struct
>>| ExpressionCallees.from_identifier
>>| register_targets ~expression_identifier:identifier
|> ignore
| Expression.BinaryOperator _ -> failwith "T101299882"
| Expression.ComparisonOperator comparison -> (
match ComparisonOperator.override ~location comparison with
| Some { Node.value = Expression.Call call; _ } ->
Expand Down

0 comments on commit 3899d4b

Please sign in to comment.