← 输入框|github.com|刷新
Skip to content

Internal function def causes SEGSIGV #2006

Open

Description

@rebcabin

Should perhaps produce a NotImplemented error message:

(lp) ┌─(~/CLionProjects/lpython/lasr/LP-pycharm)─────────────────────────────────────────────────────────────────────────────────────────────────────────(brian@Golf37:s012)─┐
└─(15:35:42 on brian-lasr ✹ ✭)──> cat > expr45.py                                                                                                         ──(Thu,Jun22)─┘
def foo():
    def bar():
        pass
    pass
(lp) ┌─(~/CLionProjects/lpython/lasr/LP-pycharm)─────────────────────────────────────────────────────────────────────────────────────────────────────────(brian@Golf37:s012)─┐
└─(15:36:41 on brian-lasr ✹ ✭)──> PYTHONPATH='../../src/runtime/lpython' python expr45.py                                                                 ──(Thu,Jun22)─┘
(lp) ┌─(~/CLionProjects/lpython/lasr/LP-pycharm)─────────────────────────────────────────────────────────────────────────────────────────────────────────(brian@Golf37:s012)─┐
└─(15:36:50 on brian-lasr ✹ ✭)──> ../../src/bin/lpython -I. ./expr45.py                                                                                   ──(Thu,Jun22)─┘
Traceback (most recent call last):
  File "/Users/brian/CLionProjects/lpython/src/bin/lpython.cpp", line 1844
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/Users/brian/CLionProjects/lpython/src/bin/lpython.cpp", line 783
    r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options,
  File "/Users/brian/CLionProjects/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7532
    auto res2 = body_visitor(al, lm, *ast_m, diagnostics, unit, main_module,
  File "/Users/brian/CLionProjects/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7485
    b.visit_Module(ast);
  File "/Users/brian/CLionProjects/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4620
    visit_stmt(*x.m_body[i]);
  File "/Users/brian/CLionProjects/lpython/src/lpython/python_ast.h", line 1883
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/brian/CLionProjects/lpython/src/lpython/python_ast.h", line 1751
    case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
  File "/Users/brian/CLionProjects/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4727
    handle_fn(x, *f);
  File "/Users/brian/CLionProjects/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4709
    transform_stmts(body, x.n_body, x.m_body);
  File "/Users/brian/CLionProjects/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4580
    this->visit_stmt(*m_body[i]);
  File "/Users/brian/CLionProjects/lpython/src/lpython/python_ast.h", line 1883
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/brian/CLionProjects/lpython/src/lpython/python_ast.h", line 1751
    case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
  File "/Users/brian/CLionProjects/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4723
    ASR::symbol_t *t = current_scope->get_symbol(x.m_name);
  Binary file "/usr/lib/system/libsystem_platform.dylib", local address: 0x18042ea23
Segfault: Signal SIGSEGV (segmentation fault) received

Activity

  1. added
    bug
    on Jun 22, 2023
  2. certik commented on Jun 22, 2023

    @certik
    Contributor

    We support this in LFortran, so it shouldn't be hard to get working, at least basic cases.

  3. certik commented on Jul 7, 2023

    @certik
    Contributor

    @arteevraina here is how to visit the nested functions:

    --- a/src/lpython/semantics/python_ast_to_asr.cpp
    +++ b/src/lpython/semantics/python_ast_to_asr.cpp
    @@ -4280,6 +4280,9 @@ public:
                 }
             } else {
                 bool is_pure = false, is_module = false;
    +            for (size_t i=0; i<x.n_body; i++) {
    +                visit_stmt(*x.m_body[i]);
    +            }
                 tmp = ASRUtils::make_Function_t_util(
                     al, x.base.base.loc,
                     /* a_symtab */ current_scope,

    The following now works:

    def main0():
        x: i32
        x = (2+3)*5
        print(x)
    
        def bar():
            print("bar")
    
            def foo():
                print("foo")
    
    
        bar()
        bar()
    
    
    main0()
    
    # Not implemented yet in LPython:
    #if __name__ == "__main__":
    #    main()

    Relevant part of the ASR:

                                main0:
                                    (Function
                                        (SymbolTable
                                            2
                                            {
                                                bar:
                                                    (Function
                                                        (SymbolTable
                                                            3
                                                            {
                                                                foo:
                                                                    (Function
                                                                        (SymbolTable
                                                                            4
                                                                            {
                                                                                
                                                                            })
                                                                        foo
                                                                        (FunctionType
                                                                            []

    This also works:

    def main0():
        x: i32
        x = (2+3)*5
        print(x)
    
        def bar():
            print(x)
    
    
        bar()
        bar()
    
    
    main0()
    
    # Not implemented yet in LPython:
    #if __name__ == "__main__":
    #    main()

    But some other combinations don't work yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions