Skip to content

Commit

Permalink
[inferpython][3.10] adding new opcode LOAD_ASSERTION_ERROR
Browse files Browse the repository at this point in the history
Summary: very basic instruction

Reviewed By: skcho

Differential Revision:
D65031487

Privacy Context Container: L1208441

fbshipit-source-id: ae2292e81525febf29af3028458e0fc91df7c98e
  • Loading branch information
davidpichardie authored and facebook-github-bot committed Oct 29, 2024
1 parent daf8c8f commit d55826d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions infer/src/python/PyIR.ml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ module Exp = struct
In this IR, name resolution is done so naming is not ambiguous. Also, we have reconstructed
the CFG of the program, lost during Python compilation *)
type t =
| AssertionError
| BuildFrozenSet of t list
| BuildSlice of t list (* 2 < length <= 3 *)
| BuildString of t list
Expand Down Expand Up @@ -472,6 +473,8 @@ module Exp = struct
let of_int i = Const (Int (Z.of_int i))

let rec pp fmt = function
| AssertionError ->
F.fprintf fmt "$AssertionError"
| Const c ->
Const.pp fmt c
| Var scope_ident ->
Expand Down Expand Up @@ -1545,6 +1548,8 @@ let parse_bytecode st ({FFI.Code.co_consts; co_names; co_varnames} as code)
let st = if Option.is_some starts_line then {st with State.loc= starts_line} else st in
State.debug st "%a@\n" (FFI.Instruction.pp ~code) instr ;
match opname with
| "LOAD_ASSERTION_ERROR" ->
Ok (State.push st Exp.AssertionError, None)
| "LOAD_CONST" ->
let* exp = convert_ffi_const st co_consts.(arg) in
let st = State.push_symbol st exp in
Expand Down Expand Up @@ -2114,6 +2119,7 @@ let parse_bytecode st ({FFI.Code.co_consts; co_names; co_varnames} as code)

let get_successors_offset {FFI.Instruction.opname; arg} =
match opname with
| "LOAD_ASSERTION_ERROR"
| "LOAD_CONST"
| "LOAD_NAME"
| "LOAD_GLOBAL"
Expand Down
1 change: 1 addition & 0 deletions infer/src/python/PyIR.mli
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ module Exp : sig
type collection = List | Set | Tuple | Map

type t =
| AssertionError
| BuildFrozenSet of t list
| BuildSlice of t list
| BuildString of t list
Expand Down
2 changes: 2 additions & 0 deletions infer/src/python/PyIR2Textual.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ let exp_globals = Textual.Exp.(Load {exp= Lvar Parameter.globals; typ= None})

let rec of_exp exp : Textual.Exp.t =
match (exp : Exp.t) with
| AssertionError ->
call_builtin "py_load_assertion_error" []
| Const const ->
of_const const
| Var {scope= Global; ident} ->
Expand Down
1 change: 1 addition & 0 deletions infer/src/python/PyIRExec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ let run_files modules =
(* Note: Python dictionnaries may have other type of keys *)
let index = eval_exp index |> expect_string ~who ~how:"as index" |> Ident.mk in
get index
| AssertionError
| BuildSlice _
| BuildString _
| BuildFrozenSet _
Expand Down
30 changes: 30 additions & 0 deletions infer/src/python/unit/PyIRTestBasic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,33 @@ def foo(n):
n2 <- $Binary.Multiply("*", n1, None)
n3 <- $CallMethod[update](n0, $BuildMap("key", n2), None)
return None |}]
let%expect_test _ =
let source = {|
def foo(n):
assert(n>0)
|} in
PyIR.test source ;
[%expect
{|
module dummy:
function toplevel():
b0:
n0 <- $MakeFunction["foo", "dummy.foo", None, None, None, None]
TOPLEVEL[foo] <- n0
return None
function dummy.foo(n):
b0:
n0 <- LOCAL[n]
n1 <- $Compare.gt(n0, 0, None)
if n1 then jmp b2 else jmp b1
b1:
throw $AssertionError
b2:
return None |}]

0 comments on commit d55826d

Please sign in to comment.