Skip to content

Commit 61a01cb

Browse files
authored
Have DiscoverableAsTestContent enumeration produce some Sequence instead of AnySequence. (#1122)
This PR changes how `DiscoverableAsTestContent` enumeration works so that we can return `some Sequence` instead of `AnySequence`. We do so by removing the `~Copyable` constraint on the protocol, which subsequently causes the compiler to get confused and crash trying to represent `some Sequence<TestContentRecord<T>>` where `T: DiscoverableAsTestContent`. The only supported/allowed consumers of the `DiscoverableAsTestContent` protocol are Swift Testing and the experimental Playgrounds package, neither of which uses (or needs to use) a move-only type here. Earlier, `ExitTest` conformed to `DiscoverableAsTestContent`, but this was changed and is no longer necessary. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 6e462ad commit 61a01cb

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

Sources/Testing/Test+Discovery+Legacy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public protocol __TestContentRecordContainer {
2424
nonisolated static var __testContentRecord: __TestContentRecord { get }
2525
}
2626

27-
extension DiscoverableAsTestContent where Self: ~Copyable {
27+
extension DiscoverableAsTestContent {
2828
/// Get all test content of this type known to Swift and found in the current
2929
/// process using the legacy discovery mechanism.
3030
///
3131
/// - Returns: A sequence of instances of ``TestContentRecord``. Only test
3232
/// content records matching this ``TestContent`` type's requirements are
3333
/// included in the sequence.
34-
static func allTypeMetadataBasedTestContentRecords() -> AnySequence<TestContentRecord<Self>> {
34+
static func allTypeMetadataBasedTestContentRecords() -> some Sequence<TestContentRecord<Self>> {
3535
return allTypeMetadataBasedTestContentRecords { type, buffer in
3636
guard let type = type as? any __TestContentRecordContainer.Type else {
3737
return false

Sources/_TestDiscovery/DiscoverableAsTestContent.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// because they may be discovered within any isolation context or within
1717
/// multiple isolation contexts running concurrently.
1818
@_spi(Experimental) @_spi(ForToolsIntegrationOnly)
19-
public protocol DiscoverableAsTestContent: Sendable, ~Copyable {
19+
public protocol DiscoverableAsTestContent: Sendable {
2020
/// The value of the `kind` field in test content records associated with this
2121
/// type.
2222
///
@@ -49,7 +49,7 @@ public protocol DiscoverableAsTestContent: Sendable, ~Copyable {
4949
}
5050

5151
#if !SWT_NO_LEGACY_TEST_DISCOVERY
52-
extension DiscoverableAsTestContent where Self: ~Copyable {
52+
extension DiscoverableAsTestContent {
5353
public static var _testContentTypeNameHint: String {
5454
"__🟡$"
5555
}

Sources/_TestDiscovery/TestContentRecord.swift

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private typealias _TestContentRecord = (
4444
reserved2: UInt
4545
)
4646

47-
extension DiscoverableAsTestContent where Self: ~Copyable {
47+
extension DiscoverableAsTestContent {
4848
/// Check that the layout of this structure in memory matches its expected
4949
/// layout in the test content section.
5050
///
@@ -64,7 +64,7 @@ extension DiscoverableAsTestContent where Self: ~Copyable {
6464
/// ``DiscoverableAsTestContent/allTestContentRecords()`` on a type that
6565
/// conforms to ``DiscoverableAsTestContent``.
6666
@_spi(Experimental) @_spi(ForToolsIntegrationOnly)
67-
public struct TestContentRecord<T> where T: DiscoverableAsTestContent & ~Copyable {
67+
public struct TestContentRecord<T> where T: DiscoverableAsTestContent {
6868
/// The base address of the image containing this instance, if known.
6969
///
7070
/// The type of this pointer is platform-dependent:
@@ -229,32 +229,26 @@ extension TestContentRecord: CustomStringConvertible {
229229

230230
// MARK: - Enumeration of test content records
231231

232-
extension DiscoverableAsTestContent where Self: ~Copyable {
232+
extension DiscoverableAsTestContent {
233233
/// Get all test content of this type known to Swift and found in the current
234234
/// process.
235235
///
236236
/// - Returns: A sequence of instances of ``TestContentRecord``. Only test
237237
/// content records matching this ``TestContent`` type's requirements are
238238
/// included in the sequence.
239-
///
240-
/// @Comment {
241-
/// - Bug: This function returns an instance of `AnySequence` instead of an
242-
/// opaque type due to a compiler crash. ([143080508](rdar://143080508))
243-
/// }
244-
public static func allTestContentRecords() -> AnySequence<TestContentRecord<Self>> {
239+
public static func allTestContentRecords() -> some Sequence<TestContentRecord<Self>> {
245240
validateMemoryLayout()
246241

247242
let kind = testContentKind.rawValue
248243

249-
let result = SectionBounds.all(.testContent).lazy.flatMap { sb in
244+
return SectionBounds.all(.testContent).lazy.flatMap { sb in
250245
sb.buffer.withMemoryRebound(to: _TestContentRecord.self) { records in
251246
(0 ..< records.count).lazy
252247
.map { (records.baseAddress! + $0) as UnsafePointer<_TestContentRecord> }
253248
.filter { $0.pointee.kind == kind }
254249
.map { TestContentRecord<Self>(imageAddress: sb.imageAddress, recordAddress: $0) }
255250
}
256251
}
257-
return AnySequence(result)
258252
}
259253
}
260254

@@ -263,7 +257,7 @@ extension DiscoverableAsTestContent where Self: ~Copyable {
263257

264258
private import _TestingInternals
265259

266-
extension DiscoverableAsTestContent where Self: ~Copyable {
260+
extension DiscoverableAsTestContent {
267261
/// Get all test content of this type known to Swift and found in the current
268262
/// process using the legacy discovery mechanism.
269263
///
@@ -277,15 +271,10 @@ extension DiscoverableAsTestContent where Self: ~Copyable {
277271
/// - Returns: A sequence of instances of ``TestContentRecord``. Only test
278272
/// content records matching this ``TestContent`` type's requirements are
279273
/// included in the sequence.
280-
///
281-
/// @Comment {
282-
/// - Bug: This function returns an instance of `AnySequence` instead of an
283-
/// opaque type due to a compiler crash. ([143080508](rdar://143080508))
284-
/// }
285274
@available(swift, deprecated: 100000.0, message: "Do not adopt this functionality in new code. It will be removed in a future release.")
286275
public static func allTypeMetadataBasedTestContentRecords(
287276
loadingWith loader: @escaping @Sendable (Any.Type, UnsafeMutableRawBufferPointer) -> Bool
288-
) -> AnySequence<TestContentRecord<Self>> {
277+
) -> some Sequence<TestContentRecord<Self>> {
289278
validateMemoryLayout()
290279

291280
let typeNameHint = _testContentTypeNameHint
@@ -300,7 +289,7 @@ extension DiscoverableAsTestContent where Self: ~Copyable {
300289
}
301290
}
302291

303-
let result = SectionBounds.all(.typeMetadata).lazy.flatMap { sb in
292+
return SectionBounds.all(.typeMetadata).lazy.flatMap { sb in
304293
stride(from: 0, to: sb.buffer.count, by: SWTTypeMetadataRecordByteCount).lazy
305294
.map { sb.buffer.baseAddress! + $0 }
306295
.compactMap { swt_getType(fromTypeMetadataRecord: $0, ifNameContains: typeNameHint) }
@@ -309,7 +298,6 @@ extension DiscoverableAsTestContent where Self: ~Copyable {
309298
.filter { $0.kind == kind }
310299
.map { TestContentRecord<Self>(imageAddress: sb.imageAddress, record: $0) }
311300
}
312-
return AnySequence(result)
313301
}
314302
}
315303
#endif

0 commit comments

Comments
 (0)