Skip to content

Commit

Permalink
[infer/py] BUILD_CONST_KEY_MAP also accepts int and bytes as literal …
Browse files Browse the repository at this point in the history
…keys

Summary:
It seems to accept any kind of constant keys (I witnessed a tuple of constant booleans ...)
So we'll improve the support as we go. We could also stop checking for "constant" keys and just take anything. The compiler will refuse them if they are not constant.

Reviewed By: davidpichardie

Differential Revision: D49912269

fbshipit-source-id: 2af76818816ff44f472f7981ecc41c350514b6d6
  • Loading branch information
Vincent Siles authored and facebook-github-bot committed Oct 11, 2023
1 parent 705cade commit c914ee0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
20 changes: 13 additions & 7 deletions infer/src/python/PyTrans.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1591,18 +1591,24 @@ end
module BUILD = struct
module CONST_KEY_MAP = struct
let is_tuple_ids cell =
let as_key = function FFI.Constant.PYCString s -> Some s | _ -> None in
let as_key c =
match (c : FFI.Constant.t) with
| PYCString s ->
Some (PyCommon.mk_string s)
| PYCInt i ->
Some (PyCommon.mk_int i)
| PYCBytes s ->
let s = Bytes.to_string s in
Some (PyCommon.mk_string s)
| _ ->
None
in
match cell with
| DataStack.Const c -> (
match c with
| FFI.Constant.PYCTuple keys ->
Array.fold_result keys ~init:[] ~f:(fun keys c ->
match as_key c with
| Some key ->
let key = PyCommon.mk_string key in
Ok (key :: keys)
| None ->
Error () )
match as_key c with Some key -> Ok (key :: keys) | None -> Error () )
| _ ->
Error () )
| _ ->
Expand Down
11 changes: 11 additions & 0 deletions infer/src/python/unit/PyTransTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3371,6 +3371,13 @@ print(s)
s = {"a": 42, "b": 1664}
print(s["1"])
# from cinder
d = { 0x78: "abc", # 1-n decoding mapping
b"abc": 0x0078,# 1-n encoding mapping
0x01: None, # decoding mapping to <undefined>
0x79: "", # decoding mapping to <remove character>
}
|}
in
test source ;
Expand All @@ -3390,6 +3397,8 @@ print(s["1"])
n4:*PyMap = load &dummy::s
n5 = $builtins.python_subscript_get(n4, $builtins.python_string("1"))
n6 = $builtins.print(n5)
n7 = $builtins.python_build_map($builtins.python_int(120), $builtins.python_string("abc"), $builtins.python_string("abc"), $builtins.python_int(120), $builtins.python_int(1), null, $builtins.python_int(121), $builtins.python_string(""))
store &dummy::d <- n7:*PyMap
ret null
}
Expand All @@ -3398,6 +3407,8 @@ print(s["1"])
global dummy::s: *PyObject
global dummy::d: *PyObject
global $python_implicit_names::__name__: *PyString
global $python_implicit_names::__file__: *PyString
Expand Down

0 comments on commit c914ee0

Please sign in to comment.