Skip to content

Failing test for runtime polymorphism. #62618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ module Fields {
header "fields.h"
}

module Polymorphism {
header "polymorphism.h"
requires cplusplus
}

module SubTypes {
header "sub-types.h"
}
Expand Down
20 changes: 20 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/polymorphism.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef TEST_INTEROP_CXX_CLASS_INHERITANCE_POLYMORPHISM_H_
#define TEST_INTEROP_CXX_CLASS_INHERITANCE_POLYMORPHISM_H_

class Shape {
public:
virtual int NumberOfSides() { return 0; }
};

class Rectangle : public Shape {
public:
virtual int NumberOfSides() { return 4; }
};

// For testing runtime polymorphism.
Shape* MakeShape() {
return new Rectangle();
}

#endif // TEST_INTEROP_CXX_CLASS_INHERITANCE_POLYMORPHISM_H_

24 changes: 24 additions & 0 deletions test/Interop/Cxx/class/inheritance/runtime-polymorphism.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop)
//
// REQUIRES: executable_test
//
// XFAIL: *

import Polymorphism
import StdlibUnittest


var PolymorphismTestSuite = TestSuite("Determining if runtime polymorphism works")


PolymorphismTestSuite.test("Call overridden methods with pointer to base type") {
// MakeShape() creates a Rectangle and returns the object as a Shape*.
let shape = MakeShape()

// DOES NOT WORK: executes the Shape implementation, not the Rectangle
// version, and thus fails the comparision (0 != 4)
// Filed as https://github.com/apple/swift/issues/62354.
expectEqual(shape!.pointee.NumberOfSides(), 4)
}

runAllTests()