From dfeacbc07fc6cfc837d95b848dcb6370a5876190 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 22:54:07 +0530 Subject: [PATCH] Add support for item access from `Const` data-structures (#2579) * Add support for accessing items from data-structures within `Const` * Update type checking logic for `Const dict` Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> * Minor formatting change Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> * Handle negative indices for `const list` and minor formatting changes * Add tests * Update test references * Heavily simplify handling `const` * Tests: Add compile time test * Remove calls to `type_get_past_const()` * Tests: Update test references * Remove extra newline Co-authored-by: Shaikh Ubaid * Delete tests/reference/asr-test_const_access-82a9a24.json * Delete tests/reference/asr-test_const_access-82a9a24.stdout * Delete tests/reference/asr-test_const_str_access-59ff543.stderr * Delete tests/reference/asr-test_const_tuple_access-0d4c6df.json * Delete tests/reference/asr-test_const_tuple_access-0d4c6df.stderr * Delete tests/reference/asr-test_const_str_access-59ff543.json * Formatting changes * Tests: Add test to CMakeLists and update error references * Update asr_to_llvm.cpp * Undo formatting changes * Undo formatting changes * Revert throwing error for `Const` annotated tuples and strings * Tests: Remove error references * Remove redundant visitor * Undo moving `index` --------- Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> Co-authored-by: Shaikh Ubaid --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_const_access.py | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 integration_tests/test_const_access.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index f2a6bbc995..d19918b843 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -556,6 +556,7 @@ RUN(NAME test_list_compare LABELS cpython llvm llvm_jit) RUN(NAME test_list_concat LABELS cpython llvm llvm_jit c NOFAST) RUN(NAME test_list_reserve LABELS cpython llvm llvm_jit) RUN(NAME test_const_list LABELS cpython llvm llvm_jit) +RUN(NAME test_const_access LABELS cpython llvm llvm_jit) RUN(NAME test_tuple_01 LABELS cpython llvm llvm_jit c) RUN(NAME test_tuple_02 LABELS cpython llvm llvm_jit c NOFAST) RUN(NAME test_tuple_03 LABELS cpython llvm llvm_jit c) diff --git a/integration_tests/test_const_access.py b/integration_tests/test_const_access.py new file mode 100644 index 0000000000..4368e3ed0c --- /dev/null +++ b/integration_tests/test_const_access.py @@ -0,0 +1,9 @@ +from lpython import i32, Const + +CONST_LIST: Const[list[i32]] = [1, 2, 3, 4, 5] +CONST_DICTIONARY: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3} + +assert CONST_LIST[0] == 1 +assert CONST_LIST[-2] == 4 + +assert CONST_DICTIONARY["a"] == 1 \ No newline at end of file