Skip to content

x: i32[3] = [1, 2, 3] fails #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
certik opened this issue Apr 5, 2022 · 11 comments
Closed

x: i32[3] = [1, 2, 3] fails #324

certik opened this issue Apr 5, 2022 · 11 comments

Comments

@certik
Copy link
Contributor

certik commented Apr 5, 2022

def main0():
    x: i32[3] = [1, 2, 3]
    print(x)

main0()

This prints:

$ lpython examples/expr2.py 
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ondrej/repos/lpython/src/bin/lpython.cpp", line 799
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir, compiler_options);
  File "/Users/ondrej/repos/lpython/src/bin/lpython.cpp", line 306
    res = fe.get_llvm3(*asr, diagnostics);
  File "/Users/ondrej/repos/lpython/src/lpython/python_evaluator.cpp", line 54
    = asr_to_llvm(asr, diagnostics,
  File "/Users/ondrej/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 4281
    pass_replace_print_arr(al, asr, rl_path);
  File "/Users/ondrej/repos/lpython/src/libasr/pass/print_arr.cpp", line 85
    v.visit_TranslationUnit(unit);
  File "../libasr/asr.h", line 2502
  File "../libasr/asr.h", line 2398
  File "../libasr/asr.h", line 2240
  File "../libasr/pass/pass_utils.h", line 135
  File "../libasr/pass/pass_utils.h", line 91
  File "../libasr/asr.h", line 2410
  File "../libasr/asr.h", line 2272
  File "/Users/ondrej/repos/lpython/src/libasr/pass/print_arr.cpp", line 55
    head.m_start = PassUtils::get_bound(arr_expr, i + 1, "lbound", al, unit, rl_path, current_scope);
  File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_utils.cpp", line 329
    unit, rl_path, current_scope, arr_expr->base.loc);
  File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_utils.cpp", line 257
    ASR::Module_t *m = LFortran::ASRUtils::load_module(al, current_scope,
  File "/Users/ondrej/repos/lpython/src/libasr/asr_utils.cpp", line 104
    loc);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/functional", line 2560
    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/functional", line 1885
    return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/functional", line 1732
    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/functional", line 1558
    return _Invoker::__call(__f_.first(),
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base", line 348
    _VSTD::__invoke(_VSTD::forward<_Args>(__args)...);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/type_traits", line 2577
    {
  File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_utils.cpp", line 260
    [&](const std::string &msg, const Location &) { throw LFortranException(msg); });
LFortranException: Module 'lfortran_intrinsic_builtin' not declared in the current source and the modfile was not found
@certik
Copy link
Contributor Author

certik commented Apr 6, 2022

The builtin module is not loaded for the "print_arr" pass. A workaround it to force the frontend to load it:

def main0():
    x: i32[3] = [1, 2, 3]
    s: f64
    s = abs(1.0)
    print(x)

main0()

This is a long standing issue, the frontend should either always load the module, so that the ASR passes can use it, or perhaps the pass would accept a callback to load the module if it is not present (that might be the best way).

Now it compiles, but prints:

0
0
0

Which is another bug.

@certik
Copy link
Contributor Author

certik commented Apr 6, 2022

One can also do:

def main0():
    x: i32[3] = [1, 2, 3]
    print(x[0])
    print(x[1])
    print(x[2])

main0()

which also prints:

0
0
0

@namannimmo10
Copy link
Collaborator

def main0():
    x: i32[3] = [1, 2, 3]
    x[0] = 5
    print(x[0])
    x[2] = -4
    print(x[2])
    x[1] = x[0] + x[2]
    print(x[1])

main0()

This works, though:

$ lpython t.py && ./a.out
5
-4
1

@certik
Copy link
Contributor Author

certik commented Apr 8, 2022

Probably [1, 2, 3] should be of type list[3], and if we want this to be a ConstantArray, we have to do array([1, 2, 3]), which would be of type i32[3].

@namannimmo10
Copy link
Collaborator

OK, so I figured that the annassign visitor didn't do a strict type checking. So now, at least #324 (comment) won't work,

semantic error: Type mismatch for an annotation-assignment. In LPython, the types must be compatible.
 --> t.py:2:5
  |
2 |     x: i32[3] = [1, 2, 3]
  |     ^           ^^^^^^^^^ type mismatch (integer and list)

See #370 .

@certik
Copy link
Contributor Author

certik commented May 20, 2022

Now it prints:

semantic error: Type mismatch in annotation-assignment, the types must be compatible
 --> a.py:2:5
  |
2 |     x: i32[3] = [1, 2, 3]
  |     ^           ^^^^^^^^^ type mismatch ('i32' and 'list[i32]')

@certik
Copy link
Contributor Author

certik commented May 20, 2022

The error message should say: type mismatch ('i32[3]' and 'list[i32]')

@certik
Copy link
Contributor Author

certik commented May 20, 2022

But this code:

from numpy import empty

def main0():
    x: f64[3]
    x = empty(3)
#    x: i32[3] = array([1, 2, 3])
    print(x)

main0()

gives

$ lpython a.py
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ondrej/repos/lpython/src/bin/lpython.cpp", line 879
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir, compiler_options, time_report);
  File "/Users/ondrej/repos/lpython/src/bin/lpython.cpp", line 373
    res = fe.get_llvm3(*asr, diagnostics);
  File "/Users/ondrej/repos/lpython/src/lpython/python_evaluator.cpp", line 54
    = asr_to_llvm(asr, diagnostics,
  File "/Users/ondrej/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 4522
    pass_replace_print_arr(al, asr, rl_path);
  File "/Users/ondrej/repos/lpython/src/libasr/pass/print_arr.cpp", line 85
    v.visit_TranslationUnit(unit);
  File "../libasr/asr.h", line 3224
  File "../libasr/asr.h", line 3089
  File "../libasr/asr.h", line 2900
  File "../libasr/pass/pass_utils.h", line 135
  File "../libasr/pass/pass_utils.h", line 91
  File "../libasr/asr.h", line 3103
  File "../libasr/asr.h", line 2934
  File "/Users/ondrej/repos/lpython/src/libasr/pass/print_arr.cpp", line 55
    head.m_start = PassUtils::get_bound(arr_expr, i + 1, "lbound", al, unit, rl_path, current_scope);
  File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_utils.cpp", line 329
    unit, rl_path, current_scope, arr_expr->base.loc);
  File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_utils.cpp", line 257
    ASR::Module_t *m = LFortran::ASRUtils::load_module(al, current_scope,
  File "/Users/ondrej/repos/lpython/src/libasr/asr_utils.cpp", line 105
    loc);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/functional", line 2560
    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/functional", line 1885
    return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/functional", line 1732
    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/functional", line 1558
    return _Invoker::__call(__f_.first(),
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base", line 348
    _VSTD::__invoke(_VSTD::forward<_Args>(__args)...);
  File "/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/type_traits", line 2577
    {
  File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_utils.cpp", line 260
    [&](const std::string &msg, const Location &) { throw LFortranException(msg); });
LFortranException: Module 'lfortran_intrinsic_builtin' not declared in the current source and the modfile was not found

@Thirumalai-Shaktivel
Copy link
Collaborator

The following code works now:

from ltypes import f64
from numpy import empty
def main0():
    x: f64[3]
    x = empty(3)
    # x[0] = 2.
    # x[1] = -3.
    # x[2] = x[1] + x[0]
    print(x)

main0()

Output:

$ lpython examples/expr2.py; ./a.out
    0.00000000000000000
    0.00000000000000000
    0.00000000000000000

@Thirumalai-Shaktivel
Copy link
Collaborator

@certik, is this issue resolved?

@certik
Copy link
Contributor Author

certik commented Aug 2, 2022

I think everything works here now. Thanks!

@certik certik closed this as completed Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants