Skip to content

Polymorphic function args dont work #2799

Closed

Description

@tanay-man
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)

Activity

  1. certik commented on Aug 11, 2024

    @certik
    Contributor

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

  2. Thirumalai-Shaktivel commented on Aug 13, 2024

    @Thirumalai-Shaktivel
    Collaborator

    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.

  3. tanay-man commented on Aug 15, 2024

    @tanay-man
    ContributorAuthor

    @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
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions