From c914ee07a7b84559d6a6f3553671c676576c2126 Mon Sep 17 00:00:00 2001 From: Vincent Siles Date: Wed, 11 Oct 2023 07:53:09 -0700 Subject: [PATCH] [infer/py] BUILD_CONST_KEY_MAP also accepts int and bytes as literal 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 --- infer/src/python/PyTrans.ml | 20 +++++++++++++------- infer/src/python/unit/PyTransTest.ml | 11 +++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/infer/src/python/PyTrans.ml b/infer/src/python/PyTrans.ml index c09bb619402..2faa8c053de 100644 --- a/infer/src/python/PyTrans.ml +++ b/infer/src/python/PyTrans.ml @@ -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 () ) | _ -> diff --git a/infer/src/python/unit/PyTransTest.ml b/infer/src/python/unit/PyTransTest.ml index 9db0ac0fd17..aed40d1560a 100644 --- a/infer/src/python/unit/PyTransTest.ml +++ b/infer/src/python/unit/PyTransTest.ml @@ -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 + 0x79: "", # decoding mapping to + } |} in test source ; @@ -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 } @@ -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