Odd issue with virtual methods in test

I was trying to write an interop test, and got sidetracked trying to get even the simplest bit of inheritance working. I figured I must be doing something wrong..

my header:

class Foo {
 public:
  Foo() { }
  virtual int Hello() { return 4; }
};

class Bar : public Foo {
 public:
   Bar() { }
   virtual int Hello() { return 5; }
};

And my swift test:

MyTestSuite.test("do something") {
  var b = Bar();
  print(b.Hello())
}

Results in an error about it being ambiguous:

 error: ambiguous use of 'Hello()'
  print(b.Hello())
        ^
Polymorphism.Bar:3:26: note: found this candidate
    public mutating func Hello() -> Int32
                         ^
<unknown>:0: note: found this candidate

This is with ToT updated Dec 12 2022 or so.

I know it's got to be obvious... :slight_smile:

-pink

Thanks, I think this might be a bug when it comes to folding inherited bases into the Swift class representation. Could you file an issue on GitHub? Also, note that we currently don't correctly invoke virtual methods from Swift - [interop] Dispatch virtual method calls correctly · Issue #62354 · apple/swift · GitHub.

Ok I'll file this. I was convinced I was doing something wrong.

Thanks for the pointer to the other bug, it's actually the failure test case I was writing a test for. Do you know if one already exists?

What do you mean if one already exists? There's a reproducer in the bug I linked to , but we don't have a failing test case committed to the repository.

That's what I meant, one committed in the repo. I'll submit a PR shortly.

Bug for this filed as [cxx-interop] Error for "ambiguous" overridden virtual method call · Issue #62620 · apple/swift · GitHub

1 Like