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

Function dependencies not respected with --disable-main #1727

Closed

Description

@certik

import_order_01.py:

from import_order_01b import f

@ccallback
def main1():
    a: i32 = f()
    print(a)

import_order_01b.py:

from lpython import i32

def f() -> i32:
    return 42

Compile using:

$ lpython -I. --show-c --disable-main import_order_01.py
...

// Implementations
void main1()
{
    int32_t a;
    a = f();
    printf("%d\n", a);
}

int32_t f()
{
    int32_t _lpython_return_variable;
    _lpython_return_variable = 42;
    return _lpython_return_variable;
}

This then fails to compile due to:

$ clang x.c                
x.c:41:9: error: implicit declaration of function 'f' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    a = f();
        ^
1 error generated.

The issue is that main1 depends on f, but f gets generated below the function. It only happens when --disable-main is used, but we need to use it.

Activity

  1. certik commented on Apr 21, 2023

    @certik
    ContributorAuthor

    I am not sure how to best test this in integration tests, due to the --disable-main. The @ccallback is needed, otherwise the function gets treated as unused.

  2. self-assigned this
    on Apr 21, 2023
  3. czgdp1807 commented on Apr 21, 2023

    @czgdp1807
    Collaborator

    The --disable-main doesn't matter. Even without it, f() goes below main1,

    ...
    
    // Implementations
    void main1()
    {
        int32_t a;
        a = f();
        printf("%d\n", a);
    }
    
    int32_t f()
    {
        int32_t _lpython_return_variable;
        _lpython_return_variable = 42;
        return _lpython_return_variable;
    }
    
    int main(int argc, char* argv[])
    {
        return 0;
    }
    
  4. czgdp1807 commented on Apr 21, 2023

    @czgdp1807
    Collaborator

    Its a problem with pass_wrap_global_stmts_into_program. It only shifts user defined functions into _global_symbols (generated module) only when they are called. In your case main1 is never called so it stays in the global scope. Now C backend first visits all the functions in global scope and since main1 lies in global scope so C code of it is generated first. Then f is generated when the modules are visited. If main1 is shifted to _global_symbols then it depends on import_order_01 and hence f will always be generated before main1 (which is correct).

    End moral - So, IMO global scope should only contain intrinsic functions generated by libasr in intrinsic_function_registry.h. All user defined functions should be shifted to _global_symbols modules irrespective of the fact whether they are called by the user or not.

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

Metadata

Metadata

Assignees

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