@@ -421,22 +421,24 @@ extension _StringGuts {
421
421
extension _StringGuts {
422
422
423
423
private static var associationKey : UnsafeRawPointer {
424
+ struct AssociationKey { }
424
425
// We never dereference this, we only use this address as a unique key
425
- unsafe unsafeBitCast(
426
- ObjectIdentifier ( __StringStorage . self) ,
426
+ return unsafe unsafeBitCast(
427
+ ObjectIdentifier ( AssociationKey . self) ,
427
428
to: UnsafeRawPointer . self
428
429
)
429
430
}
430
431
431
432
internal func getAssociatedStorage( ) -> __StringStorage ? {
433
+ _internalInvariant ( _object. hasObjCBridgeableObject)
432
434
let getter = unsafe unsafeBitCast(
433
435
getGetAssociatedObjectPtr ( ) ,
434
436
to: ( @convention( c) (
435
437
AnyObject,
436
438
UnsafeRawPointer
437
439
) - > UnsafeRawPointer? ) . self
438
440
)
439
- _precondition ( _object . hasObjCBridgeableObject )
441
+
440
442
if let assocPtr = unsafe getter(
441
443
_object. objCBridgeableObject,
442
444
Self . associationKey
@@ -449,6 +451,7 @@ extension _StringGuts {
449
451
}
450
452
451
453
internal func setAssociatedStorage( _ storage: __StringStorage ) {
454
+ _internalInvariant ( _object. hasObjCBridgeableObject)
452
455
let setter = unsafe unsafeBitCast(
453
456
getSetAssociatedObjectPtr ( ) ,
454
457
to: ( @convention( c) (
@@ -468,7 +471,7 @@ extension _StringGuts {
468
471
}
469
472
470
473
internal func getOrAllocateAssociatedStorage( ) -> __StringStorage {
471
- _precondition ( _object. hasObjCBridgeableObject)
474
+ _internalInvariant ( _object. hasObjCBridgeableObject)
472
475
let unwrapped : __StringStorage
473
476
// libobjc already provides the necessary memory barriers for
474
477
// double checked locking to be safe, per comments on
@@ -482,8 +485,10 @@ extension _StringGuts {
482
485
unwrapped = storage
483
486
} else {
484
487
var contents = String . UnicodeScalarView ( )
485
- // always reserve a size larger than a small string
486
- 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
+ )
487
492
for c in String . UnicodeScalarView ( self ) {
488
493
contents. append ( c)
489
494
}
0 commit comments