Skip to content

Commit 9adad32

Browse files
committed
C++ Interop: add tests for extensions of C++ classes
1 parent c01de63 commit 9adad32

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_EXTENSIONS_H
2+
#define TEST_INTEROP_CXX_CLASS_INPUTS_EXTENSIONS_H
3+
4+
namespace Outer {
5+
namespace Space {
6+
7+
struct Foo {
8+
int a;
9+
};
10+
11+
}
12+
}
13+
14+
#endif //TEST_INTEROP_CXX_CLASS_INPUTS_EXTENSIONS_H

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ module Destructors {
2323
requires cplusplus
2424
}
2525

26+
module Extensions {
27+
header "extensions.h"
28+
requires cplusplus
29+
}
30+
2631
module LoadableTypes {
2732
header "loadable-types.h"
2833
requires cplusplus
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-cxx-interop)
2+
// REQUIRES: executable_test
3+
4+
import StdlibUnittest
5+
import Extensions
6+
7+
var CxxClassExtensionsTestSuite = TestSuite("CxxClassExtensions")
8+
9+
extension Outer.Space.Foo {
10+
func bar() -> Int32 { return a + 1 }
11+
}
12+
13+
CxxClassExtensionsTestSuite.test("calling extension method") {
14+
var foo = Outer.Space.Foo(a: 123)
15+
expectEqual(124, foo.bar())
16+
}
17+
18+
runAllTests()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2+
//
3+
// We can't yet call member functions correctly on Windows (SR-13129).
4+
// XFAIL: OS=windows-msvc
5+
6+
import Extensions
7+
8+
extension Outer.Space.Foo {
9+
func foo() {}
10+
}
11+
12+
Outer.Space.Foo().foo()
13+
14+
// CHECK: call swiftcc void @"$sSo5OuterO5SpaceO10ExtensionsE3FooV4mainE3fooyyF"
15+
16+
// CHECK: define hidden swiftcc void @"$sSo5OuterO5SpaceO10ExtensionsE3FooV4mainE3fooyyF"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-cxx-interop
2+
3+
import Extensions
4+
5+
extension Outer.Space.Foo {
6+
func bar() -> Int32 { return a }
7+
}
8+
9+
extension Outer.Space.Foo: ExpressibleByIntegerLiteral {
10+
public init(integerLiteral value: IntegerLiteralType) {
11+
self.init(a: Int32(value))
12+
}
13+
}
14+
15+
var it: Outer.Space.Foo = 123
16+
let res = it.bar()

0 commit comments

Comments
 (0)