Skip to content

Commit 7df4076

Browse files
authored
Merge pull request #20509 from lorentey/not-quite-shadowy-enough
[stdlib] Unexport shadow protocols
2 parents d6a9f30 + ca10375 commit 7df4076

21 files changed

+316
-199
lines changed

stdlib/public/SDK/Foundation/NSArray.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ extension Array : _ObjectiveCBridgeable {
4343
//
4444
// The bug is fixed in: OS X 10.11.0, iOS 9.0, all versions of tvOS
4545
// and watchOS.
46-
self = Array(
47-
_immutableCocoaArray:
48-
unsafeBitCast(_cocoaArray.copy() as AnyObject, to: _NSArrayCore.self))
46+
self = Array(_immutableCocoaArray: _cocoaArray.copy() as AnyObject)
4947
}
5048

5149
@_semantics("convertToObjectiveC")

stdlib/public/SDK/Foundation/NSDictionary.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Dictionary {
3434
///
3535
/// The provided `NSDictionary` will be copied to ensure that the copy can
3636
/// not be mutated by other code.
37-
fileprivate init(_cocoaDictionary: __shared _NSDictionary) {
37+
fileprivate init(_cocoaDictionary: __shared AnyObject) {
3838
assert(
3939
_isBridgedVerbatimToObjectiveC(Key.self) &&
4040
_isBridgedVerbatimToObjectiveC(Value.self),
@@ -49,9 +49,7 @@ extension Dictionary {
4949
// The bug is fixed in: OS X 10.11.0, iOS 9.0, all versions of tvOS
5050
// and watchOS.
5151
self = Dictionary(
52-
_immutableCocoaDictionary:
53-
unsafeBitCast(_cocoaDictionary.copy(with: nil) as AnyObject,
54-
to: _NSDictionary.self))
52+
_immutableCocoaDictionary: _cocoaDictionary.copy(with: nil) as AnyObject)
5553
}
5654
}
5755

@@ -75,8 +73,7 @@ extension Dictionary : _ObjectiveCBridgeable {
7573

7674
if _isBridgedVerbatimToObjectiveC(Key.self) &&
7775
_isBridgedVerbatimToObjectiveC(Value.self) {
78-
result = [Key : Value](
79-
_cocoaDictionary: unsafeBitCast(d as AnyObject, to: _NSDictionary.self))
76+
result = [Key : Value](_cocoaDictionary: d)
8077
return
8178
}
8279

stdlib/public/SDK/Foundation/NSSet.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension Set {
1717
///
1818
/// The provided `NSSet` will be copied to ensure that the copy can
1919
/// not be mutated by other code.
20-
fileprivate init(_cocoaSet: __shared _NSSet) {
20+
fileprivate init(_cocoaSet: __shared AnyObject) {
2121
assert(_isBridgedVerbatimToObjectiveC(Element.self),
2222
"Set can be backed by NSSet _variantStorage only when the member type can be bridged verbatim to Objective-C")
2323
// FIXME: We would like to call CFSetCreateCopy() to avoid doing an
@@ -29,9 +29,7 @@ extension Set {
2929
//
3030
// The bug is fixed in: OS X 10.11.0, iOS 9.0, all versions of tvOS
3131
// and watchOS.
32-
self = Set(
33-
_immutableCocoaSet:
34-
unsafeBitCast(_cocoaSet.copy(with: nil) as AnyObject, to: _NSSet.self))
32+
self = Set(_immutableCocoaSet: _cocoaSet.copy(with: nil) as AnyObject)
3533
}
3634
}
3735

@@ -69,7 +67,7 @@ extension Set : _ObjectiveCBridgeable {
6967
}
7068

7169
if _isBridgedVerbatimToObjectiveC(Element.self) {
72-
result = Set<Element>(_cocoaSet: unsafeBitCast(s, to: _NSSet.self))
70+
result = Set<Element>(_cocoaSet: s)
7371
return
7472
}
7573

stdlib/public/core/Array.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ extension Array {
17281728
// to do the bridging in an ABI safe way. Even though this looks useless,
17291729
// DO NOT DELETE!
17301730
@usableFromInline internal
1731-
func _bridgeCocoaArray<T>(_ _immutableCocoaArray: _NSArrayCore) -> Array<T> {
1731+
func _bridgeCocoaArray<T>(_ _immutableCocoaArray: AnyObject) -> Array<T> {
17321732
return Array(_buffer: _ArrayBuffer(nsArray: _immutableCocoaArray))
17331733
}
17341734

@@ -1764,7 +1764,7 @@ extension Array {
17641764
/// * `Element` is bridged verbatim to Objective-C (i.e.,
17651765
/// is a reference type).
17661766
@inlinable
1767-
public init(_immutableCocoaArray: _NSArrayCore) {
1767+
public init(_immutableCocoaArray: AnyObject) {
17681768
self = _bridgeCocoaArray(_immutableCocoaArray)
17691769
}
17701770
}

stdlib/public/core/ArrayBuffer.swift

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import SwiftShims
2020

2121
@usableFromInline
2222
internal typealias _ArrayBridgeStorage
23-
= _BridgeStorage<__ContiguousArrayStorageBase, _NSArrayCore>
23+
= _BridgeStorage<__ContiguousArrayStorageBase>
2424

2525
@usableFromInline
2626
@_fixed_layout
@@ -33,7 +33,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
3333
}
3434

3535
@inlinable
36-
internal init(nsArray: _NSArrayCore) {
36+
internal init(nsArray: AnyObject) {
3737
_sanityCheck(_isClassOrObjCExistential(Element.self))
3838
_storage = _ArrayBridgeStorage(objC: nsArray)
3939
}
@@ -121,8 +121,8 @@ extension _ArrayBuffer {
121121
///
122122
/// O(1) if the element type is bridged verbatim, O(*n*) otherwise.
123123
@inlinable
124-
internal func _asCocoaArray() -> _NSArrayCore {
125-
return _fastPath(_isNative) ? _native._asCocoaArray() : _nonNative
124+
internal func _asCocoaArray() -> AnyObject {
125+
return _fastPath(_isNative) ? _native._asCocoaArray() : _nonNative.buffer
126126
}
127127

128128
/// If this buffer is backed by a uniquely-referenced mutable
@@ -175,8 +175,7 @@ extension _ArrayBuffer {
175175
)
176176
}
177177
else {
178-
let ns = _nonNative
179-
let element = ns.objectAt(index)
178+
let element = _nonNative[index]
180179
precondition(
181180
element is Element,
182181
"""
@@ -216,26 +215,12 @@ extension _ArrayBuffer {
216215
if _fastPath(_isNative) {
217216
return _native._copyContents(subRange: bounds, initializing: target)
218217
}
219-
220-
let nonNative = _nonNative
221-
222-
let nsSubRange = SwiftShims._SwiftNSRange(
223-
location: bounds.lowerBound,
224-
length: bounds.upperBound - bounds.lowerBound)
225-
226-
let buffer = UnsafeMutableRawPointer(target).assumingMemoryBound(
227-
to: AnyObject.self)
228-
229-
// Copies the references out of the NSArray without retaining them
230-
nonNative.getObjects(buffer, range: nsSubRange)
231-
232-
// Make another pass to retain the copied objects
233-
var result = target
234-
for _ in bounds {
235-
result.initialize(to: result.pointee)
236-
result += 1
237-
}
238-
return result
218+
let buffer = UnsafeMutableRawPointer(target)
219+
.assumingMemoryBound(to: AnyObject.self)
220+
let result = _nonNative._copyContents(
221+
subRange: bounds,
222+
initializing: buffer)
223+
return UnsafeMutableRawPointer(result).assumingMemoryBound(to: Element.self)
239224
}
240225

241226
/// Returns a `_SliceBuffer` containing the given sub-range of elements in
@@ -244,45 +229,10 @@ extension _ArrayBuffer {
244229
internal subscript(bounds: Range<Int>) -> _SliceBuffer<Element> {
245230
get {
246231
_typeCheck(bounds)
247-
248232
if _fastPath(_isNative) {
249233
return _native[bounds]
250234
}
251-
252-
let boundsCount = bounds.count
253-
if boundsCount == 0 {
254-
return _SliceBuffer(
255-
_buffer: _ContiguousArrayBuffer<Element>(),
256-
shiftedToStartIndex: bounds.lowerBound)
257-
}
258-
259-
// Look for contiguous storage in the NSArray
260-
let nonNative = self._nonNative
261-
let cocoa = _CocoaArrayWrapper(nonNative)
262-
let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices)
263-
264-
if let cocoaStorageBaseAddress = cocoaStorageBaseAddress {
265-
let basePtr = UnsafeMutableRawPointer(cocoaStorageBaseAddress)
266-
.assumingMemoryBound(to: Element.self)
267-
return _SliceBuffer(
268-
owner: nonNative,
269-
subscriptBaseAddress: basePtr,
270-
indices: bounds,
271-
hasNativeBuffer: false)
272-
}
273-
274-
// No contiguous storage found; we must allocate
275-
let result = _ContiguousArrayBuffer<Element>(
276-
_uninitializedCount: boundsCount, minimumCapacity: 0)
277-
278-
// Tell Cocoa to copy the objects into our storage
279-
cocoa.buffer.getObjects(
280-
UnsafeMutableRawPointer(result.firstElementAddress)
281-
.assumingMemoryBound(to: AnyObject.self),
282-
range: _SwiftNSRange(location: bounds.lowerBound, length: boundsCount))
283-
284-
return _SliceBuffer(
285-
_buffer: result, shiftedToStartIndex: bounds.lowerBound)
235+
return _nonNative[bounds].unsafeCastElements(to: Element.self)
286236
}
287237
set {
288238
fatalError("not implemented")
@@ -315,7 +265,7 @@ extension _ArrayBuffer {
315265
_native.count = newValue
316266
}
317267
}
318-
268+
319269
/// Traps if an inout violation is detected or if the buffer is
320270
/// native and the subscript is out of range.
321271
///
@@ -392,7 +342,7 @@ extension _ArrayBuffer {
392342
)
393343
} else {
394344
// ObjC arrays do their own subscript checking.
395-
element = _nonNative.objectAt(i)
345+
element = _nonNative[i]
396346
precondition(
397347
element is Element,
398348
"""
@@ -460,7 +410,7 @@ extension _ArrayBuffer {
460410
/// An object that keeps the elements stored in this buffer alive.
461411
@inlinable
462412
internal var owner: AnyObject {
463-
return _fastPath(_isNative) ? _native._storage : _nonNative
413+
return _fastPath(_isNative) ? _native._storage : _nonNative.buffer
464414
}
465415

466416
/// An object that keeps the elements stored in this buffer alive.
@@ -481,7 +431,8 @@ extension _ArrayBuffer {
481431
return _native.identity
482432
}
483433
else {
484-
return UnsafeRawPointer(Unmanaged.passUnretained(_nonNative).toOpaque())
434+
return UnsafeRawPointer(
435+
Unmanaged.passUnretained(_nonNative.buffer).toOpaque())
485436
}
486437
}
487438

@@ -550,11 +501,11 @@ extension _ArrayBuffer {
550501
}
551502

552503
@inlinable
553-
internal var _nonNative: _NSArrayCore {
504+
internal var _nonNative: _CocoaArrayWrapper {
554505
@inline(__always)
555506
get {
556507
_sanityCheck(_isClassOrObjCExistential(Element.self))
557-
return _storage.objCInstance
508+
return _CocoaArrayWrapper(_storage.objCInstance)
558509
}
559510
}
560511
}

stdlib/public/core/BridgeStorage.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Types that are bridged to Objective-C need to manage an object
1414
// that may be either some native class or the @objc Cocoa
1515
// equivalent. _BridgeStorage discriminates between these two
16-
// possibilities and stores a few extra bits when the stored type is
16+
// possibilities and stores a single extra bit when the stored type is
1717
// native. It is assumed that the @objc class instance may in fact
1818
// be a tagged pointer, and thus no extra bits may be available.
1919
//
@@ -22,15 +22,12 @@ import SwiftShims
2222

2323
@_fixed_layout
2424
@usableFromInline
25-
internal struct _BridgeStorage<
26-
NativeClass: AnyObject,
27-
ObjCClass: AnyObject
28-
> {
25+
internal struct _BridgeStorage<NativeClass: AnyObject> {
2926
@usableFromInline
3027
internal typealias Native = NativeClass
3128

3229
@usableFromInline
33-
internal typealias ObjC = ObjCClass
30+
internal typealias ObjC = AnyObject
3431

3532
// rawValue is passed inout to _isUnique. Although its value
3633
// is unchanged, it must appear mutable to the optimizer.

0 commit comments

Comments
 (0)