Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Declaring a u16 function breaks the % operation #2146

Closed
@certik

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions