Skip to content

Add a test documenting the current state of C++ exception handling #30674

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

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 20 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/CxxExceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class MyException {
public:
virtual ~MyException() {}
};

// Calls f and catches any exceptions thrown by f.
// Returns whether an exception was caught.
inline bool callAndCatchExceptions(void (*f)()) {
try {
f();
} catch (...) {
return true;
}

return false;
}

inline void throwException() {
throw MyException();
}
5 changes: 5 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/module.map
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ module CoreCooling {
export *
}

module CxxExceptions {
header "CxxExceptions.h"
link "stdc++"
}

module CXXInterop {
header "cxx_interop.h"
}
Expand Down
40 changes: 40 additions & 0 deletions test/ClangImporter/cxx-exceptions-executable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -I %S/Inputs/custom-modules/ -o %t/cxx_exceptions -Xfrontend -enable-cxx-interop
// RUN: %target-codesign %t/cxx_exceptions
// RUN: %target-run %t/cxx_exceptions

import CxxExceptions
import StdlibUnittest

var CxxExceptionsTestSuite = TestSuite("CxxExceptions")

// Uncaught C++ exceptions terminate the program.
CxxExceptionsTestSuite.test("UncaughtException") {
expectCrashLater(withMessage: "terminate called")
throwException()
}

// Exceptions can be thrown and caught within C++ code if they don't cross any
// interop boundaries. In addition, ClangImporter correctly codegens throw and
// try-catch in a C++ inline function.
CxxExceptionsTestSuite.test("ExceptionCaughtWithinCpp") {
expectTrue(callAndCatchExceptions(throwException))
}

// Exceptions cannot be thrown across interop boundaries. If while unwinding the
// stack we reach a Swift stack frame, the program terminates.
// FIXME: This test documents the current behavior, which is wrong. Currently,
// exceptions will propagate through Swift code. This is bad, because Swift
// stack frames will be unwound without doing any potentially necessary
// cleanups.
// I'm documenting the wrong behavior instead of making this an XFAIL because I
// want to show exactly how this fails today.
CxxExceptionsTestSuite.test("DontUnwindAcrossSwiftStackFrame") {
func callThrowException() {
throwException()
}

expectTrue(callAndCatchExceptions(callThrowException))
}

runAllTests()