lpython
lpython copied to clipboard
Misleading error message
from numpy import empty, int16
from lpython import (i16, i32, CPtr, ccall, sizeof)
@ccall
def _lfortran_malloc(size : i32) -> CPtr:
"""borrowed from bindc_07.py in integration_tests"""
pass
def main():
n : i32 = 15 # <~~~~~~~~~~~ LOOKS LIKE COMPILE-TIME CONSTANT ??? ~~~~~~~~~~
m : i32 = 3
# Emulate getting stuff from the C side.
Anm_l4 : CPtr = _lfortran_malloc( (n * m) * i32(sizeof(i16)) )
A_nm: i16[n, m] = empty((n, m), dtype=int16)
if __name__ == "__main__":
main()
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)────(brian@MacBook-Pro:s001)─┐
└─(12:16:55 on vector-backend ✹ ✭)──> lpython ../ISSUES/UNHANDLED-EXCEPTIONS/Issue2492.py 2 ↵ ──(Tue,Feb06)─┘
semantic error: Only those local variables which can be reduced to compile time constant should be used in dimensions of an array.
--> ../ISSUES/UNHANDLED-EXCEPTIONS/Issue2492.py:18:15
|
18 | A_nm: i16[n, m] = empty((n, m), dtype=int16)
| ^
Some more possibilities:
# The following does not work in lpython (Issue #2492)
# A_nm: i16[n, m] = empty((n, m), dtype=int16)
# doesn't work
# A_nm : i16[:] = empty((n, m), dtype=int16)
# works!
A_nm : Allocatable[i16[:]] = empty((n, m), dtype=int16)
The following produces an unhandled exception
A_nm : Array[i16[:]] = empty((n, m), dtype=int16)
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)────────────────────────────────────────────────────────────────────────────────(brian@MacBook-Pro:s001)─┐
└─(12:48:58 on vector-backend ✹ ✭)──> lpython matmul_integration.py ──(Tue,Feb06)─┘
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/bin/lpython.cpp", line 1872
err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/bin/lpython.cpp", line 786
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7996
auto res2 = body_visitor(al, lm, *ast_m, diagnostics, unit, main_module, module_name,
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7932
BodyVisitor b(al, lm, unit, diagnostics, main_module, module_name, ast_overload, allow_implicit_casting);
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4713
visit_stmt(*x.m_body[i]);
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1883
void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1751
case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4814
handle_fn(x, *f);
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4790
transform_stmts(body, x.n_body, x.m_body);
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4667
this->visit_stmt(*m_body[i]);
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1883
void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/python_ast.h", line 1757
case stmtType::AugAssign: { v.visit_AugAssign((const AugAssign_t &)x); return; }
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4885
visit_AnnAssignUtil(x, var_name, init_expr);
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 2785
type = ast_expr_to_asr_type(x.m_annotation->base.loc, *x.m_annotation, is_allocatable, true, abi);
File "/Users/brian/Dropbox/Mac/Documents/GitHub/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 1739
LCOMPILERS_ASSERT(AST::is_a<AST::Tuple_t>(*s->m_slice));
AssertFailed: AST::is_a<AST::Tuple_t>(*s->m_slice)
We need to declare the variables m, n as constants. For example:
% cat examples/expr2.py
from lpython import i16, i32, Const
from numpy import empty, int16
def main():
n : Const[i32] = 15 # <~~~~~~~~~~~ LOOKS LIKE COMPILE-TIME CONSTANT ??? ~~~~~~~~~~
m : Const[i32] = 3
# Emulate getting stuff from the C side.
A_nm: i16[n, m] = empty((n, m), dtype=int16)
print(A_nm.size)
if __name__ == "__main__":
main()
% python examples/expr2.py
45
% lpython examples/expr2.py
45
Thanks! I'm leaving it open because of the uncaught exception, but your answer with Const unblocks my work!