Skip to content

Typechecker rejects callable argument in virtual method call #850

Description

@BI71317

The compiler fails to typecheck this program:

class A:
    def f(self, block: CallableTrait[[], bool]):
        return False

class B(A):
    def f(self, block: CallableTrait[[], bool]):
        return block()

def make_base() -> A:
    return B()

print(make_base().f(lambda: False or True))
print(make_base().f(lambda: True and False))

B inherits from A. The function make_base is annotated as returning A, but it actually returns an instance of B.
So it should dispatch to B.f, execute the passed lambda, and return the lambda result.
However, it looks like the program fails during typechecking before it can get as far as calling either A.f or B.f.

Result

$ codon run -release 04_factory_base_return_callable.codon 
04_factory_base_return_callable.codon:15 (1-44): error: cannot typecheck 'print(make_base().f(lambda: False or True))'
04_factory_base_return_callable.codon:16 (1-45): error: cannot typecheck 'print(make_base().f(lambda: True and False))'

Expected to print:

True
False

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