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

Declaring a u16 function breaks the % operation #2146

Closed

Description

@certik

Minimal example:

from lpython import i16, i32

def f():
    i: i32 = 5
    print(i16(i % 1023))

def u16(x: i16) -> i32:
    if x >= i16(0):
        return i32(x)
    else:
        return i32(x) + 65536

This gives:

$ lpython a.py 
warning: The module 'lpython_builtin' located in /Users/ondrej/repos/lpython/src/bin/../runtime/lpython_builtin.py cannot be loaded
 --> a.py:5:15
  |
5 |     print(i16(i % 1023))
  |               ^^^^^^^^ imported here

semantic error: Unsupported type annotation: u16
   --> /Users/ondrej/repos/lpython/src/bin/../runtime/lpython_builtin.py:504:26
    |
504 | def _lpython_floordiv(a: u16, b: u16) -> u16:
    |                          ^^^ 

The issue is likely here:

    ASR::ttype_t* get_type_from_var_annotation(std::string var_annotation,
        const Location& loc, Vec<ASR::dimension_t>& dims,
        AST::expr_t** m_args=nullptr, size_t n_args=0,
        bool raise_error=true, ASR::abiType abi=ASR::abiType::Source,
        bool is_argument=false) {
        ASR::ttype_t* type = nullptr;
        ASR::symbol_t *s = current_scope->resolve_symbol(var_annotation);
        if (s) {
            if (ASR::is_a<ASR::Variable_t>(*s)) {
...

Where it finds s as a "user" function. However this function was declared below and in the main module, not the built-in module, so unless the module is inlined, this shouldn't happen.

Finally, the error message should point to the user defined function and say that it can't be used for annotation.

Activity

  1. added
    bug
    on Jul 11, 2023
  2. added a commit that references this issue on Jul 15, 2023
  3. ubaidsk commented on Jul 15, 2023

    @ubaidsk
    Collaborator

    Since #2049 we were using the same Translation_Unit_t/global_scope across modules. The symbols defined in the main_module get added to the Translation_Unit_t/global_scope. Due to this, other modules are able to access the symbols defined in the main_module (as the global_scope is shared with them). I think, therefore, the error described in this issue occurs.

    I fixed this issue in #2161 by processing/forming the main_module as a Module_t (and embedding it inside the Translation_Unit_t. This way the symbols defined in the main_module are not added to the global scope (but instead to the module scope) and thus are not accessible by other modules.

  4. added a commit that references this issue on Jul 16, 2023
  5. certik commented on Jul 16, 2023

    @certik
    ContributorAuthor

    @Shaikh-Ubaid awesome, great job!

    I think this is a cleaner solution.

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