Skip to content

Commit 678ea6d

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 e107fa4 commit 678ea6d

17 files changed

+30
-531
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()
@@ -211,9 +207,8 @@ start with `"SWT_"`).
211207
If your platform does not support dynamic linking and loading, you will need to
212208
use static linkage instead. Define the `"SWT_NO_DYNAMIC_LINKING"` compiler
213209
conditional for your platform in both `Package.swift` and
214-
`CompilerSettings.cmake`, then define the symbols `_testContentSectionBegin`,
215-
`_testContentSectionEnd`, `_typeMetadataSectionBegin`, and
216-
`_typeMetadataSectionEnd` in `SectionBounds.swift`:
210+
`CompilerSettings.cmake`, then define the symbols `_testContentSectionBegin` and
211+
`_testContentSectionEnd` in `SectionBounds.swift`:
217212

218213
```diff
219214
--- a/Sources/_TestDiscovery/SectionBounds.swift
@@ -222,18 +217,10 @@ conditional for your platform in both `Package.swift` and
222217
+#elseif os(Classic)
223218
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _testContentSectionBegin: CChar
224219
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _testContentSectionEnd: CChar
225-
+#if !SWT_NO_LEGACY_TEST_DISCOVERY
226-
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _typeMetadataSectionBegin: CChar
227-
+@_silgen_name(raw: "...") private nonisolated(unsafe) var _typeMetadataSectionEnd: CChar
228-
+#endif
229220
#else
230221
#warning("Platform-specific implementation missing: Runtime test discovery unavailable (static)")
231222
private nonisolated(unsafe) var _testContentSectionBegin: Void
232223
private nonisolated(unsafe) var _testContentSectionEnd: Void
233-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
234-
private nonisolated(unsafe) var _typeMetadataSectionBegin: Void
235-
private nonisolated(unsafe) var _typeMetadataSectionEnd: Void
236-
#endif
237224
// ...
238225
```
239226

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
@@ -11,8 +11,8 @@
1111
public import SwiftSyntax
1212
public import SwiftSyntaxMacros
1313

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

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

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

Diff for: Sources/TestingMacros/SuiteDeclarationMacro.swift

+2-17
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
public import SwiftSyntax
1212
public import SwiftSyntaxMacros
1313

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

1818
/// A type describing the expansion of the `@Suite` attribute macro.
@@ -164,21 +164,6 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
164164
)
165165
)
166166

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

Diff for: Sources/TestingMacros/TestDeclarationMacro.swift

+2-17
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
public import SwiftSyntax
1212
public import SwiftSyntaxMacros
1313

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

1818
/// A type describing the expansion of the `@Test` attribute macro.
@@ -489,21 +489,6 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
489489
)
490490
)
491491

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

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

+2-35
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.
@@ -62,10 +57,6 @@ extension SectionBounds.Kind {
6257
switch self {
6358
case .testContent:
6459
("__DATA_CONST", "__swift5_tests")
65-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
66-
case .typeMetadata:
67-
("__TEXT", "__swift5_types")
68-
#endif
6960
}
7061
}
7162
}
@@ -190,10 +181,6 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<Section
190181
let range = switch context.pointee.kind {
191182
case .testContent:
192183
sections.swift5_tests
193-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
194-
case .typeMetadata:
195-
sections.swift5_type_metadata
196-
#endif
197184
}
198185
let start = UnsafeRawPointer(bitPattern: range.start)
199186
let size = Int(clamping: range.length)
@@ -282,10 +269,6 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<Section
282269
let sectionName = switch kind {
283270
case .testContent:
284271
".sw5test"
285-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
286-
case .typeMetadata:
287-
".sw5tymd"
288-
#endif
289272
}
290273
return HMODULE.all.lazy.compactMap { _findSection(named: sectionName, in: $0) }
291274
}
@@ -315,25 +298,13 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<Section
315298
#if SWT_TARGET_OS_APPLE
316299
@_silgen_name(raw: "section$start$__DATA_CONST$__swift5_tests") private nonisolated(unsafe) var _testContentSectionBegin: CChar
317300
@_silgen_name(raw: "section$end$__DATA_CONST$__swift5_tests") private nonisolated(unsafe) var _testContentSectionEnd: CChar
318-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
319-
@_silgen_name(raw: "section$start$__TEXT$__swift5_types") private nonisolated(unsafe) var _typeMetadataSectionBegin: CChar
320-
@_silgen_name(raw: "section$end$__TEXT$__swift5_types") private nonisolated(unsafe) var _typeMetadataSectionEnd: CChar
321-
#endif
322301
#elseif os(WASI)
323302
@_silgen_name(raw: "__start_swift5_tests") private nonisolated(unsafe) var _testContentSectionBegin: CChar
324303
@_silgen_name(raw: "__stop_swift5_tests") private nonisolated(unsafe) var _testContentSectionEnd: CChar
325-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
326-
@_silgen_name(raw: "__start_swift5_type_metadata") private nonisolated(unsafe) var _typeMetadataSectionBegin: CChar
327-
@_silgen_name(raw: "__stop_swift5_type_metadata") private nonisolated(unsafe) var _typeMetadataSectionEnd: CChar
328-
#endif
329304
#else
330305
#warning("Platform-specific implementation missing: Runtime test discovery unavailable (static)")
331306
private nonisolated(unsafe) var _testContentSectionBegin: Void
332307
private nonisolated(unsafe) var _testContentSectionEnd: Void
333-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
334-
private nonisolated(unsafe) var _typeMetadataSectionBegin: Void
335-
private nonisolated(unsafe) var _typeMetadataSectionEnd: Void
336-
#endif
337308

338309
extension UnsafeRawBufferPointer {
339310
/// Construct an empty buffer.
@@ -360,16 +331,12 @@ extension UnsafeRawBufferPointer {
360331
/// - Parameters:
361332
/// - kind: Which kind of metadata section to return.
362333
///
363-
/// - Returns: A structure describing the bounds of the type metadata section
364-
/// contained in the same image as the testing library itself.
334+
/// - Returns: A structure describing the bounds of the given section contained
335+
/// in the same image as the testing library itself.
365336
private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<SectionBounds> {
366337
let buffer = switch kind {
367338
case .testContent:
368339
UnsafeRawBufferPointer(sectionBegin: &_testContentSectionBegin, sectionEnd: &_testContentSectionEnd)
369-
#if !SWT_NO_LEGACY_TEST_DISCOVERY
370-
case .typeMetadata:
371-
UnsafeRawBufferPointer(sectionBegin: &_typeMetadataSectionBegin, sectionEnd: &_typeMetadataSectionEnd)
372-
#endif
373340
}
374341
let sb = SectionBounds(imageAddress: nil, buffer: buffer)
375342
return CollectionOfOne(sb)

0 commit comments

Comments
 (0)