lcompilers / lpython

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

Programming error produces traceback instead of error message #2084

Open rebcabin opened 1 year ago

rebcabin commented 1 year ago

Consider the following, which has a programming error:

@dataclass
class LPBHV_small:
    dim : i32 = 4
    a : i8[4] = empty(4, dtype=int8)

def as_hex(lphv_small : LPBHV_small) -> None:
    i : i32
    for i in range(4):
        hx : str = hex(lphv_small[i])  ########### ATTENTION ##########
        print(hx)

The attention line should be

        hx : str = hex(lphv_small.a[i])  ########### ATTENTION ##########

I get the following traceback:

/Users/brian/CLionProjects/lpython/src/bin/python /Users/brian/CLionProjects/lpython/lasr/LP-pycharm/hdc.py 
ASR verify pass error: ASR verify: The variable in ArrayItem must be an array, not a scalar
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/brian/CLionProjects/lpython/src/bin/lpython.cpp", line 1844
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/Users/brian/CLionProjects/lpython/src/bin/lpython.cpp", line 783
    r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
  File "/Users/brian/CLionProjects/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 7583
    throw LCompilersException("Verify failed");
LCompilersException: Verify failed

LPython should produce a more informative error message.

The entire offending example is

from lpython import (i8, i32, i64, f32, f64,
                     TypeVar, Const,
                     dataclass)
from numpy import empty, sqrt, float32, float64, int8

# Issue 2066
n = TypeVar("n")

def modify(b: f64[:], n: i32) -> f64[n]:
    return sqrt(b)

# Issue 2072; workaround is f64 instead of f32
def verify(a: f64[:], b: f64[:], result: f64[:], size: i32):
    i: i32
    eps: f64
    eps = f64(1e-6)

    for i in range(size):
        check : f64 = abs(a[i] * a[i] + sqrt(b[i]) - result[i])

        if not check <= eps or i == 5792:
            print("a[", end='')
            print(i, end='')
            print("] = ", end='')
            print(a[i], end='')
            print(", b[", end='')
            print(i,end='')
            print("] = ", end='')
            print(b[i])
            print("a[", end='')
            print(i, end='')
            print("] * a[", end='')
            print(i, end='')
            print("] + sqrt(b[", end='')
            print(i, end='')
            print("]) = ", end='')
            print(a[i] * a[i] + sqrt(b[i]))
            print("a[", end='')
            print(i, end='')
            print("] ** 2      + sqrt(b[", end='')
            print(i, end='')
            print("]) = ", end='')
            print(result[i])
            print('')

        # assert check <= eps

HDC_DIM: Const[i32] = 8192

@dataclass
class LPBHV:
    # Issue 2083: can't say HDC_DIM
    dim : i32 = 8192
    a : i8[8192] = empty(8192, dtype=int8)

@dataclass
class LPBHV_small:
    dim : i32 = 4
    a : i8[4] = empty(4, dtype=int8)

def as_hex(lphv_small : LPBHV_small) -> None:
    i : i32
    for i in range(4):
        hx : str = hex(lphv_small[i])
        print(hx)

def g() -> None:
    lpbhv_small : LPBHV_small = LPBHV_small()
    print(lpbhv_small.a)

def f():
    i: i32
    j: i32

    # Issue 2067
    a: f64[8192] = empty(HDC_DIM, dtype=float64)
    b: f64[8192] = empty(HDC_DIM, dtype=float64)
    c: f64[8192] = empty(HDC_DIM, dtype=float64)

    for i in range(HDC_DIM):
        a[i] = f64(i)

    # print(a)

    for j in range(HDC_DIM):
        b[j] = f64(j + 5)

    c = a ** f64(2) + modify(b, HDC_DIM)
    verify(a, b, c, HDC_DIM)

if __name__ == "__main__":
    print("Module HDC")
    g()
    f()
certik commented 1 year ago

Yes, every time you get an "ASR verify pass error", it's an internal compiler error. There are two bugs here: