Skip to content

Commit 060645b

Browse files
committed
Store test content in a custom metadata section.
This PR uses the experimental symbol linkage margers feature in the Swift compiler to emit metadata about tests (and exit tests) into a dedicated section of the test executable being built. At runtime, we discover that section and read out the tests from it. This has several benefits over our current model, which involves walking Swift's type metadata table looking for types that conform to a protocol: 1. We don't need to define that protocol as public API in Swift Testing, 1. We don't need to emit type metadata (much larger than what we really need) for every test function, 1. We don't need to duplicate a large chunk of the Swift ABI sources in order to walk the type metadata table correctly, and 1. Almost all the new code is written in Swift, whereas the code it is intended to replace could not be fully represented in Swift and needed to be written in C++. The change also opens up the possibility of supporting generic types in the future because we can emit metadata without needing to emit a nested type (which is not always valid in a generic context.) That's a "future direction" and not covered by this PR specifically. I've defined a layout for entries in the new `swift5_tests` section that should be flexible enough for us in the short-to-medium term and which lets us define additional arbitrary test content record types. The layout of this section is covered in depth in the new [TestContent.md](Documentation/ABI/TestContent.md) article. This functionality is only available if a test target enables the experimental `"SymbolLinkageMarkers"` feature. We continue to emit protocol-conforming types for now—that code will be removed if and when the experimental feature is properly supported (modulo us adopting relevant changes to the feature's API.) #735 swiftlang/swift#76698 swiftlang/swift#78411
1 parent e76a44f commit 060645b

21 files changed

+444
-87
lines changed

Documentation/ABI/TestContent.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ struct SWTTestContentRecord {
7575
};
7676
```
7777

78-
Do not use the `__TestContentRecord` or `__TestContentRecordAccessor` typealias
79-
defined in the testing library. These types exist to support the testing
78+
Do not use the `__TestContentRecord` or `__TestContentRecordAccessor` type
79+
aliases defined in the testing library. These types exist to support the testing
8080
library's macros and may change in the future (e.g. to accomodate a generic
8181
argument or to make use of a reserved field.)
8282

8383
Instead, define your own copy of these types where needed—you can copy the
8484
definitions above _verbatim_. If your test record type's `context` field (as
8585
described below) is a pointer type, make sure to change its type in your version
8686
of `TestContentRecord` accordingly so that, on systems with pointer
87-
authentication enabled, the pointer is correctly resigned at load time.
87+
authentication enabled, the pointer is correctly re-signed at load time.
8888

8989
### Record content
9090

Documentation/Porting.md

+6
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ to load that information:
145145
+ let resourceName: Str255 = switch kind {
146146
+ case .testContent:
147147
+ "__swift5_tests"
148+
+#if !SWT_NO_LEGACY_TEST_DISCOVERY
148149
+ case .typeMetadata:
149150
+ "__swift5_types"
151+
+#endif
150152
+ }
151153
+
152154
+ let oldRefNum = CurResFile()
@@ -219,15 +221,19 @@ diff --git a/Sources/_TestingInternals/Discovery.cpp b/Sources/_TestingInternals
219221
+#elif defined(macintosh)
220222
+extern "C" const char testContentSectionBegin __asm__("...");
221223
+extern "C" const char testContentSectionEnd __asm__("...");
224+
+#if !defined(SWT_NO_LEGACY_TEST_DISCOVERY)
222225
+extern "C" const char typeMetadataSectionBegin __asm__("...");
223226
+extern "C" const char typeMetadataSectionEnd __asm__("...");
227+
+#endif
224228
#else
225229
#warning Platform-specific implementation missing: Runtime test discovery unavailable (static)
226230
static const char testContentSectionBegin = 0;
227231
static const char& testContentSectionEnd = testContentSectionBegin;
232+
#if !defined(SWT_NO_LEGACY_TEST_DISCOVERY)
228233
static const char typeMetadataSectionBegin = 0;
229234
static const char& typeMetadataSectionEnd = testContentSectionBegin;
230235
#endif
236+
#endif
231237
```
232238

233239
These symbols must have unique addresses corresponding to the first byte of the

Package.swift

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ extension Array where Element == PackageDescription.SwiftSetting {
165165
.enableExperimentalFeature("AccessLevelOnImport"),
166166
.enableUpcomingFeature("InternalImportsByDefault"),
167167

168+
.enableExperimentalFeature("SymbolLinkageMarkers"),
169+
168170
.define("SWT_TARGET_OS_APPLE", .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])),
169171

170172
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),

Sources/Testing/Discovery+Platform.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ struct SectionBounds: Sendable {
2727
/// The test content metadata section.
2828
case testContent
2929

30+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
3031
/// The type metadata section.
3132
case typeMetadata
33+
#endif
3234
}
3335

3436
/// All section bounds of the given kind found in the current process.
@@ -60,8 +62,10 @@ extension SectionBounds.Kind {
6062
switch self {
6163
case .testContent:
6264
("__DATA_CONST", "__swift5_tests")
65+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
6366
case .typeMetadata:
6467
("__TEXT", "__swift5_types")
68+
#endif
6569
}
6670
}
6771
}
@@ -101,9 +105,8 @@ private let _startCollectingSectionBounds: Void = {
101105
var size = CUnsignedLong(0)
102106
if let start = getsectiondata(mh, segmentName.utf8Start, sectionName.utf8Start, &size), size > 0 {
103107
let buffer = UnsafeRawBufferPointer(start: start, count: Int(clamping: size))
104-
let sb = SectionBounds(imageAddress: mh, buffer: buffer)
105108
_sectionBounds.withLock { sectionBounds in
106-
sectionBounds[kind]!.append(sb)
109+
sectionBounds[kind]!.append(SectionBounds(imageAddress: mh, buffer: buffer))
107110
}
108111
}
109112
}
@@ -165,8 +168,10 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> [SectionBounds] {
165168
let range = switch context.pointee.kind {
166169
case .testContent:
167170
sections.swift5_tests
171+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
168172
case .typeMetadata:
169173
sections.swift5_type_metadata
174+
#endif
170175
}
171176
let start = UnsafeRawPointer(bitPattern: range.start)
172177
let size = Int(clamping: range.length)
@@ -255,8 +260,10 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<Section
255260
let sectionName = switch kind {
256261
case .testContent:
257262
".sw5test"
263+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
258264
case .typeMetadata:
259265
".sw5tymd"
266+
#endif
260267
}
261268
return HMODULE.all.lazy.compactMap { _findSection(named: sectionName, in: $0) }
262269
}

Sources/Testing/Discovery.swift

+17
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ public typealias __TestContentRecord = (
4949
reserved2: UInt
5050
)
5151

52+
/// Check if the type at the given address is equal to a given Swift type.
53+
///
54+
/// - Parameters:
55+
/// - typeAddress: A pointer to a Swift type, as in the `type` argument to a
56+
/// test content record accessor function.
57+
/// - type: The type expected to be at `typeAddress`.
58+
///
59+
/// - Returns: Whether or not the type at `typeAddress` equals `type`.
60+
///
61+
/// - Warning: This function is used to implement the `@Test`, `@Suite`, and
62+
/// `#expect(exitsWith:)` macros. Do not use it directly.
63+
public func __type(at typeAddress: UnsafeRawPointer, is type: (some ~Copyable).Type) -> Bool {
64+
// `typeAddress` may actually point to a move-only type, but attempting to
65+
// load it as such leads to a crash. SEE: rdar://134277439
66+
TypeInfo(describing: typeAddress.load(as: Any.Type.self)) == TypeInfo(describing: type)
67+
}
68+
5269
// MARK: -
5370

5471
/// A protocol describing a type that can be stored as test content at compile

Sources/Testing/ExitTests/ExitTest.swift

+46-22
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ public struct __ExitTest: Sendable, ~Copyable {
5959
private var _lo: UInt64
6060
private var _hi: UInt64
6161

62-
/// Initialize an instance of this type.
63-
///
64-
/// - Warning: This member is used to implement the `#expect(exitsWith:)`
65-
/// macro. Do not use it directly.
66-
public init(__uuid uuid: (UInt64, UInt64)) {
62+
init(_ uuid: (UInt64, UInt64)) {
6763
self._lo = uuid.0
6864
self._hi = uuid.1
6965
}
@@ -77,7 +73,7 @@ public struct __ExitTest: Sendable, ~Copyable {
7773
/// Do not invoke this closure directly. Instead, invoke ``callAsFunction()``
7874
/// to run the exit test. Running the exit test will always terminate the
7975
/// current process.
80-
fileprivate var body: @Sendable () async throws -> Void
76+
fileprivate var body: @Sendable () async throws -> Void = {}
8177

8278
/// Storage for ``observedValues``.
8379
///
@@ -113,18 +109,6 @@ public struct __ExitTest: Sendable, ~Copyable {
113109
_observedValues = newValue
114110
}
115111
}
116-
117-
/// Initialize an exit test at runtime.
118-
///
119-
/// - Warning: This initializer is used to implement the `#expect(exitsWith:)`
120-
/// macro. Do not use it directly.
121-
public init(
122-
__identifiedBy id: ID,
123-
body: @escaping @Sendable () async throws -> Void = {}
124-
) {
125-
self.id = id
126-
self.body = body
127-
}
128112
}
129113

130114
#if !SWT_NO_EXIT_TESTS
@@ -229,6 +213,42 @@ extension ExitTest: TestContent {
229213
}
230214

231215
typealias TestContentAccessorHint = ID
216+
217+
/// Store the test generator function into the given memory.
218+
///
219+
/// - Parameters:
220+
/// - outValue: The uninitialized memory to store the exit test into.
221+
/// - id: The unique identifier of the exit test to store.
222+
/// - body: The body closure of the exit test to store. This value is passed
223+
/// as a thin function to avoid a heap allocation when the exit test does
224+
/// not match the hint (which is common during linear searches.)
225+
/// - typeAddress: A pointer to the expected type of the exit test as passed
226+
/// to the test content record calling this function.
227+
/// - hintAddress: A pointer to an instance of ``ID`` to use as a hint.
228+
///
229+
/// - Returns: Whether or not an exit test was stored into `outValue`.
230+
///
231+
/// - Warning: This function is used to implement the `#expect(exitsWith:)`
232+
/// macro. Do not use it directly.
233+
public static func __store(
234+
_ id: (UInt64, UInt64),
235+
_ body: @convention(thin) @Sendable () async throws -> Void,
236+
into outValue: UnsafeMutableRawPointer,
237+
asTypeAt typeAddress: UnsafeRawPointer,
238+
withHintAt hintAddress: UnsafeRawPointer? = nil
239+
) -> CBool {
240+
let callerExpectedType = TypeInfo(describing: typeAddress.load(as: Any.Type.self))
241+
let selfType = TypeInfo(describing: Self.self)
242+
guard callerExpectedType == selfType else {
243+
return false
244+
}
245+
let id = ID(id)
246+
if let hintedID = hintAddress?.load(as: ID.self), hintedID != id {
247+
return false
248+
}
249+
outValue.initializeMemory(as: Self.self, to: Self(id: id, body: { try await body() }))
250+
return true
251+
}
232252
}
233253

234254
@_spi(Experimental) @_spi(ForToolsIntegrationOnly)
@@ -247,11 +267,15 @@ extension ExitTest {
247267
}
248268
}
249269

270+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
250271
// Call the legacy lookup function that discovers tests embedded in types.
251272
return types(withNamesContaining: exitTestContainerTypeNameMagic).lazy
252273
.compactMap { $0 as? any __ExitTestContainer.Type }
253-
.first { $0.__id == id }
254-
.map { ExitTest(__identifiedBy: $0.__id, body: $0.__body) }
274+
.first { ID($0.__id) == id }
275+
.map { ExitTest(id: ID($0.__id), body: $0.__body) }
276+
#else
277+
return nil
278+
#endif
255279
}
256280
}
257281

@@ -280,7 +304,7 @@ extension ExitTest {
280304
/// `await #expect(exitsWith:) { }` invocations regardless of calling
281305
/// convention.
282306
func callExitTest(
283-
identifiedBy exitTestID: ExitTest.ID,
307+
identifiedBy exitTestID: (UInt64, UInt64),
284308
exitsWith expectedExitCondition: ExitCondition,
285309
observing observedValues: [any PartialKeyPath<ExitTestArtifacts> & Sendable],
286310
expression: __Expression,
@@ -295,7 +319,7 @@ func callExitTest(
295319

296320
var result: ExitTestArtifacts
297321
do {
298-
var exitTest = ExitTest(__identifiedBy: exitTestID)
322+
var exitTest = ExitTest(id: ExitTest.ID(exitTestID))
299323
exitTest.observedValues = observedValues
300324
result = try await configuration.exitTestHandler(exitTest)
301325

Sources/Testing/Expectations/ExpectationChecking+Macro.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ public func __checkClosureCall<R>(
11471147
/// `#require()` macros. Do not call it directly.
11481148
@_spi(Experimental)
11491149
public func __checkClosureCall(
1150-
identifiedBy exitTestID: __ExitTest.ID,
1150+
identifiedBy exitTestID: (UInt64, UInt64),
11511151
exitsWith expectedExitCondition: ExitCondition,
11521152
observing observedValues: [any PartialKeyPath<ExitTestArtifacts> & Sendable],
11531153
performing body: @convention(thin) () -> Void,

Sources/Testing/Test+Discovery+Legacy.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
private import _TestingInternals
1212

13+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
1314
/// A protocol describing a type that contains tests.
1415
///
1516
/// - Warning: This protocol is used to implement the `@Test` macro. Do not use
@@ -33,7 +34,7 @@ let testContainerTypeNameMagic = "__🟠$test_container__"
3334
@_spi(Experimental)
3435
public protocol __ExitTestContainer {
3536
/// The unique identifier of the exit test.
36-
static var __id: __ExitTest.ID { get }
37+
static var __id: (UInt64, UInt64) { get }
3738

3839
/// The body function of the exit test.
3940
static var __body: @Sendable () async throws -> Void { get }
@@ -60,3 +61,4 @@ func types(withNamesContaining nameSubstring: String) -> some Sequence<Any.Type>
6061
.map { unsafeBitCast($0, to: Any.Type.self) }
6162
}
6263
}
64+
#endif

Sources/Testing/Test+Discovery.swift

+39-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
private import _TestingInternals
1212

1313
extension Test {
14+
/// The type of the actual (asynchronous) generator function produced by test
15+
/// content records.
16+
///
17+
/// - Warning: This type is used to implement the `@Test` macro. Do not use it
18+
/// directly.
19+
public typealias __Generator = @Sendable () async -> Test
20+
1421
/// A type that encapsulates test content records that produce instances of
1522
/// ``Test``.
1623
///
@@ -29,14 +36,35 @@ extension Test {
2936
}
3037

3138
static var testContentAccessorTypeArgument: any ~Copyable.Type {
32-
Generator.self
39+
__Generator.self
3340
}
3441

35-
/// The type of the actual (asynchronous) generator function.
36-
typealias Generator = @Sendable () async -> Test
37-
3842
/// The actual (asynchronous) accessor function.
39-
case generator(Generator)
43+
case generator(__Generator)
44+
}
45+
46+
/// Store the test generator function into the given memory.
47+
///
48+
/// - Parameters:
49+
/// - generator: The generator function to store.
50+
/// - outValue: The uninitialized memory to store `generator` into.
51+
/// - typeAddress: A pointer to the expected type of `generator` as passed
52+
/// to the test content record calling this function.
53+
///
54+
/// - Returns: Whether or not `generator` was stored into `outValue`.
55+
///
56+
/// - Warning: This function is used to implement the `@Test` macro. Do not
57+
/// use it directly.
58+
public static func __store(
59+
_ generator: @escaping __Generator,
60+
into outValue: UnsafeMutableRawPointer,
61+
asTypeAt typeAddress: UnsafeRawPointer
62+
) -> CBool {
63+
guard typeAddress.load(as: Any.Type.self) == __Generator.self else {
64+
return false
65+
}
66+
outValue.initializeMemory(as: _Record.self, to: .generator(generator))
67+
return true
4068
}
4169

4270
/// All available ``Test`` instances in the process, according to the runtime.
@@ -53,6 +81,7 @@ extension Test {
5381
// the legacy and new mechanisms, but we can set an environment variable
5482
// to explicitly select one or the other. When we remove legacy support,
5583
// we can also remove this enumeration and environment variable check.
84+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
5685
let (useNewMode, useLegacyMode) = switch Environment.flag(named: "SWT_USE_LEGACY_TEST_DISCOVERY") {
5786
case .none:
5887
(true, true)
@@ -61,6 +90,9 @@ extension Test {
6190
case .some(false):
6291
(true, false)
6392
}
93+
#else
94+
let useNewMode = true
95+
#endif
6496

6597
// Walk all test content and gather generator functions, then call them in
6698
// a task group and collate their results.
@@ -79,6 +111,7 @@ extension Test {
79111
}
80112
}
81113

114+
#if !SWT_NO_LEGACY_TEST_DISCOVERY
82115
// Perform legacy test discovery if needed.
83116
if useLegacyMode && result.isEmpty {
84117
let types = types(withNamesContaining: testContainerTypeNameMagic).lazy
@@ -92,6 +125,7 @@ extension Test {
92125
result = await taskGroup.reduce(into: result) { $0.formUnion($1) }
93126
}
94127
}
128+
#endif
95129

96130
return result
97131
}

Sources/TestingMacros/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ target_sources(TestingMacros PRIVATE
8787
Support/Additions/DeclGroupSyntaxAdditions.swift
8888
Support/Additions/EditorPlaceholderExprSyntaxAdditions.swift
8989
Support/Additions/FunctionDeclSyntaxAdditions.swift
90+
Support/Additions/IntegerLiteralExprSyntaxAdditions.swift
9091
Support/Additions/MacroExpansionContextAdditions.swift
9192
Support/Additions/TokenSyntaxAdditions.swift
9293
Support/Additions/TriviaPieceAdditions.swift
@@ -103,6 +104,7 @@ target_sources(TestingMacros PRIVATE
103104
Support/DiagnosticMessage+Diagnosing.swift
104105
Support/SourceCodeCapturing.swift
105106
Support/SourceLocationGeneration.swift
107+
Support/TestContentGeneration.swift
106108
TagMacro.swift
107109
TestDeclarationMacro.swift
108110
TestingMacrosMain.swift)

0 commit comments

Comments
 (0)