codon icon indicating copy to clipboard operation
codon copied to clipboard

Handling a self-defined error leads to the failure of codon

Open xiaxinmeng opened this issue 1 year ago • 4 comments

We define a attributeError in the following code. Then we raise and catch it. The expected behavior is that the compiler can handle it well and print "hi", while codon fails. CPython can work well with this case.

test.py

class B:
     def __bool__(self):
         raise AttributeError("don't do that!")

b = B()
try:
   if b:
        pass
except AttributeError:
   print("HI")

Reproduce: codon/codon-linux-x86_64/codon-deploy/bin/codon' run -release test.py

Crash message:

Trunc only operates on integer
  %238 = trunc {} %220 to i1, !dbg !2405
Assert failed:	module broken
Expression:	!broken
Source:		/github/workspace/codon/cir/llvm/optimize.cpp:601
Aborted (core dumped)

Behavior on CPython 3.10.8: work well

Environment: codon: v0.15.5 on Feb 6 Ubuntu 18.04

xiaxinmeng avatar Mar 15 '23 09:03 xiaxinmeng

The following code works as expected:

class B:
     def __bool__(self) -> bool:
         raise AttributeError("don't do that!")

b = B()
try:
   if b:
        pass
except AttributeError:
   print("HI")

learnforpractice avatar Mar 15 '23 10:03 learnforpractice

I think its easier to list cases when codon actually works, than crashes :) And there are no updates for long time.

Nikita-Sherstnev avatar Mar 15 '23 20:03 Nikita-Sherstnev

Thanks for the report — it seems there’s a check missing in the type checker to ensure __bool__ actually returns a boolean. We’ll fix it in the next release.

We plan to put out v0.16 in the next couple weeks. We’re a small team currently so appreciate your patience.

arshajii avatar Mar 15 '23 21:03 arshajii

Thanks for your confirmation. I hope these bugs are helpful for the development of codon.

xiaxinmeng avatar Mar 16 '23 01:03 xiaxinmeng