From e1004130adf0428a63c9c249e5679fc30bb4e607 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Thu, 25 Sep 2025 11:53:15 -0400 Subject: [PATCH] Disable backtrace capturing in Embedded Swift. This PR disables the bulk of the backtrace-capturing-on-error logic when building for Embedded Swift. At least for the moment, we can't deal with error existentials in Embedded Swift, so this code won't function correctly even if it successfully compiles. --- .../Testing/SourceAttribution/Backtrace.swift | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sources/Testing/SourceAttribution/Backtrace.swift b/Sources/Testing/SourceAttribution/Backtrace.swift index 97815755e..60d08fede 100644 --- a/Sources/Testing/SourceAttribution/Backtrace.swift +++ b/Sources/Testing/SourceAttribution/Backtrace.swift @@ -119,6 +119,7 @@ extension Backtrace: Codable { // MARK: - Backtraces for thrown errors extension Backtrace { +#if !hasFeature(Embedded) // MARK: - Error cache keys /// A type used as a cache key that uniquely identifies error existential @@ -321,6 +322,7 @@ extension Backtrace { } forward(errorType) } +#endif /// Whether or not Foundation provides a function that triggers the capture of /// backtaces when instances of `NSError` or `CFError` are created. @@ -336,7 +338,7 @@ extension Backtrace { /// - Note: The underlying Foundation function is called (if present) the /// first time the value of this property is read. static let isFoundationCaptureEnabled = { -#if _runtime(_ObjC) && !SWT_NO_DYNAMIC_LINKING +#if !hasFeature(Embedded) && _runtime(_ObjC) && !SWT_NO_DYNAMIC_LINKING if Environment.flag(named: "SWT_FOUNDATION_ERROR_BACKTRACING_ENABLED") == true { let _CFErrorSetCallStackCaptureEnabled = symbol(named: "_CFErrorSetCallStackCaptureEnabled").map { castCFunction(at: $0, to: (@convention(c) (DarwinBoolean) -> DarwinBoolean).self) @@ -348,6 +350,7 @@ extension Backtrace { return false }() +#if !hasFeature(Embedded) /// The implementation of ``Backtrace/startCachingForThrownErrors()``, run /// only once. /// @@ -373,6 +376,7 @@ extension Backtrace { } } }() +#endif /// Configure the Swift runtime to allow capturing backtraces when errors are /// thrown. @@ -381,7 +385,9 @@ extension Backtrace { /// developer-supplied code to ensure that thrown errors' backtraces are /// always captured. static func startCachingForThrownErrors() { +#if !hasFeature(Embedded) __SWIFT_TESTING_IS_CAPTURING_A_BACKTRACE_FOR_A_THROWN_ERROR__ +#endif } /// Flush stale entries from the error-mapping cache. @@ -389,9 +395,11 @@ extension Backtrace { /// Call this function periodically to ensure that errors do not continue to /// take up space in the cache after they have been deinitialized. static func flushThrownErrorCache() { +#if !hasFeature(Embedded) _errorMappingCache.withLock { cache in cache = cache.filter { $0.value.errorObject != nil } } +#endif } /// Initialize an instance of this type with the previously-cached backtrace @@ -411,6 +419,7 @@ extension Backtrace { /// initializer cannot be made an instance method or property of `Error` /// because doing so will cause Swift-native errors to be unboxed into /// existential containers with different addresses. +#if !hasFeature(Embedded) @inline(never) init?(forFirstThrowOf error: any Error, checkFoundation: Bool = true) { if checkFoundation && Self.isFoundationCaptureEnabled, @@ -430,4 +439,9 @@ extension Backtrace { return nil } } +#else + init?(forFirstThrowOf error: some Error, checkFoundation: Bool = true) { + return nil + } +#endif }