C++ Interop workgroup meeting notes (February 21st 2023, exception handling, interop versioning)

This post contains the summarized meeting notes from the Swift and C++ interoperability workgroup sync-up meeting from the 21st of February.

Attendees: Alex_L, zoecarver, plotfi, NuriAmari


Two things on the design discussion agenda: exception handling and versioning.

Exception handling

  • Would like to avoid UB for projects that use exceptions in Swift-5.9 release.
    • Note: Unwinding through a Swift stack frame is UB.
  • Proposal: trap on uncaught exception during the call to C/C++ function when interop is enabled. This guarantees that we won't unwind through Swift frames.
    • No need to do that when exceptions are disabled in the Clang flags passed to Swift.
  • Initially, LLDB debugger would show you the location of the call during which exception was thrown, this can be improved later to stop at the point of where exception was thrown (for Itanium ABI):
* thread #1, queue = 'com.apple.main-thread', stop reason = Swift runtime failure: unhandled C++ / Objective-C exception
    frame #1: 0x0000000100007200 trap-exceptions`testMe() at trap-on-exception-execution.swift:31:5
   30  	func testMe() {
-> 31  	    freeFunctionThrows(2)
   32  	}
   34  	testMe()

Interoperability versioning

  • Existing adopters of interoperability do not have any stability guarantees.
    • Will become more of an issue as interoperability support will evolve and change overtime, breaking adopters code.
  • Proposal: version interoperability support starting in the Swift-5.9 release.
    • E.g. Swift-5.9 supports interoperability version "5.9".
    • Swift-XYZ supports interoperability versions "5.9" and "XYZ"
  • New Swift compiler releases should support older interop versions (going back one / several releases).
  • Provides grace period for adopters of "5.9" interoperability before they must adopt newer interoperability with any required code changes.
  • Requires addition of a new flag to Swift. The flag enables C++ interop and sets the version.
  • Imposes additional engineering work for all future interop changes.
    • Would like to have a Swift-5.9 compatibility test suite composed of existing unit tests to do some compatibility verification for PRs and in CI to help with engineering.