Skip to content

Returning empty list not supported #1841

@ubaidsk

Description

@ubaidsk
$ cat examples/expr2.py 
from lpython import i32

def return_empty_list_of_integers() -> list[i32]:
    return []

def main0():
    x: list[i32] = return_empty_list_of_integers()
    print(len(x))

    assert len(x) == 0

main0()
$ python examples/expr2.py
0
$ lpython examples/expr2.py
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/bin/lpython.cpp", line 1822
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/bin/lpython.cpp", line 783
    r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options,
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7274
    #endif
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7227
    {
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4405
    visit_stmt(*x.m_body[i]);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_ast.h", line 1883
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_ast.h", line 1751
    case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4514
    handle_fn(x, *f);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4494
    transform_stmts(body, x.n_body, x.m_body);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4365
    this->visit_stmt(*m_body[i]);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_ast.h", line 1883
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_ast.h", line 1753
    case stmtType::ClassDef: { v.visit_ClassDef((const ClassDef_t &)x); return; }
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 6033
    ASR::expr_t *target = ASRUtils::EXPR(return_var_ref);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/asr_utils.h", line 34
    return ASR::down_cast<ASR::expr_t>(f);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/asr.h", line 40
    LCOMPILERS_ASSERT(f != nullptr);
AssertFailed: f != nullptr

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions