Skip to content

Commit d1990b4

Browse files
authored
Fix returning empty list (#1842)
1 parent 0577159 commit d1990b4

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ RUN(NAME test_list_07 LABELS cpython llvm c)
372372
RUN(NAME test_list_08 LABELS cpython llvm c)
373373
RUN(NAME test_list_09 LABELS cpython llvm c)
374374
RUN(NAME test_list_10 LABELS cpython llvm c)
375+
RUN(NAME test_list_11 LABELS cpython llvm c)
375376
RUN(NAME test_list_section LABELS cpython llvm c)
376377
RUN(NAME test_list_count LABELS cpython llvm)
377378
RUN(NAME test_list_index LABELS cpython llvm)

integration_tests/test_list_11.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from lpython import i32
2+
3+
def return_empty_list_of_tuples() -> list[i32]:
4+
return []
5+
6+
def main0():
7+
x: list[i32] = return_empty_list_of_tuples()
8+
print(len(x))
9+
10+
assert len(x) == 0
11+
12+
main0()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6029,15 +6029,18 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
60296029
tmp = ASR::make_Return_t(al, x.base.base.loc);
60306030
return;
60316031
}
6032-
this->visit_expr(*x.m_value);
6033-
ASR::expr_t *value = ASRUtils::EXPR(tmp);
60346032
ASR::asr_t *return_var_ref = ASR::make_Var_t(al, x.base.base.loc, return_var);
60356033
ASR::expr_t *target = ASRUtils::EXPR(return_var_ref);
60366034
ASR::ttype_t *target_type = ASRUtils::expr_type(target);
6037-
ASR::ttype_t *value_type = ASRUtils::expr_type(value);
60386035
if( ASR::is_a<ASR::Const_t>(*target_type) ) {
60396036
target_type = ASRUtils::get_contained_type(target_type);
60406037
}
6038+
ASR::ttype_t* ann_assign_target_type_copy = ann_assign_target_type;
6039+
ann_assign_target_type = target_type;
6040+
this->visit_expr(*x.m_value);
6041+
ann_assign_target_type = ann_assign_target_type_copy;
6042+
ASR::expr_t *value = ASRUtils::EXPR(tmp);
6043+
ASR::ttype_t *value_type = ASRUtils::expr_type(value);
60416044
if( ASR::is_a<ASR::Const_t>(*value_type) ) {
60426045
value_type = ASRUtils::get_contained_type(value_type);
60436046
}

0 commit comments

Comments
 (0)