Skip to content

Commit b986343

Browse files
authored
Always cast the bsearch() context argument to optional. (#1527)
Different platforms assign different nullability to the `context` argument of `bsearch()`'s callback function. For consistency's sake and to avoid problems porting to future platforms, always cast the value to an optional before loading from it. The compiler is smart enough to eliminate the copy/cast, so this is ultimately a zero-cost no-op. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent e20dc82 commit b986343

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Sources/Testing/Support/Additions/ArrayAdditions.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ extension Array {
5454
return withUnsafePointer(to: context) { context in
5555
self.withUnsafeBufferPointer { elements in
5656
let result = bsearch(context, elements.baseAddress!, elements.count, MemoryLayout<Element>.stride) { contextAddress, elementAddress in
57-
#if os(Android) || os(FreeBSD)
58-
let contextAddress = contextAddress as UnsafeRawPointer?
59-
#endif
57+
// Some platforms mark this argument `_Nonnull`, so unconditionally
58+
// cast it to an optional before loading from it (the compiler will
59+
// optimize this line away).
60+
let contextAddress: UnsafeRawPointer? = contextAddress
6061
let context = contextAddress!.load(as: _BinarySearchContext.self)
6162
return context.compare(elementAddress)
6263
}

0 commit comments

Comments
 (0)