Skip to content

Commit 84b1d49

Browse files
authored
Add a few more inlinability annotation (#383)
Motivation: Version 3.13.0 hits a compiler assertion when compiling in release mode on Linux with Swift 6.1. This appears to stem from missing inlinable annotations. Modifications: - Add a few more `@inlinable` and `@usableFromInline` annotations Result: Builds on 6.1+ in release mode
1 parent 88b9c9b commit 84b1d49

File tree

1 file changed

+2
-36
lines changed

1 file changed

+2
-36
lines changed

Sources/Crypto/Util/SecureBytes.swift

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ private let emptyStorage:SecureBytes.Backing = SecureBytes.Backing.createEmpty()
2121

2222
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
2323
struct SecureBytes {
24-
@usableFromInline
2524
var backing: Backing
2625

27-
@inlinable
2826
init() {
2927
self = .init(count: 0)
3028
}
3129

32-
@usableFromInline
3330
init(count: Int) {
3431
if count == 0 {
3532
self.backing = emptyStorage
@@ -43,7 +40,6 @@ struct SecureBytes {
4340
}
4441

4542
/// Allows initializing a SecureBytes object with a closure that will initialize the memory.
46-
@usableFromInline
4743
init(unsafeUninitializedCapacity: Int, initializingWith callback: (inout UnsafeMutableRawBufferPointer, inout Int) throws -> Void) rethrows {
4844
self.backing = Backing.create(capacity: unsafeUninitializedCapacity)
4945
try self.backing._withVeryUnsafeMutableBytes { veryUnsafePointer in
@@ -59,7 +55,6 @@ struct SecureBytes {
5955

6056
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
6157
extension SecureBytes {
62-
@inlinable
6358
mutating func append<C: Collection>(_ data: C) where C.Element == UInt8 {
6459
let requiredCapacity = self.count + data.count
6560
let backingCapacity = self.backing.allocatedCapacity
@@ -71,7 +66,6 @@ extension SecureBytes {
7166
self.backing._appendBytes(data)
7267
}
7368

74-
@usableFromInline
7569
mutating func reserveCapacity(_ n: Int) {
7670
let backingCapacity = self.backing.allocatedCapacity
7771
if backingCapacity >= n {
@@ -95,32 +89,27 @@ extension SecureBytes: Equatable {
9589
// MARK: - Collection conformance
9690
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
9791
extension SecureBytes: Collection {
98-
@usableFromInline
9992
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
10093
struct Index {
101-
/* fileprivate but usableFromInline */ @usableFromInline var offset: Int
94+
/* fileprivate but usableFromInline */ var offset: Int
10295

103-
/*@inlinable*/ @usableFromInline internal init(offset: Int) {
96+
/*@inlinable*/ internal init(offset: Int) {
10497
self.offset = offset
10598
}
10699
}
107100

108-
@inlinable
109101
var startIndex: Index {
110102
return Index(offset: 0)
111103
}
112104

113-
@inlinable
114105
var endIndex: Index {
115106
return Index(offset: self.count)
116107
}
117108

118-
@inlinable
119109
var count: Int {
120110
return self.backing.count
121111
}
122112

123-
@inlinable
124113
subscript(_ index: Index) -> UInt8 {
125114
get {
126115
return self.backing[offset: index.offset]
@@ -130,7 +119,6 @@ extension SecureBytes: Collection {
130119
}
131120
}
132121

133-
@inlinable
134122
func index(after index: Index) -> Index {
135123
return index.advanced(by: 1)
136124
}
@@ -139,7 +127,6 @@ extension SecureBytes: Collection {
139127
// MARK: - BidirectionalCollection conformance
140128
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
141129
extension SecureBytes: BidirectionalCollection {
142-
@inlinable
143130
func index(before index: Index) -> Index {
144131
return index.advanced(by: -1)
145132
}
@@ -156,7 +143,6 @@ extension SecureBytes: MutableCollection { }
156143
// MARK: - RangeReplaceableCollection conformance
157144
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
158145
extension SecureBytes: RangeReplaceableCollection {
159-
@inlinable
160146
mutating func replaceSubrange<C: Collection>(_ subrange: Range<Index>, with newElements: C) where C.Element == UInt8 {
161147
let requiredCapacity = self.backing.count - subrange.count + newElements.count
162148
let backingCapacity = self.backing.allocatedCapacity
@@ -182,7 +168,6 @@ extension SecureBytes: RangeReplaceableCollection {
182168
}
183169

184170
// The default implementation of this from RangeReplaceableCollection can't take advantage of `ContiguousBytes`, so we override it here
185-
@inlinable
186171
public mutating func append<Elements: Sequence>(contentsOf newElements: Elements) where Elements.Element == UInt8 {
187172
let done:Void? = newElements.withContiguousStorageIfAvailable {
188173
replaceSubrange(endIndex..<endIndex, with: $0)
@@ -199,12 +184,10 @@ extension SecureBytes: RangeReplaceableCollection {
199184
// MARK: - ContiguousBytes conformance
200185
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
201186
extension SecureBytes: ContiguousBytes {
202-
@inlinable
203187
func withUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
204188
return try self.backing.withUnsafeBytes(body)
205189
}
206190

207-
@inlinable
208191
mutating func withUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
209192
if !isKnownUniquelyReferenced(&self.backing) {
210193
self.backing = Backing.create(copying: self.backing)
@@ -213,7 +196,6 @@ extension SecureBytes: ContiguousBytes {
213196
return try self.backing.withUnsafeMutableBytes(body)
214197
}
215198

216-
@inlinable
217199
func withContiguousStorageIfAvailable<R>(_ body: (UnsafeBufferPointer<UInt8>) throws -> R) rethrows -> R? {
218200
return try self.backing.withContiguousStorageIfAvailable(body)
219201
}
@@ -222,7 +204,6 @@ extension SecureBytes: ContiguousBytes {
222204
// MARK: - DataProtocol conformance
223205
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
224206
extension SecureBytes: DataProtocol {
225-
@inlinable
226207
var regions: CollectionOfOne<SecureBytes> {
227208
return CollectionOfOne(self)
228209
}
@@ -257,37 +238,29 @@ extension SecureBytes.Index: Strideable {
257238
// MARK: - Heap allocated backing storage.
258239
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
259240
extension SecureBytes {
260-
@usableFromInline
261241
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
262242
internal struct BackingHeader {
263-
@usableFromInline
264243
internal var count: Int
265244

266-
@usableFromInline
267245
internal var capacity: Int
268246
}
269247

270-
@usableFromInline
271248
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
272249
internal class Backing: ManagedBuffer<BackingHeader, UInt8> {
273250

274-
@usableFromInline
275251
class func createEmpty() -> Backing {
276252
return Backing.create(minimumCapacity: 0, makingHeaderWith: { _ in BackingHeader(count: 0, capacity: 0) }) as! Backing
277253
}
278254

279-
@usableFromInline
280255
class func create(capacity: Int) -> Backing {
281256
let capacity = Int(UInt32(capacity).nextPowerOf2ClampedToMax())
282257
return Backing.create(minimumCapacity: capacity, makingHeaderWith: { _ in BackingHeader(count: 0, capacity: capacity) }) as! Backing
283258
}
284259

285-
@usableFromInline
286260
class func create(copying original: Backing) -> Backing {
287261
return Backing.create(bytes: original)
288262
}
289263

290-
@inlinable
291264
class func create<D: ContiguousBytes>(bytes: D) -> Backing {
292265
return bytes.withUnsafeBytes { bytesPtr in
293266
let backing = Backing.create(capacity: bytesPtr.count)
@@ -300,7 +273,6 @@ extension SecureBytes {
300273
}
301274
}
302275

303-
@usableFromInline
304276
class func create(randomBytes: Int) -> Backing {
305277
let backing = Backing.create(capacity: randomBytes)
306278
backing._withVeryUnsafeMutableBytes { targetPtr in
@@ -320,7 +292,6 @@ extension SecureBytes {
320292
}
321293
}
322294

323-
@usableFromInline
324295
var count: Int {
325296
get {
326297
return self.header.count
@@ -330,7 +301,6 @@ extension SecureBytes {
330301
}
331302
}
332303

333-
@usableFromInline
334304
subscript(offset offset: Int) -> UInt8 {
335305
get {
336306
// precondition(offset >= 0 && offset < self.count)
@@ -346,7 +316,6 @@ extension SecureBytes {
346316

347317
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
348318
extension SecureBytes.Backing {
349-
@usableFromInline
350319
var allocatedCapacity: Int {
351320
#if os(OpenBSD)
352321
return self.header.capacity
@@ -419,7 +388,6 @@ extension SecureBytes.Backing {
419388
/// Moves the range of bytes identified by the slice by the delta, crashing if the move would
420389
/// place the bytes out of the storage. Note that this does not update the count: external code
421390
/// must ensure that that happens.
422-
@usableFromInline
423391
/* private but usableFromInline */ func _moveBytes(range: Range<Int>, by delta: Int) {
424392
// We have to check that the range is within the delta, as is the new location.
425393
precondition(range.lowerBound >= 0)
@@ -437,7 +405,6 @@ extension SecureBytes.Backing {
437405
}
438406

439407
// Copies some bytes into the buffer at the appropriate place. Does not update count: external code must do so.
440-
@inlinable
441408
/* private but inlinable */ func _copyBytes<C: Collection>(_ bytes: C, at offset: Int) where C.Element == UInt8 {
442409
precondition(offset >= 0)
443410
precondition(offset + bytes.count <= self.allocatedCapacity)
@@ -470,7 +437,6 @@ extension SecureBytes.Backing: ContiguousBytes {
470437
}
471438

472439
/// Very unsafe in the sense that this points to uninitialized memory. Used only for implementations within this file.
473-
@inlinable
474440
/* private but inlinable */ func _withVeryUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
475441
let capacity = self.allocatedCapacity
476442

0 commit comments

Comments
 (0)