Skip to content

Commit 68bfbdd

Browse files
[SwiftToCxx] Creating new test case and fixing namespace
1 parent 9ebc0bd commit 68bfbdd

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
695695
// Create the condition and the statement to throw an exception.
696696
if (hasThrows) {
697697
os << " if (opaqueError != nullptr)\n";
698-
os << " throw (swift::_impl::swift::Error(opaqueError));\n";
698+
os << " throw (swift::Error(opaqueError));\n";
699699
}
700700

701701
// Return the function result value if it doesn't throw.

stdlib/public/SwiftShims/_SwiftCxxInteroperability.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ extern "C" void *_Nonnull swift_retain(void *_Nonnull) noexcept;
3232

3333
extern "C" void swift_release(void *_Nonnull) noexcept;
3434

35-
extern "C" void *_Nonnull swift_errorRetain(void *_Nonnull swiftError) noexcept;
36-
37-
extern "C" void swift_errorRelease(void *_Nonnull swiftError) noexcept;
38-
3935
inline void *_Nonnull opaqueAlloc(size_t size, size_t align) noexcept {
4036
#if defined(_WIN32)
4137
void *r = _aligned_malloc(size, align);
@@ -176,7 +172,12 @@ template <class T> inline void *_Nonnull getOpaquePointer(T &value) {
176172
return reinterpret_cast<void *>(&value);
177173
}
178174

179-
namespace swift {
175+
} // namespace _impl
176+
177+
extern "C" void *_Nonnull swift_errorRetain(void *_Nonnull swiftError) noexcept;
178+
179+
extern "C" void swift_errorRelease(void *_Nonnull swiftError) noexcept;
180+
180181
class Error {
181182
public:
182183
Error() {}
@@ -198,9 +199,6 @@ class Error {
198199
private:
199200
void * _Nonnull opaqueValue = nullptr;
200201
};
201-
}
202-
203-
} // namespace _impl
204202

205203
#pragma clang diagnostic pop
206204

test/Interop/SwiftToCxx/functions/swift-functions-errors-execution.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ int main() {
2121

2222
try {
2323
Functions::emptyThrowFunction();
24-
} catch (swift::_impl::swift::Error& e) {
24+
} catch (swift::Error& e) {
2525
printf("Exception\n");
2626
}
2727
try {
2828
Functions::throwFunction();
29-
} catch (swift::_impl::swift::Error& e) {
29+
} catch (swift::Error& e) {
3030
printf("Exception\n");
3131
}
3232
try {
3333
Functions::throwFunctionWithReturn();
34-
} catch (swift::_impl::swift::Error& e) {
34+
} catch (swift::Error& e) {
3535
printf("Exception\n");
3636
}
37+
try {
38+
Functions::testDestroyedError();
39+
} catch(const swift::Error &e) { }
3740

3841
return 0;
3942
}
@@ -43,3 +46,4 @@ int main() {
4346
// CHECK-NEXT: Exception
4447
// CHECK-NEXT: passThrowFunctionWithReturn
4548
// CHECK-NEXT: Exception
49+
// CHECK-NEXT: Test destroyed

test/Interop/SwiftToCxx/functions/swift-functions-errors.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,27 @@ public func emptyThrowFunction() throws { print("passEmptyThrowFunction") }
2525
// CHECK: void* self = nullptr;
2626
// CHECK: _impl::$s9Functions18emptyThrowFunctionyyKF(self, &opaqueError);
2727
// CHECK: if (opaqueError != nullptr)
28-
// CHECK: throw (swift::_impl::swift::Error(opaqueError))
28+
// CHECK: throw (swift::Error(opaqueError))
29+
// CHECK: }
30+
31+
class TestDestroyed {
32+
deinit {
33+
print("Test destroyed")
34+
}
35+
}
36+
37+
public struct DestroyedError : Error {
38+
let t = TestDestroyed()
39+
}
40+
41+
public func testDestroyedError() throws { throw DestroyedError() }
42+
43+
// CHECK: inline void testDestroyedError() {
44+
// CHECK: void* opaqueError = nullptr;
45+
// CHECK: void* self = nullptr;
46+
// CHECK: _impl::$s9Functions18testDestroyedErroryyKF(self, &opaqueError);
47+
// CHECK: if (opaqueError != nullptr)
48+
// CHECK: throw (swift::Error(opaqueError))
2949
// CHECK: }
3050

3151
public func throwFunction() throws {
@@ -38,7 +58,7 @@ public func throwFunction() throws {
3858
// CHECK: void* self = nullptr;
3959
// CHECK: _impl::$s9Functions13throwFunctionyyKF(self, &opaqueError);
4060
// CHECK: if (opaqueError != nullptr)
41-
// CHECK: throw (swift::_impl::swift::Error(opaqueError))
61+
// CHECK: throw (swift::Error(opaqueError))
4262
// CHECK: }
4363

4464
public func throwFunctionWithReturn() throws -> Int {
@@ -52,6 +72,6 @@ public func throwFunctionWithReturn() throws -> Int {
5272
// CHECK: void* self = nullptr;
5373
// CHECK: auto returnValue = _impl::$s9Functions23throwFunctionWithReturnSiyKF(self, &opaqueError);
5474
// CHECK: if (opaqueError != nullptr)
55-
// CHECK: throw (swift::_impl::swift::Error(opaqueError))
75+
// CHECK: throw (swift::Error(opaqueError))
5676
// CHECK: return returnValue;
5777
// CHECK: }

0 commit comments

Comments
 (0)