From 5230ad6e5a058930fe322a44a5a600591d4c5943 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 20 May 2023 19:52:01 +0530 Subject: [PATCH 1/2] ASR: Support returning empty list --- src/lpython/semantics/python_ast_to_asr.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b0a0424757..6cbfa2f6d6 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -6029,15 +6029,18 @@ class BodyVisitor : public CommonVisitor { tmp = ASR::make_Return_t(al, x.base.base.loc); return; } - this->visit_expr(*x.m_value); - ASR::expr_t *value = ASRUtils::EXPR(tmp); ASR::asr_t *return_var_ref = ASR::make_Var_t(al, x.base.base.loc, return_var); ASR::expr_t *target = ASRUtils::EXPR(return_var_ref); ASR::ttype_t *target_type = ASRUtils::expr_type(target); - ASR::ttype_t *value_type = ASRUtils::expr_type(value); if( ASR::is_a(*target_type) ) { target_type = ASRUtils::get_contained_type(target_type); } + ASR::ttype_t* ann_assign_target_type_copy = ann_assign_target_type; + ann_assign_target_type = target_type; + this->visit_expr(*x.m_value); + ann_assign_target_type = ann_assign_target_type_copy; + ASR::expr_t *value = ASRUtils::EXPR(tmp); + ASR::ttype_t *value_type = ASRUtils::expr_type(value); if( ASR::is_a(*value_type) ) { value_type = ASRUtils::get_contained_type(value_type); } From 926fe7bb660231a469414fe7767a097231e9f480 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 20 May 2023 19:52:26 +0530 Subject: [PATCH 2/2] TEST: Add test for empty list --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_list_11.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 integration_tests/test_list_11.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 8bcddab123..590da72d46 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -372,6 +372,7 @@ RUN(NAME test_list_07 LABELS cpython llvm c) RUN(NAME test_list_08 LABELS cpython llvm c) RUN(NAME test_list_09 LABELS cpython llvm c) RUN(NAME test_list_10 LABELS cpython llvm c) +RUN(NAME test_list_11 LABELS cpython llvm c) RUN(NAME test_list_section LABELS cpython llvm c) RUN(NAME test_list_count LABELS cpython llvm) RUN(NAME test_list_index LABELS cpython llvm) diff --git a/integration_tests/test_list_11.py b/integration_tests/test_list_11.py new file mode 100644 index 0000000000..f9d2b3fc48 --- /dev/null +++ b/integration_tests/test_list_11.py @@ -0,0 +1,12 @@ +from lpython import i32 + +def return_empty_list_of_tuples() -> list[i32]: + return [] + +def main0(): + x: list[i32] = return_empty_list_of_tuples() + print(len(x)) + + assert len(x) == 0 + +main0()