lpython icon indicating copy to clipboard operation
lpython copied to clipboard

Doubt in Implementation of `visit_Raise`

Open anutosh491 opened this issue 1 year ago • 8 comments

Consider the following example

(lf) anutosh491@spbhat68:~/lpython/lpython$ cat examples/expr2.py 
from lpython import i32

def main0():
    x: i32
    x = -1
    if x < 0:
        raise Exception("Sorry, no numbers below zero")
    print(x)

(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython examples/expr2.py 
ERROR STOP

Now if we see the AST and the implementation for visit_Raise here

(Raise
                (Call
                    (Name
                        Exception
                        Load
                    )
                    [(ConstantStr
                        "Sorry, no numbers below zero"
                        ()
                    )]
                    []
                )
                ()
            )
    void visit_Raise(const AST::Raise_t &x) {
        ASR::expr_t *code;
        if (x.m_cause) {
            visit_expr(*x.m_cause);
            code = ASRUtils::EXPR(tmp);
        } else {
            code = nullptr;
        }
        tmp = ASR::make_ErrorStop_t(al, x.base.base.loc, code);
    }

The string which we would like to make use lies in x.m_exc but we check if x.m_cause is a nullptr or not. Hence any change we make in the Exception string won't make a difference as the x.m_cause is empty in such cases. Is this intended behaviour ?

anutosh491 avatar Oct 30 '23 08:10 anutosh491

Nope, I think we have to re-design the handling. Currently, the following works:

def main0():
    x: i32
    x = -1
    assert x < 0, "Sorry, no numbers below zero"
    print(x)

Thirumalai-Shaktivel avatar Oct 30 '23 08:10 Thirumalai-Shaktivel

Yes, the raise must be fixed to error stop with that string.

certik avatar Oct 30 '23 11:10 certik

Hi, is this an issue a newcomer can work on? If yes I'll give it a try.

haanhvu avatar Jan 20 '24 16:01 haanhvu

@haanhvu you can work on any issue you want.

certik avatar Jan 20 '24 19:01 certik

(lf) anutosh491@spbhat68:~/lpython/lpython$ lpython examples/expr2.py 
ERROR STOP

I want to test this with my change. How can I build lpython from my source code?

haanhvu avatar Jan 22 '24 08:01 haanhvu

How can I build lpython from my source code?

You can follow the README.

If you have further questions, please ask at https://lfortran.zulipchat.com/#narrow/stream/311866-LPython.

certik avatar Jan 22 '24 15:01 certik

You can follow the README.

Yeah, I followed the build process here. But after that, still:

(lp) admin1@admin1-U37VC:~/lpython$ lpython examples/expr2.py
Command 'lpython' not found

Do I need to do anything else to build lpython from my forked repo?

I'll post this in chat room too.


Update: I figured it out. Thanks!

haanhvu avatar Jan 22 '24 15:01 haanhvu

To fix this issue, I need to take the string from AST::Raise_t and turn it into an ASR::expr_t right? How can I do that?

haanhvu avatar Jan 23 '24 11:01 haanhvu