Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASR: Fix expression as statement #2216

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions integration_tests/test_list_pop3.py
Original file line number Diff line number Diff line change
@@ -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()
39 changes: 25 additions & 14 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,24 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
}


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<ASR::symbol_t>(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,
Expand Down Expand Up @@ -1290,20 +1308,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
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<ASR::symbol_t>(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;
}
Expand Down Expand Up @@ -4724,11 +4729,17 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// Visit the statement
this->visit_stmt(*m_body[i]);
if (tmp != nullptr) {
if (ASR::is_a<ASR::expr_t>(*tmp)) {
Shaikh-Ubaid marked this conversation as resolved.
Show resolved Hide resolved
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<ASR::expr_t>(*t)) {
t = make_dummy_assignment(ASRUtils::EXPR(t));
}
ASR::stmt_t* tmp_stmt = ASRUtils::STMT(t);
body.push_back(al, tmp_stmt);
}
Expand Down