@@ -23,7 +23,7 @@ struct SectionBounds: Sendable {
2323
2424 /// An enumeration describing the different sections discoverable by the
2525 /// testing library.
26- enum Kind : Int , Equatable , Hashable , CaseIterable {
26+ enum Kind : Equatable , Hashable , CaseIterable {
2727 /// The test content metadata section.
2828 case testContent
2929
@@ -146,7 +146,7 @@ private let _startCollectingSectionBounds: Void = {
146146///
147147/// - Returns: An array of structures describing the bounds of all known test
148148/// content sections in the current process.
149- private func _sectionBounds( _ kind: SectionBounds . Kind ) -> [ SectionBounds ] {
149+ private func _sectionBounds( _ kind: SectionBounds . Kind ) -> some Sequence < SectionBounds > {
150150 _startCollectingSectionBounds
151151 return _sectionBounds. withUnsafeMutablePointers { sectionBounds, lock in
152152 pthread_mutex_lock ( lock)
@@ -169,7 +169,7 @@ private import SwiftShims // For MetadataSections
169169///
170170/// - Returns: An array of structures describing the bounds of all known test
171171/// content sections in the current process.
172- private func _sectionBounds( _ kind: SectionBounds . Kind ) -> [ SectionBounds ] {
172+ private func _sectionBounds( _ kind: SectionBounds . Kind ) -> some Sequence < SectionBounds > {
173173 struct Context {
174174 var kind : SectionBounds . Kind
175175 var result = [ SectionBounds] ( )
@@ -297,14 +297,63 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> some Sequence<Section
297297/// - kind: Ignored.
298298///
299299/// - Returns: The empty array.
300- private func _sectionBounds( _ kind: SectionBounds . Kind ) -> [ SectionBounds ] {
300+ private func _sectionBounds( _ kind: SectionBounds . Kind ) -> some Sequence < SectionBounds > {
301301 #warning("Platform-specific implementation missing: Runtime test discovery unavailable (dynamic)")
302- return [ ]
302+ return EmptyCollection ( )
303303}
304304#endif
305305#else
306306// MARK: - Statically-linked implementation
307307
308+ /// The bounds of the statically-linked sections of interest to the testing
309+ /// library.
310+ ///
311+ /// We use `@_silgen_name(raw:)` to reference these linker-defined symbols.
312+ /// Then, to get their addresses, we must use `&` (`withPointer(to:)` is not
313+ /// guaranteed to avoid a copy in this context.) Hence, they must also be
314+ /// declared `var` rather than `let`, but they should be treated as read-only.
315+ #if SWT_TARGET_OS_APPLE
316+ @_silgen_name ( raw: " section$start$__DATA_CONST$__swift5_tests " ) private nonisolated ( unsafe) var _testContentSectionBegin : CChar
317+ @_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
322+ #elseif os(WASI)
323+ @_silgen_name ( raw: " __start_swift5_tests " ) private nonisolated ( unsafe) var _testContentSectionBegin : CChar
324+ @_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
329+ #else
330+ #warning("Platform-specific implementation missing: Runtime test discovery unavailable (static)")
331+ private nonisolated ( unsafe) var _testContentSectionBegin : Void
332+ 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
337+
338+ extension UnsafeRawBufferPointer {
339+ /// Construct an empty buffer.
340+ fileprivate init ( _: Void = ( ) , sectionBegin _: UnsafeRawPointer , sectionEnd _: UnsafeRawPointer ) {
341+ self . init ( start: nil , count: 0 )
342+ }
343+ }
344+ #endif
345+
346+ extension UnsafeRawBufferPointer {
347+ /// Construct a buffer representing a section's byte bounds.
348+ ///
349+ /// - Parameters:
350+ /// - sectionBegin: The address of the first byte of the section.
351+ /// - sectionEnd: The address of the first byte _after_ the section.
352+ @_disfavoredOverload fileprivate init ( sectionBegin: UnsafeRawPointer , sectionEnd: UnsafeRawPointer ) {
353+ self . init ( start: sectionBegin, count: sectionEnd - sectionBegin)
354+ }
355+ }
356+
308357/// The common implementation of ``SectionBounds/all(_:)`` for platforms that do
309358/// not support dynamic linking.
310359///
@@ -313,10 +362,15 @@ private func _sectionBounds(_ kind: SectionBounds.Kind) -> [SectionBounds] {
313362///
314363/// - Returns: A structure describing the bounds of the type metadata section
315364/// contained in the same image as the testing library itself.
316- private func _sectionBounds( _ kind: SectionBounds . Kind ) -> CollectionOfOne < SectionBounds > {
317- var ( baseAddress, count) : ( UnsafeRawPointer ? , Int ) = ( nil , 0 )
318- swt_getStaticallyLinkedSectionBounds ( kind. rawValue, & baseAddress, & count)
319- let buffer = UnsafeRawBufferPointer ( start: baseAddress, count: count)
365+ private func _sectionBounds( _ kind: SectionBounds . Kind ) -> some Sequence < SectionBounds > {
366+ let buffer = switch kind {
367+ case . testContent:
368+ UnsafeRawBufferPointer ( sectionBegin: & _testContentSectionBegin, sectionEnd: & _testContentSectionEnd)
369+ #if !SWT_NO_LEGACY_TEST_DISCOVERY
370+ case . typeMetadata:
371+ UnsafeRawBufferPointer ( sectionBegin: & _typeMetadataSectionBegin, sectionEnd: & _typeMetadataSectionEnd)
372+ #endif
373+ }
320374 let sb = SectionBounds ( imageAddress: nil , buffer: buffer)
321375 return CollectionOfOne ( sb)
322376}
0 commit comments