From 3a5598eba14ad2dd60e526394e58f5f5260c4115 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 29 Jul 2023 16:08:56 +0530 Subject: [PATCH 1/3] Refactor: Define and use make_dummy_assignment() --- src/lpython/semantics/python_ast_to_asr.cpp | 33 ++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index f96e9213cb..49cf381f64 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1124,6 +1124,24 @@ class CommonVisitor : public AST::BaseVisitor { } + ASR::asr_t* make_dummy_assignment(ASR::expr_t* expr) { + ASR::ttype_t* type = ASRUtils::expr_type(expr); + std::string dummy_ret_name = current_scope->get_unique_name("__lcompilers_dummy", false); + SetChar variable_dependencies_vec; + variable_dependencies_vec.reserve(al, 1); + ASRUtils::collect_variable_dependencies(al, variable_dependencies_vec, type); + ASR::asr_t* variable_asr = ASR::make_Variable_t(al, expr->base.loc, current_scope, + s2c(al, dummy_ret_name), variable_dependencies_vec.p, + variable_dependencies_vec.size(), ASR::intentType::Local, + nullptr, nullptr, ASR::storage_typeType::Default, + type, nullptr, ASR::abiType::Source, ASR::accessType::Public, + ASR::presenceType::Required, false); + ASR::symbol_t* variable_sym = ASR::down_cast(variable_asr); + current_scope->add_symbol(dummy_ret_name, variable_sym); + ASR::expr_t* variable_var = ASRUtils::EXPR(ASR::make_Var_t(al, expr->base.loc, variable_sym)); + return ASR::make_Assignment_t(al, expr->base.loc, variable_var, expr, nullptr); + } + // Function to create appropriate call based on symbol type. If it is external // generic symbol then it changes the name accordingly. ASR::asr_t* make_call_helper(Allocator &al, ASR::symbol_t* s, SymbolTable *current_scope, @@ -1290,20 +1308,7 @@ class CommonVisitor : public AST::BaseVisitor { s_generic, args_new.p, args_new.size(), a_type, value, nullptr); if( ignore_return_value ) { - std::string dummy_ret_name = current_scope->get_unique_name("__lcompilers_dummy", false); - SetChar variable_dependencies_vec; - variable_dependencies_vec.reserve(al, 1); - ASRUtils::collect_variable_dependencies(al, variable_dependencies_vec, a_type); - ASR::asr_t* variable_asr = ASR::make_Variable_t(al, loc, current_scope, - s2c(al, dummy_ret_name), variable_dependencies_vec.p, - variable_dependencies_vec.size(), ASR::intentType::Local, - nullptr, nullptr, ASR::storage_typeType::Default, - a_type, nullptr, ASR::abiType::Source, ASR::accessType::Public, - ASR::presenceType::Required, false); - ASR::symbol_t* variable_sym = ASR::down_cast(variable_asr); - current_scope->add_symbol(dummy_ret_name, variable_sym); - ASR::expr_t* variable_var = ASRUtils::EXPR(ASR::make_Var_t(al, loc, variable_sym)); - return ASR::make_Assignment_t(al, loc, variable_var, ASRUtils::EXPR(func_call_asr), nullptr); + return make_dummy_assignment(ASRUtils::EXPR(func_call_asr)); } else { return func_call_asr; } From e5de5948a2e7d735275fdbf571b6fc2c0f1eae02 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 29 Jul 2023 16:09:20 +0530 Subject: [PATCH 2/3] ASR: Make a dummy assign for exprs --- src/lpython/semantics/python_ast_to_asr.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 49cf381f64..aa0db8e46a 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -4729,11 +4729,17 @@ class BodyVisitor : public CommonVisitor { // Visit the statement this->visit_stmt(*m_body[i]); if (tmp != nullptr) { + if (ASR::is_a(*tmp)) { + tmp = make_dummy_assignment(ASRUtils::EXPR(tmp)); + } ASR::stmt_t* tmp_stmt = ASRUtils::STMT(tmp); body.push_back(al, tmp_stmt); } else if (!tmp_vec.empty()) { for (auto t: tmp_vec) { if (t != nullptr) { + if (ASR::is_a(*t)) { + t = make_dummy_assignment(ASRUtils::EXPR(t)); + } ASR::stmt_t* tmp_stmt = ASRUtils::STMT(t); body.push_back(al, tmp_stmt); } From 3e183fd08e32299f47dc3aba2b17072afe7b735f Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sat, 29 Jul 2023 16:12:19 +0530 Subject: [PATCH 3/3] TEST: For expr as stmt --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_list_pop3.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 integration_tests/test_list_pop3.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 64aace4c21..e3fdabcdba 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -507,6 +507,7 @@ RUN(NAME test_list_repeat LABELS cpython llvm NOFAST) RUN(NAME test_list_reverse LABELS cpython llvm) RUN(NAME test_list_pop LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here. RUN(NAME test_list_pop2 LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here. +RUN(NAME test_list_pop3 LABELS cpython llvm) RUN(NAME test_list_compare LABELS cpython llvm) RUN(NAME test_tuple_01 LABELS cpython llvm c) RUN(NAME test_tuple_02 LABELS cpython llvm c NOFAST) diff --git a/integration_tests/test_list_pop3.py b/integration_tests/test_list_pop3.py new file mode 100644 index 0000000000..13f0740a67 --- /dev/null +++ b/integration_tests/test_list_pop3.py @@ -0,0 +1,14 @@ +from lpython import i32 + +def main0(): + a: list[i32] = [3, 4, 5] + i: i32 + for i in range(10): + a.append(1) + a.pop() + + print(a) + assert a[-1] == 5 + assert len(a) == 3 + +main0()