Skip to content

Commit 0664c3f

Browse files
authored
Add a distinct hook for swift_willThrowTypedImpl(). (#71651)
Add a distinct hook for `swift_willThrowTypedImpl()`. This PR adds a hook for `swift_willThrowTypedImpl()` that is distinct from `swift_willThrow()`. The current implementation of `swift_willThrowTypedImpl()` creates a temporary existential box for the thrown error and calls the `_swift_willThrow` hook that is set by XCTest and swift-testing. Unfortunately, this temporary existential box isn't very useful because its address is not expected to be stable. Instead, expose a separate hook, `_swift_willThrowTypedImpl`, whose signature matches that of the new ABI function. This hook can then be used by XCTest and swift-testing to distinguish between typed throws and untyped throws and avoids creating a temporary existential box when it won't be useful. As implemented, if `_swift_willThrowTypedImpl` is not set but `_swift_willThrow` is, the `swift_willThrowTypedImpl()` will fall back to calling `_swift_willThrow` with a temporary existential box. We might decide this is unnecessary, but I figured it would allow us more time to stage adoption of the new hook in the testing libraries. Resolves rdar://122824443.
1 parent 3a80ed7 commit 0664c3f

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

stdlib/public/runtime/ErrorObjectCommon.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,37 @@ swift::swift_willThrow(SWIFT_CONTEXT void *unused,
4343
}
4444
}
4545

46+
std::atomic<void (*)(
47+
OpaqueValue *value,
48+
const Metadata *type,
49+
const WitnessTable *errorConformance
50+
)> swift::_swift_willThrowTypedImpl;
51+
4652
/// Breakpoint hook for debuggers that is called for typed throws, and calls
47-
/// _swift_willThrow if set. This implicitly boxes the typed error in an
48-
/// any Error for the call.
53+
/// _swift_willThrowTypedImpl if set. If not set and _swift_willThrow is set, this calls
54+
/// that hook instead and implicitly boxes the typed error in an any Error for that call.
4955
SWIFT_CC(swift) void
5056
swift::swift_willThrowTypedImpl(OpaqueValue *value,
5157
const Metadata *type,
5258
const WitnessTable *errorConformance) {
5359
// Cheap check to bail out early, since we expect there to be no callbacks
5460
// the vast majority of the time.
55-
auto handler = _swift_willThrow.load(std::memory_order_acquire);
61+
auto handler = _swift_willThrowTypedImpl.load(std::memory_order_acquire);
5662
if (SWIFT_UNLIKELY(handler)) {
57-
// Form an error box containing the error.
58-
BoxPair boxedError = swift_allocError(
63+
(* handler)(value, type, errorConformance);
64+
} else {
65+
auto fallbackHandler = _swift_willThrow.load(std::memory_order_acquire);
66+
if (SWIFT_UNLIKELY(fallbackHandler)) {
67+
// Form an error box containing the error.
68+
BoxPair boxedError = swift_allocError(
5969
type, errorConformance, value, /*isTake=*/false);
6070

61-
// Hand the boxed error off to the handler.
62-
auto errorBox = reinterpret_cast<SwiftError *>(boxedError.object);
63-
(* handler)(errorBox);
71+
// Hand the boxed error off to the handler.
72+
auto errorBox = reinterpret_cast<SwiftError *>(boxedError.object);
73+
(* fallbackHandler)(errorBox);
6474

65-
// Release the error box.
66-
swift_errorRelease(errorBox);
75+
// Release the error box.
76+
swift_errorRelease(errorBox);
77+
}
6778
}
6879
}

stdlib/public/runtime/ErrorObjectTestSupport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace swift {
2323

2424
#if defined(__cplusplus)
2525
SWIFT_RUNTIME_EXPORT std::atomic<void (*)(SwiftError *error)> _swift_willThrow;
26+
SWIFT_RUNTIME_EXPORT std::atomic<void (*)(
27+
OpaqueValue *value,
28+
const Metadata *type,
29+
const WitnessTable *errorConformance
30+
)> _swift_willThrowTypedImpl;
2631
#endif
2732

2833
/// Set the value of @c _swift_willThrow atomically.

test/abi/macOS/arm64/stdlib.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,5 @@ Added: __swift_pod_indirect_initializeBufferWithCopyOfBuffer
256256
Added: __swift_validatePrespecializedMetadata
257257
Added: __swift_exceptionPersonality
258258
Added: _swift_willThrowTypedImpl
259+
Added: __swift_willThrowTypedImpl
259260
Added: __swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,5 @@ Added: __swift_pod_indirect_initializeBufferWithCopyOfBuffer
256256
Added: __swift_validatePrespecializedMetadata
257257
Added: __swift_exceptionPersonality
258258
Added: _swift_willThrowTypedImpl
259+
Added: __swift_willThrowTypedImpl
259260
Added: __swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly

0 commit comments

Comments
 (0)