Skip to content

Commit 97d4c18

Browse files
authored
Refine the Foundation error backtracing guard (#813)
Follow-on from #812: * Move the check slightly to ensure that it is applied in all scenarios. * Disable the behavior by default due to it causing problems for some tests which perform equality checks on NSError objects.
1 parent e2528b9 commit 97d4c18

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Sources/Testing/SourceAttribution/Backtrace.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,15 @@ extension Backtrace {
337337
/// first time the value of this property is read.
338338
static let isFoundationCaptureEnabled = {
339339
#if SWT_TARGET_OS_APPLE && !SWT_NO_DYNAMIC_LINKING
340-
let _CFErrorSetCallStackCaptureEnabled = symbol(named: "_CFErrorSetCallStackCaptureEnabled").map {
341-
unsafeBitCast($0, to: (@convention(c) (DarwinBoolean) -> DarwinBoolean).self)
340+
if Environment.flag(named: "SWT_FOUNDATION_ERROR_BACKTRACING_ENABLED") == true {
341+
let _CFErrorSetCallStackCaptureEnabled = symbol(named: "_CFErrorSetCallStackCaptureEnabled").map {
342+
unsafeBitCast($0, to: (@convention(c) (DarwinBoolean) -> DarwinBoolean).self)
343+
}
344+
_ = _CFErrorSetCallStackCaptureEnabled?(true)
345+
return _CFErrorSetCallStackCaptureEnabled != nil
342346
}
343-
_ = _CFErrorSetCallStackCaptureEnabled?(true)
344-
return _CFErrorSetCallStackCaptureEnabled != nil
345-
#else
346-
false
347347
#endif
348+
return false
348349
}()
349350

350351
/// The implementation of ``Backtrace/startCachingForThrownErrors()``, run
@@ -353,11 +354,7 @@ extension Backtrace {
353354
/// This value is named oddly so that it shows up clearly in symbolicated
354355
/// backtraces.
355356
private static let __SWIFT_TESTING_IS_CAPTURING_A_BACKTRACE_FOR_A_THROWN_ERROR__: Void = {
356-
#if SWT_TARGET_OS_APPLE && !SWT_NO_DYNAMIC_LINKING
357-
if Environment.flag(named: "SWT_FOUNDATION_ERROR_BACKTRACING_ENABLED") != false {
358-
_ = isFoundationCaptureEnabled
359-
}
360-
#endif
357+
_ = isFoundationCaptureEnabled
361358

362359
if Environment.flag(named: "SWT_SWIFT_ERROR_BACKTRACING_ENABLED") != false {
363360
_oldWillThrowHandler.withLock { oldWillThrowHandler in

0 commit comments

Comments
 (0)