Skip to content

Have DiscoverableAsTestContent enumeration produce some Sequence instead of AnySequence. #1122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Testing/Test+Discovery+Legacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public protocol __TestContentRecordContainer {
nonisolated static var __testContentRecord: __TestContentRecord { get }
}

extension DiscoverableAsTestContent where Self: ~Copyable {
extension DiscoverableAsTestContent {
/// Get all test content of this type known to Swift and found in the current
/// process using the legacy discovery mechanism.
///
/// - Returns: A sequence of instances of ``TestContentRecord``. Only test
/// content records matching this ``TestContent`` type's requirements are
/// included in the sequence.
static func allTypeMetadataBasedTestContentRecords() -> AnySequence<TestContentRecord<Self>> {
static func allTypeMetadataBasedTestContentRecords() -> some Sequence<TestContentRecord<Self>> {
return allTypeMetadataBasedTestContentRecords { type, buffer in
guard let type = type as? any __TestContentRecordContainer.Type else {
return false
Expand Down
4 changes: 2 additions & 2 deletions Sources/_TestDiscovery/DiscoverableAsTestContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// because they may be discovered within any isolation context or within
/// multiple isolation contexts running concurrently.
@_spi(Experimental) @_spi(ForToolsIntegrationOnly)
public protocol DiscoverableAsTestContent: Sendable, ~Copyable {
public protocol DiscoverableAsTestContent: Sendable {
/// The value of the `kind` field in test content records associated with this
/// type.
///
Expand Down Expand Up @@ -49,7 +49,7 @@ public protocol DiscoverableAsTestContent: Sendable, ~Copyable {
}

#if !SWT_NO_LEGACY_TEST_DISCOVERY
extension DiscoverableAsTestContent where Self: ~Copyable {
extension DiscoverableAsTestContent {
public static var _testContentTypeNameHint: String {
"__🟡$"
}
Expand Down
28 changes: 8 additions & 20 deletions Sources/_TestDiscovery/TestContentRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private typealias _TestContentRecord = (
reserved2: UInt
)

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

// MARK: - Enumeration of test content records

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

let kind = testContentKind.rawValue

let result = SectionBounds.all(.testContent).lazy.flatMap { sb in
return SectionBounds.all(.testContent).lazy.flatMap { sb in
sb.buffer.withMemoryRebound(to: _TestContentRecord.self) { records in
(0 ..< records.count).lazy
.map { (records.baseAddress! + $0) as UnsafePointer<_TestContentRecord> }
.filter { $0.pointee.kind == kind }
.map { TestContentRecord<Self>(imageAddress: sb.imageAddress, recordAddress: $0) }
}
}
return AnySequence(result)
}
}

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

private import _TestingInternals

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

let typeNameHint = _testContentTypeNameHint
Expand All @@ -300,7 +289,7 @@ extension DiscoverableAsTestContent where Self: ~Copyable {
}
}

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