Skip to content

Commit 5291f1e

Browse files
committed
[stdlib] tweaks in associated object code
1 parent 906fdb1 commit 5291f1e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,15 @@ extension _StringGuts {
430430
}
431431

432432
internal func getAssociatedStorage() -> __StringStorage? {
433+
_internalInvariant(_object.hasObjCBridgeableObject)
433434
let getter = unsafe unsafeBitCast(
434435
getGetAssociatedObjectPtr(),
435436
to: (@convention(c)(
436437
AnyObject,
437438
UnsafeRawPointer
438439
) -> UnsafeRawPointer?).self
439440
)
440-
_precondition(_object.hasObjCBridgeableObject)
441+
441442
if let assocPtr = unsafe getter(
442443
_object.objCBridgeableObject,
443444
Self.associationKey
@@ -450,6 +451,7 @@ extension _StringGuts {
450451
}
451452

452453
internal func setAssociatedStorage(_ storage: __StringStorage) {
454+
_internalInvariant(_object.hasObjCBridgeableObject)
453455
let setter = unsafe unsafeBitCast(
454456
getSetAssociatedObjectPtr(),
455457
to: (@convention(c)(
@@ -469,7 +471,7 @@ extension _StringGuts {
469471
}
470472

471473
internal func getOrAllocateAssociatedStorage() -> __StringStorage {
472-
_precondition(_object.hasObjCBridgeableObject)
474+
_internalInvariant(_object.hasObjCBridgeableObject)
473475
let unwrapped: __StringStorage
474476
// libobjc already provides the necessary memory barriers for
475477
// double checked locking to be safe, per comments on
@@ -483,8 +485,10 @@ extension _StringGuts {
483485
unwrapped = storage
484486
} else {
485487
var contents = String.UnicodeScalarView()
486-
// always reserve a size larger than a small string
487-
contents.reserveCapacity(Swift.max(_SmallString.capacity + 1, 1 + count + count >> 1))
488+
// always reserve a capacity larger than a small string
489+
contents.reserveCapacity(
490+
Swift.max(_SmallString.capacity + 1, count + count >> 1)
491+
)
488492
for c in String.UnicodeScalarView(self) {
489493
contents.append(c)
490494
}

0 commit comments

Comments
 (0)