lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.37k stars 157 forks source link

Bug: Assert failure in logical operation #2597

Closed Shaikh-Ubaid closed 3 months ago

Shaikh-Ubaid commented 3 months ago
% cat examples/expr2.py 
print(2.0 > 0.0 and 2.0**0.5)
% python examples/expr2.py                
1.4142135623730951
% lpython examples/expr2.py --backend llvm
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/bin/lpython.cpp", line 1929
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/bin/lpython.cpp", line 837
    r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 8149
    auto res2 = body_visitor(al, lm, *ast_m, diagnostics, unit, main_module, module_name,
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 8085
    BodyVisitor b(al, lm, unit, diagnostics, main_module, module_name, ast_overload, allow_implicit_casting);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 4732
    visit_stmt(*x.m_body[i]);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_ast.h", line 1883
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_ast.h", line 1772
    case stmtType::Nonlocal: { v.visit_Nonlocal((const Nonlocal_t &)x); return; }
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 6524
    AST::Call_t *c = AST::down_cast<AST::Call_t>(x.m_value);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7618
    visit_expr_list(x.m_args, x.n_args, args);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 613
    this->visit_expr(*exprs[i]);
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_ast.h", line 1910
    void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_ast.h", line 1784
    case exprType::BoolOp: { v.visit_BoolOp((const BoolOp_t &)x); return; }
  File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3329
    LCOMPILERS_ASSERT(
AssertFailed: ASRUtils::check_equal_type(ASRUtils::expr_type(lhs), ASRUtils::expr_type(rhs))

We should rather be printing a semantic error that the types to the logical operation are not equal and also are not of type logical.

Shaikh-Ubaid commented 3 months ago

From https://github.com/lcompilers/lpython/pull/1506#discussion_r1521780154, it looks like and operation on types other than logical is valid.

>>> 2 and 3
3
>>> 0 and 3
0
>>> True and 3
3
>>> False and 3
False
>>> -1 and 3
3

It returns the left side value if it is false, otherwise the right side value. So, in this current issue, we just need to fix the assert failure shared in the issue description (https://github.com/lcompilers/lpython/issues/2597#issue-2178514051) above.

Shaikh-Ubaid commented 3 months ago

Another example related to this shared here https://github.com/lcompilers/lpython/pull/2598#issuecomment-1997370218.

% cat examples/expr2.py 
print(1 or 3)
% python examples/expr2.py          
1
% lpython_in_main examples/expr2.py 
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/bin/lpython.cpp", line 1932
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/bin/lpython.cpp", line 837
    r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/semantics/python_ast_to_asr.cpp", line 8149
    auto res2 = body_visitor(al, lm, *ast_m, diagnostics, unit, main_module, module_name,
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/semantics/python_ast_to_asr.cpp", line 8085
    BodyVisitor b(al, lm, unit, diagnostics, main_module, module_name, ast_overload, allow_implicit_casting);
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/semantics/python_ast_to_asr.cpp", line 4732
    visit_stmt(*x.m_body[i]);
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/python_ast.h", line 1883
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/python_ast.h", line 1772
    case stmtType::Nonlocal: { v.visit_Nonlocal((const Nonlocal_t &)x); return; }
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/semantics/python_ast_to_asr.cpp", line 6524
    AST::Call_t *c = AST::down_cast<AST::Call_t>(x.m_value);
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/semantics/python_ast_to_asr.cpp", line 7618
    visit_expr_list(x.m_args, x.n_args, args);
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/semantics/python_ast_to_asr.cpp", line 613
    this->visit_expr(*exprs[i]);
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/python_ast.h", line 1910
    void visit_expr(const expr_t &b) { visit_expr_t(b, self()); }
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/python_ast.h", line 1784
    case exprType::BoolOp: { v.visit_BoolOp((const BoolOp_t &)x); return; }
  File "/Users/ubaid/Desktop/OpenSource/lpython_in_main/src/lpython/semantics/python_ast_to_asr.cpp", line 3336
    LCOMPILERS_ASSERT(ASR::is_a<ASR::Logical_t>(*dest_type));
AssertFailed: ASR::is_a<ASR::Logical_t>(*dest_type)