From b30ac487e0d6122f4a12d3e4980b06e2d1d5e323 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Sat, 31 Oct 2026 16:09:14 -0400 Subject: [PATCH] Require `TestContentContext` conform to `ConvertibleFromBytes`. The associated type `TestContentContext` in the `_TestDiscovery` module is constrained to `BitwiseCopyable`. Swift recently introduced `ConvertibleFromBytes` which refines `BitwiseCopyable` and guarantees that conforming types have no uninhabited members (in other words, all bit patterns are valid instances). This PR changes the constraint on `TestContentContext` to `ConvertibleFromBytes`. This improves type safety somewhat by ensuring that a library using `_TestDiscovery` cannot specify a type here that has a trap representation (for instance, `UnsafeRawPointer` and `0x0`) which Swift Testing cannot guard against. --- Documentation/ABI/TestContent.md | 4 ++-- Sources/_TestDiscovery/DiscoverableAsTestContent.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Documentation/ABI/TestContent.md b/Documentation/ABI/TestContent.md index cd12b2872..370566eb3 100644 --- a/Documentation/ABI/TestContent.md +++ b/Documentation/ABI/TestContent.md @@ -252,14 +252,14 @@ a custom `hint` type for your accessor functions, you can set extension FoodTruckDiagnostic: DiscoverableAsTestContent { static var testContentKind: TestContentKind { /* Your `kind` value here. */ } - typealias TestContentContext = UnsafePointer + typealias TestContentContext = UnsafePointer? typealias TestContentAccessorHint = String } ``` If you customize `TestContentContext`, be aware that the type you specify must have the same stride as `UInt` and must have an alignment less than or equal to -that of `UInt`. +that of `UInt`. The type must also conform to [`ConvertibleFromBytes`](https://developer.apple.com/documentation/swift/convertiblefrombytes). When you are done configuring your type's protocol conformance, you can then enumerate all test content records matching it as instances of diff --git a/Sources/_TestDiscovery/DiscoverableAsTestContent.swift b/Sources/_TestDiscovery/DiscoverableAsTestContent.swift index 0484dd8af..565baef0d 100644 --- a/Sources/_TestDiscovery/DiscoverableAsTestContent.swift +++ b/Sources/_TestDiscovery/DiscoverableAsTestContent.swift @@ -31,7 +31,7 @@ public protocol DiscoverableAsTestContent: Sendable { /// type with the same stride and alignment as `UInt`. Using a type with /// different stride or alignment will result in a failure when trying to /// discover test content records associated with this type. - associatedtype TestContentContext: BitwiseCopyable = UInt + associatedtype TestContentContext: ConvertibleFromBytes = UInt /// A type of "hint" passed to ``allTestContentRecords()`` to help the testing /// library find the correct result.