Skip to content

Commit 7a809ef

Browse files
authored
Merge pull request #62618 from mikepinkerton/polymorphism
Failing test for runtime polymorphism.
2 parents e709d94 + 742d478 commit 7a809ef

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

test/Interop/Cxx/class/inheritance/Inputs/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ module Fields {
66
header "fields.h"
77
}
88

9+
module Polymorphism {
10+
header "polymorphism.h"
11+
requires cplusplus
12+
}
13+
914
module SubTypes {
1015
header "sub-types.h"
1116
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef TEST_INTEROP_CXX_CLASS_INHERITANCE_POLYMORPHISM_H_
2+
#define TEST_INTEROP_CXX_CLASS_INHERITANCE_POLYMORPHISM_H_
3+
4+
class Shape {
5+
public:
6+
virtual int NumberOfSides() { return 0; }
7+
};
8+
9+
class Rectangle : public Shape {
10+
public:
11+
virtual int NumberOfSides() { return 4; }
12+
};
13+
14+
// For testing runtime polymorphism.
15+
Shape* MakeShape() {
16+
return new Rectangle();
17+
}
18+
19+
#endif // TEST_INTEROP_CXX_CLASS_INHERITANCE_POLYMORPHISM_H_
20+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
//
5+
// XFAIL: *
6+
7+
import Polymorphism
8+
import StdlibUnittest
9+
10+
11+
var PolymorphismTestSuite = TestSuite("Determining if runtime polymorphism works")
12+
13+
14+
PolymorphismTestSuite.test("Call overridden methods with pointer to base type") {
15+
// MakeShape() creates a Rectangle and returns the object as a Shape*.
16+
let shape = MakeShape()
17+
18+
// DOES NOT WORK: executes the Shape implementation, not the Rectangle
19+
// version, and thus fails the comparision (0 != 4)
20+
// Filed as https://github.com/apple/swift/issues/62354.
21+
expectEqual(shape!.pointee.NumberOfSides(), 4)
22+
}
23+
24+
runAllTests()

0 commit comments

Comments
 (0)