Skip to content
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

[DO NOT MERGE] Remove legacy test content discovery. #1013

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
17 changes: 2 additions & 15 deletions Documentation/Porting.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ to load that information:
+ let resourceName: Str255 = switch kind {
+ case .testContent:
+ "__swift5_tests"
+#if !SWT_NO_LEGACY_TEST_DISCOVERY
+ case .typeMetadata:
+ "__swift5_types"
+#endif
+ }
+
+ let oldRefNum = CurResFile()
Expand Down Expand Up @@ -214,9 +210,8 @@ start with `"SWT_"`).
If your platform does not support dynamic linking and loading, you will need to
use static linkage instead. Define the `"SWT_NO_DYNAMIC_LINKING"` compiler
conditional for your platform in both `Package.swift` and
`CompilerSettings.cmake`, then define the symbols `_testContentSectionBegin`,
`_testContentSectionEnd`, `_typeMetadataSectionBegin`, and
`_typeMetadataSectionEnd` in `SectionBounds.swift`:
`CompilerSettings.cmake`, then define the symbols `_testContentSectionBegin` and
`_testContentSectionEnd` in `SectionBounds.swift`:

```diff
--- a/Sources/_TestDiscovery/SectionBounds.swift
Expand All @@ -225,18 +220,10 @@ conditional for your platform in both `Package.swift` and
+#elseif os(Classic)
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _testContentSectionBegin: _SectionBound
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _testContentSectionEnd: _SectionBound
+#if !SWT_NO_LEGACY_TEST_DISCOVERY
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _typeMetadataSectionBegin: _SectionBound
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _typeMetadataSectionEnd: _SectionBound
+#endif
#else
#warning("Platform-specific implementation missing: Runtime test discovery unavailable (static)")
private nonisolated(unsafe) let _testContentSectionBegin = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 16)
private nonisolated(unsafe) let _testContentSectionEnd = _testContentSectionBegin
#if !SWT_NO_LEGACY_TEST_DISCOVERY
private nonisolated(unsafe) let _typeMetadataSectionBegin = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 16)
private nonisolated(unsafe) let _typeMetadataSectionEnd = _typeMetadataSectionBegin
#endif
#endif
// ...
```
Expand Down
1 change: 0 additions & 1 deletion Sources/Testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ add_library(Testing
Test.ID.swift
Test.swift
Test+Discovery.swift
Test+Discovery+Legacy.swift
Test+Macro.swift
Traits/Bug.swift
Traits/Comment.swift
Expand Down
9 changes: 0 additions & 9 deletions Sources/Testing/ExitTests/ExitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,6 @@ extension ExitTest {
}
}

#if !SWT_NO_LEGACY_TEST_DISCOVERY
// Call the legacy lookup function that discovers tests embedded in types.
for record in Self.allTypeMetadataBasedTestContentRecords() {
if let exitTest = record.load(withHint: id) {
return exitTest
}
}
#endif

return nil
}
}
Expand Down
47 changes: 0 additions & 47 deletions Sources/Testing/Test+Discovery+Legacy.swift

This file was deleted.

42 changes: 5 additions & 37 deletions Sources/Testing/Test+Discovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,15 @@ extension Test {
// defective test records.)
var result = Set<Self>()

// Figure out which discovery mechanism to use. By default, we'll use both
// the legacy and new mechanisms, but we can set an environment variable
// to explicitly select one or the other. When we remove legacy support,
// we can also remove this enumeration and environment variable check.
#if !SWT_NO_LEGACY_TEST_DISCOVERY
let (useNewMode, useLegacyMode) = switch Environment.flag(named: "SWT_USE_LEGACY_TEST_DISCOVERY") {
case .none:
(true, true)
case .some(true):
(false, true)
case .some(false):
(true, false)
}
#else
let useNewMode = true
#endif

// Walk all test content and gather generator functions, then call them in
// a task group and collate their results.
if useNewMode {
let generators = Generator.allTestContentRecords().lazy.compactMap { $0.load() }
await withTaskGroup(of: Self.self) { taskGroup in
for generator in generators {
taskGroup.addTask { await generator.rawValue() }
}
result = await taskGroup.reduce(into: result) { $0.insert($1) }
}
}

#if !SWT_NO_LEGACY_TEST_DISCOVERY
// Perform legacy test discovery if needed.
if useLegacyMode && result.isEmpty {
let generators = Generator.allTypeMetadataBasedTestContentRecords().lazy.compactMap { $0.load() }
await withTaskGroup(of: Self.self) { taskGroup in
for generator in generators {
taskGroup.addTask { await generator.rawValue() }
}
result = await taskGroup.reduce(into: result) { $0.insert($1) }
let generators = Generator.allTestContentRecords().lazy.compactMap { $0.load() }
await withTaskGroup(of: Self.self) { taskGroup in
for generator in generators {
taskGroup.addTask { await generator.rawValue() }
}
result = await taskGroup.reduce(into: result) { $0.insert($1) }
}
#endif

return result
}
Expand Down
19 changes: 2 additions & 17 deletions Sources/TestingMacros/ConditionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public import SwiftSyntax
import SwiftSyntaxBuilder
public import SwiftSyntaxMacros

#if !hasFeature(SymbolLinkageMarkers) && SWT_NO_LEGACY_TEST_DISCOVERY
#error("Platform-specific misconfiguration: either SymbolLinkageMarkers or legacy test discovery is required to expand #expect(exitsWith:)")
#if !hasFeature(SymbolLinkageMarkers)
#error("Platform-specific misconfiguration: SymbolLinkageMarkers is required to expand #expect(exitsWith:)")
#endif

/// A protocol containing the common implementation for the expansions of the
Expand Down Expand Up @@ -467,19 +467,6 @@ extension ExitTestConditionMacro {
accessingWith: .identifier("accessor")
)

// Create another local type for legacy test discovery.
var recordDecl: DeclSyntax?
#if !SWT_NO_LEGACY_TEST_DISCOVERY
let legacyEnumName = context.makeUniqueName("__🟡$")
recordDecl = """
enum \(legacyEnumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(enumName).testContentRecord
}
}
"""
#endif

decls.append(
"""
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
Expand All @@ -495,8 +482,6 @@ extension ExitTestConditionMacro {
}

\(testContentRecordDecl)

\(recordDecl)
}
"""
)
Expand Down
19 changes: 2 additions & 17 deletions Sources/TestingMacros/SuiteDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public import SwiftSyntax
import SwiftSyntaxBuilder
public import SwiftSyntaxMacros

#if !hasFeature(SymbolLinkageMarkers) && SWT_NO_LEGACY_TEST_DISCOVERY
#error("Platform-specific misconfiguration: either SymbolLinkageMarkers or legacy test discovery is required to expand @Suite")
#if !hasFeature(SymbolLinkageMarkers)
#error("Platform-specific misconfiguration: SymbolLinkageMarkers is required to expand @Suite")
#endif

/// A type describing the expansion of the `@Suite` attribute macro.
Expand Down Expand Up @@ -166,21 +166,6 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
)
)

#if !SWT_NO_LEGACY_TEST_DISCOVERY
// Emit a type that contains a reference to the test content record.
let enumName = context.makeUniqueName("__🟡$")
result.append(
"""
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(testContentRecordName)
}
}
"""
)
#endif

return result
}
}
19 changes: 2 additions & 17 deletions Sources/TestingMacros/TestDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public import SwiftSyntax
import SwiftSyntaxBuilder
public import SwiftSyntaxMacros

#if !hasFeature(SymbolLinkageMarkers) && SWT_NO_LEGACY_TEST_DISCOVERY
#error("Platform-specific misconfiguration: either SymbolLinkageMarkers or legacy test discovery is required to expand @Test")
#if !hasFeature(SymbolLinkageMarkers)
#error("Platform-specific misconfiguration: SymbolLinkageMarkers is required to expand @Test")
#endif

/// A type describing the expansion of the `@Test` attribute macro.
Expand Down Expand Up @@ -491,21 +491,6 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
)
)

#if !SWT_NO_LEGACY_TEST_DISCOVERY
// Emit a type that contains a reference to the test content record.
let enumName = context.makeUniqueName(thunking: functionDecl, withPrefix: "__🟡$")
result.append(
"""
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(testContentRecordName)
}
}
"""
)
#endif

return result
}
}
13 changes: 0 additions & 13 deletions Sources/_TestDiscovery/DiscoverableAsTestContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,4 @@ public protocol DiscoverableAsTestContent: Sendable, ~Copyable {
/// By default, this type equals `Never`, indicating that this type of test
/// content does not support hinting during discovery.
associatedtype TestContentAccessorHint = Never

#if !SWT_NO_LEGACY_TEST_DISCOVERY
/// A string present in the names of types containing test content records
/// associated with this type.
@available(swift, deprecated: 100000.0, message: "Do not adopt this functionality in new code. It will be removed in a future release.")
static var _testContentTypeNameHint: String { get }
#endif
}

extension DiscoverableAsTestContent where Self: ~Copyable {
public static var _testContentTypeNameHint: String {
"__🟡$"
}
}
33 changes: 0 additions & 33 deletions Sources/_TestDiscovery/SectionBounds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ struct SectionBounds: Sendable {
enum Kind: Equatable, Hashable, CaseIterable {
/// The test content metadata section.
case testContent

#if !SWT_NO_LEGACY_TEST_DISCOVERY
/// The type metadata section.
case typeMetadata
#endif
}

/// All section bounds of the given kind found in the current process.
Expand Down Expand Up @@ -61,10 +56,6 @@ extension SectionBounds.Kind {
switch self {
case .testContent:
("__DATA_CONST", "__swift5_tests")
#if !SWT_NO_LEGACY_TEST_DISCOVERY
case .typeMetadata:
("__TEXT", "__swift5_types")
#endif
}
}
}
Expand Down Expand Up @@ -189,10 +180,6 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> [SectionBounds] {
let range = switch context.pointee.kind {
case .testContent:
sections.swift5_tests
#if !SWT_NO_LEGACY_TEST_DISCOVERY
case .typeMetadata:
sections.swift5_type_metadata
#endif
}
let start = UnsafeRawPointer(bitPattern: range.start)
let size = Int(clamping: range.length)
Expand Down Expand Up @@ -284,10 +271,6 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<Section
let sectionName = switch kind {
case .testContent:
".sw5test"
#if !SWT_NO_LEGACY_TEST_DISCOVERY
case .typeMetadata:
".sw5tymd"
#endif
}
return HMODULE.all.lazy.compactMap { _findSection(named: sectionName, in: $0) }
}
Expand Down Expand Up @@ -335,25 +318,13 @@ private struct _SectionBound: Sendable, ~Copyable {
#if SWT_TARGET_OS_APPLE
@_silgen_name(raw: "section$start$__DATA_CONST$__swift5_tests") private nonisolated(unsafe) var _testContentSectionBegin: _SectionBound
@_silgen_name(raw: "section$end$__DATA_CONST$__swift5_tests") private nonisolated(unsafe) var _testContentSectionEnd: _SectionBound
#if !SWT_NO_LEGACY_TEST_DISCOVERY
@_silgen_name(raw: "section$start$__TEXT$__swift5_types") private nonisolated(unsafe) var _typeMetadataSectionBegin: _SectionBound
@_silgen_name(raw: "section$end$__TEXT$__swift5_types") private nonisolated(unsafe) var _typeMetadataSectionEnd: _SectionBound
#endif
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || os(WASI)
@_silgen_name(raw: "__start_swift5_tests") private nonisolated(unsafe) var _testContentSectionBegin: _SectionBound
@_silgen_name(raw: "__stop_swift5_tests") private nonisolated(unsafe) var _testContentSectionEnd: _SectionBound
#if !SWT_NO_LEGACY_TEST_DISCOVERY
@_silgen_name(raw: "__start_swift5_type_metadata") private nonisolated(unsafe) var _typeMetadataSectionBegin: _SectionBound
@_silgen_name(raw: "__stop_swift5_type_metadata") private nonisolated(unsafe) var _typeMetadataSectionEnd: _SectionBound
#endif
#else
#warning("Platform-specific implementation missing: Runtime test discovery unavailable (static)")
private nonisolated(unsafe) let _testContentSectionBegin = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 16)
private nonisolated(unsafe) let _testContentSectionEnd = _testContentSectionBegin
#if !SWT_NO_LEGACY_TEST_DISCOVERY
private nonisolated(unsafe) let _typeMetadataSectionBegin = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 16)
private nonisolated(unsafe) let _typeMetadataSectionEnd = _typeMetadataSectionBegin
#endif
#endif

/// The common implementation of ``SectionBounds/all(_:)`` for platforms that do
Expand All @@ -368,10 +339,6 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> CollectionOfOne<Secti
let range = switch kind {
case .testContent:
_testContentSectionBegin ..< _testContentSectionEnd
#if !SWT_NO_LEGACY_TEST_DISCOVERY
case .typeMetadata:
_typeMetadataSectionBegin ..< _typeMetadataSectionEnd
#endif
}
let buffer = UnsafeRawBufferPointer(start: range.lowerBound, count: range.count)
let sb = SectionBounds(imageAddress: nil, buffer: buffer)
Expand Down
Loading