lpython icon indicating copy to clipboard operation
lpython copied to clipboard

Polymorphic function args dont work

Open tanay-man opened this issue 1 year ago • 1 comments

from lpython import i32

class Base:
    def __init__(self:"Base"):
        self.x_A : i32 = 10

class Derived(Base):
    pass

def check(x: Base):
    print(x.x_A)

def main():
    d : Derived = Derived()
    check(d)
main()

Op:

(base) tanay@tanay-man:~/lcompilers/lpython$ lpython integration_tests/class_05.py 
semantic error: Type mismatch in procedure call; the types must be compatible
  --> integration_tests/class_05.py:21:11
   |
21 |     check(d)
   |           ^ type mismatch (passed argument type is struct Derived but required type is struct Base)
   |
15 | def check(x: Base):
   |              ^^^^ type mismatch (passed argument type is struct Derived but required type is struct Base)

tanay-man avatar Aug 10 '24 13:08 tanay-man

I think the subclass has to be automatically cast to the base class in this case.

certik avatar Aug 11 '24 20:08 certik

Let's represent the Cast node in the ASR

def main():
    d : Derived = Derived()
    check(d)

will become:

def main():
    d : Derived = Derived()
    check(Cast(d, DerivedToBase, Base, nullptr))

Using the Base name, get the Base type in the asr to llvm backend.

Thirumalai-Shaktivel avatar Aug 13 '24 16:08 Thirumalai-Shaktivel

@Thirumalai-Shaktivel I have implemented the ASR level cast node for this. How to get the cast working in the llvm backend? Current op-

(lp) tanay@tanay-man:~/lcompilers/lpython$ lpython integration_tests/class_06.py 
code generation error: Cast kind not implemented

tanay-man avatar Aug 15 '24 19:08 tanay-man