Skip to content

Commit 52d997b

Browse files
committed
Remove legacy test content discovery.
This PR removes the legacy test content discovery mechanism that is based on finding protocol conformances in the type metadata section emitted by the Swift compiler.
1 parent 65ca016 commit 52d997b

16 files changed

+27
-474
lines changed

Diff for: Documentation/Porting.md

+2-15
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ to load that information:
145145
+ let resourceName: Str255 = switch kind {
146146
+ case .testContent:
147147
+ "__swift5_tests"
148-
+#if !SWT_NO_LEGACY_TEST_DISCOVERY
149-
+ case .typeMetadata:
150-
+ "__swift5_types"
151-
+#endif
152148
+ }
153149
+
154150
+ let oldRefNum = CurResFile()
@@ -214,9 +210,8 @@ start with `"SWT_"`).
214210
If your platform does not support dynamic linking and loading, you will need to
215211
use static linkage instead. Define the `"SWT_NO_DYNAMIC_LINKING"` compiler
216212
conditional for your platform in both `Package.swift` and
217-
`CompilerSettings.cmake`, then define the symbols `_testContentSectionBegin`,
218-
`_testContentSectionEnd`, `_typeMetadataSectionBegin`, and
219-
`_typeMetadataSectionEnd` in `SectionBounds.swift`:
213+
`CompilerSettings.cmake`, then define the symbols `_testContentSectionBegin` and
214+
`_testContentSectionEnd` in `SectionBounds.swift`:
220215

221216
```diff
222217
--- a/Sources/_TestDiscovery/SectionBounds.swift
@@ -225,18 +220,10 @@ conditional for your platform in both `Package.swift` and
225220
+#elseif os(Classic)
226221
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _testContentSectionBegin: _SectionBound
227222
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _testContentSectionEnd: _SectionBound
228-
+#if !SWT_NO_LEGACY_TEST_DISCOVERY
229-
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _typeMetadataSectionBegin: _SectionBound
230-
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _typeMetadataSectionEnd: _SectionBound
231-
+#endif
232223
#else
233224
#warning("Platform-specific implementation missing: Runtime test discovery unavailable (static)")
234225
private nonisolated(unsafe) let _testContentSectionBegin = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 16)
235226
private nonisolated(unsafe) let _testContentSectionEnd = _testContentSectionBegin
236-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
237-
private nonisolated(unsafe) let _typeMetadataSectionBegin = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 16)
238-
private nonisolated(unsafe) let _typeMetadataSectionEnd = _typeMetadataSectionBegin
239-
#endif
240227
#endif
241228
// ...
242229
```

Diff for: Sources/Testing/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ add_library(Testing
8888
Test.ID.swift
8989
Test.swift
9090
Test+Discovery.swift
91-
Test+Discovery+Legacy.swift
9291
Test+Macro.swift
9392
Traits/Bug.swift
9493
Traits/Comment.swift

Diff for: Sources/Testing/ExitTests/ExitTest.swift

-9
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,6 @@ extension ExitTest {
296296
}
297297
}
298298

299-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
300-
// Call the legacy lookup function that discovers tests embedded in types.
301-
for record in Self.allTypeMetadataBasedTestContentRecords() {
302-
if let exitTest = record.load(withHint: id) {
303-
return exitTest
304-
}
305-
}
306-
#endif
307-
308299
return nil
309300
}
310301
}

Diff for: Sources/Testing/Test+Discovery+Legacy.swift

-47
This file was deleted.

Diff for: Sources/Testing/Test+Discovery.swift

+5-37
Original file line numberDiff line numberDiff line change
@@ -61,47 +61,15 @@ extension Test {
6161
// defective test records.)
6262
var result = Set<Self>()
6363

64-
// Figure out which discovery mechanism to use. By default, we'll use both
65-
// the legacy and new mechanisms, but we can set an environment variable
66-
// to explicitly select one or the other. When we remove legacy support,
67-
// we can also remove this enumeration and environment variable check.
68-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
69-
let (useNewMode, useLegacyMode) = switch Environment.flag(named: "SWT_USE_LEGACY_TEST_DISCOVERY") {
70-
case .none:
71-
(true, true)
72-
case .some(true):
73-
(false, true)
74-
case .some(false):
75-
(true, false)
76-
}
77-
#else
78-
let useNewMode = true
79-
#endif
80-
8164
// Walk all test content and gather generator functions, then call them in
8265
// a task group and collate their results.
83-
if useNewMode {
84-
let generators = Generator.allTestContentRecords().lazy.compactMap { $0.load() }
85-
await withTaskGroup(of: Self.self) { taskGroup in
86-
for generator in generators {
87-
taskGroup.addTask { await generator.rawValue() }
88-
}
89-
result = await taskGroup.reduce(into: result) { $0.insert($1) }
90-
}
91-
}
92-
93-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
94-
// Perform legacy test discovery if needed.
95-
if useLegacyMode && result.isEmpty {
96-
let generators = Generator.allTypeMetadataBasedTestContentRecords().lazy.compactMap { $0.load() }
97-
await withTaskGroup(of: Self.self) { taskGroup in
98-
for generator in generators {
99-
taskGroup.addTask { await generator.rawValue() }
100-
}
101-
result = await taskGroup.reduce(into: result) { $0.insert($1) }
66+
let generators = Generator.allTestContentRecords().lazy.compactMap { $0.load() }
67+
await withTaskGroup(of: Self.self) { taskGroup in
68+
for generator in generators {
69+
taskGroup.addTask { await generator.rawValue() }
10270
}
71+
result = await taskGroup.reduce(into: result) { $0.insert($1) }
10372
}
104-
#endif
10573

10674
return result
10775
}

Diff for: Sources/TestingMacros/ConditionMacro.swift

+2-17
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public import SwiftSyntax
1212
import SwiftSyntaxBuilder
1313
public import SwiftSyntaxMacros
1414

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

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

470-
// Create another local type for legacy test discovery.
471-
var recordDecl: DeclSyntax?
472-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
473-
let legacyEnumName = context.makeUniqueName("__🟡$")
474-
recordDecl = """
475-
enum \(legacyEnumName): Testing.__TestContentRecordContainer {
476-
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
477-
\(enumName).testContentRecord
478-
}
479-
}
480-
"""
481-
#endif
482-
483470
decls.append(
484471
"""
485472
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
@@ -495,8 +482,6 @@ extension ExitTestConditionMacro {
495482
}
496483
497484
\(testContentRecordDecl)
498-
499-
\(recordDecl)
500485
}
501486
"""
502487
)

Diff for: Sources/TestingMacros/SuiteDeclarationMacro.swift

+2-17
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public import SwiftSyntax
1313
import SwiftSyntaxBuilder
1414
public import SwiftSyntaxMacros
1515

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

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

169-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
170-
// Emit a type that contains a reference to the test content record.
171-
let enumName = context.makeUniqueName("__🟡$")
172-
result.append(
173-
"""
174-
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
175-
enum \(enumName): Testing.__TestContentRecordContainer {
176-
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
177-
\(testContentRecordName)
178-
}
179-
}
180-
"""
181-
)
182-
#endif
183-
184169
return result
185170
}
186171
}

Diff for: Sources/TestingMacros/TestDeclarationMacro.swift

+2-17
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public import SwiftSyntax
1313
import SwiftSyntaxBuilder
1414
public import SwiftSyntaxMacros
1515

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

2020
/// A type describing the expansion of the `@Test` attribute macro.
@@ -491,21 +491,6 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
491491
)
492492
)
493493

494-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
495-
// Emit a type that contains a reference to the test content record.
496-
let enumName = context.makeUniqueName(thunking: functionDecl, withPrefix: "__🟡$")
497-
result.append(
498-
"""
499-
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
500-
enum \(enumName): Testing.__TestContentRecordContainer {
501-
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
502-
\(testContentRecordName)
503-
}
504-
}
505-
"""
506-
)
507-
#endif
508-
509494
return result
510495
}
511496
}

Diff for: Sources/_TestDiscovery/DiscoverableAsTestContent.swift

-13
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,4 @@ public protocol DiscoverableAsTestContent: Sendable, ~Copyable {
3939
/// By default, this type equals `Never`, indicating that this type of test
4040
/// content does not support hinting during discovery.
4141
associatedtype TestContentAccessorHint = Never
42-
43-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
44-
/// A string present in the names of types containing test content records
45-
/// associated with this type.
46-
@available(swift, deprecated: 100000.0, message: "Do not adopt this functionality in new code. It will be removed in a future release.")
47-
static var _testContentTypeNameHint: String { get }
48-
#endif
49-
}
50-
51-
extension DiscoverableAsTestContent where Self: ~Copyable {
52-
public static var _testContentTypeNameHint: String {
53-
"__🟡$"
54-
}
5542
}

Diff for: Sources/_TestDiscovery/SectionBounds.swift

-33
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ struct SectionBounds: Sendable {
2626
enum Kind: Equatable, Hashable, CaseIterable {
2727
/// The test content metadata section.
2828
case testContent
29-
30-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
31-
/// The type metadata section.
32-
case typeMetadata
33-
#endif
3429
}
3530

3631
/// All section bounds of the given kind found in the current process.
@@ -61,10 +56,6 @@ extension SectionBounds.Kind {
6156
switch self {
6257
case .testContent:
6358
("__DATA_CONST", "__swift5_tests")
64-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
65-
case .typeMetadata:
66-
("__TEXT", "__swift5_types")
67-
#endif
6859
}
6960
}
7061
}
@@ -189,10 +180,6 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> [SectionBounds] {
189180
let range = switch context.pointee.kind {
190181
case .testContent:
191182
sections.swift5_tests
192-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
193-
case .typeMetadata:
194-
sections.swift5_type_metadata
195-
#endif
196183
}
197184
let start = UnsafeRawPointer(bitPattern: range.start)
198185
let size = Int(clamping: range.length)
@@ -284,10 +271,6 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<Section
284271
let sectionName = switch kind {
285272
case .testContent:
286273
".sw5test"
287-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
288-
case .typeMetadata:
289-
".sw5tymd"
290-
#endif
291274
}
292275
return HMODULE.all.lazy.compactMap { _findSection(named: sectionName, in: $0) }
293276
}
@@ -335,25 +318,13 @@ private struct _SectionBound: Sendable, ~Copyable {
335318
#if SWT_TARGET_OS_APPLE
336319
@_silgen_name(raw: "section$start$__DATA_CONST$__swift5_tests") private nonisolated(unsafe) var _testContentSectionBegin: _SectionBound
337320
@_silgen_name(raw: "section$end$__DATA_CONST$__swift5_tests") private nonisolated(unsafe) var _testContentSectionEnd: _SectionBound
338-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
339-
@_silgen_name(raw: "section$start$__TEXT$__swift5_types") private nonisolated(unsafe) var _typeMetadataSectionBegin: _SectionBound
340-
@_silgen_name(raw: "section$end$__TEXT$__swift5_types") private nonisolated(unsafe) var _typeMetadataSectionEnd: _SectionBound
341-
#endif
342321
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || os(WASI)
343322
@_silgen_name(raw: "__start_swift5_tests") private nonisolated(unsafe) var _testContentSectionBegin: _SectionBound
344323
@_silgen_name(raw: "__stop_swift5_tests") private nonisolated(unsafe) var _testContentSectionEnd: _SectionBound
345-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
346-
@_silgen_name(raw: "__start_swift5_type_metadata") private nonisolated(unsafe) var _typeMetadataSectionBegin: _SectionBound
347-
@_silgen_name(raw: "__stop_swift5_type_metadata") private nonisolated(unsafe) var _typeMetadataSectionEnd: _SectionBound
348-
#endif
349324
#else
350325
#warning("Platform-specific implementation missing: Runtime test discovery unavailable (static)")
351326
private nonisolated(unsafe) let _testContentSectionBegin = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 16)
352327
private nonisolated(unsafe) let _testContentSectionEnd = _testContentSectionBegin
353-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
354-
private nonisolated(unsafe) let _typeMetadataSectionBegin = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 16)
355-
private nonisolated(unsafe) let _typeMetadataSectionEnd = _typeMetadataSectionBegin
356-
#endif
357328
#endif
358329

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

0 commit comments

Comments
 (0)