Skip to content

Commit c3fb541

Browse files
authored
Merge pull request #4083 from apple/stdlib-make-array-implementation-internal-2
stdlib: make Array implementation internal
2 parents 9c05bdc + 9705ccb commit c3fb541

File tree

15 files changed

+163
-107
lines changed

15 files changed

+163
-107
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ public func expectEqual<T : Equatable, U : Equatable, V : Equatable>(
150150
expectEqualTest(expected.2, actual.2, ${trace}, showFrame: false) {$0 == $1}
151151
}
152152

153+
public func expectEqual<T : Equatable, U : Equatable, V : Equatable, W : Equatable>(
154+
_ expected: (T, U, V, W), _ actual: (T, U, V, W), ${TRACE}) {
155+
expectEqualTest(expected.0, actual.0, ${trace}, showFrame: false) {$0 == $1}
156+
expectEqualTest(expected.1, actual.1, ${trace}, showFrame: false) {$0 == $1}
157+
expectEqualTest(expected.2, actual.2, ${trace}, showFrame: false) {$0 == $1}
158+
expectEqualTest(expected.3, actual.3, ${trace}, showFrame: false) {$0 == $1}
159+
}
160+
153161
public func expectationFailure(
154162
_ reason: String,
155163
trace message: String,

stdlib/private/StdlibUnittestFoundationExtras/StdlibUnittestFoundationExtras.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ internal func _stdlib_NSArray_getObjects(
8282
rangeLength: Int)
8383

8484
extension NSArray {
85+
@nonobjc // FIXME: there should be no need in this attribute.
8586
public func available_getObjects(
8687
_ objects: AutoreleasingUnsafeMutablePointer<AnyObject?>?, range: NSRange
8788
) {
@@ -101,6 +102,7 @@ func _stdlib_NSDictionary_getObjects(
101102
)
102103

103104
extension NSDictionary {
105+
@nonobjc // FIXME: there should be no need in this attribute.
104106
public func available_getObjects(
105107
_ objects: AutoreleasingUnsafeMutablePointer<AnyObject?>?,
106108
andKeys keys: AutoreleasingUnsafeMutablePointer<AnyObject?>?

stdlib/public/SDK/Foundation/Foundation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ extension Array : _ObjectiveCBridgeable {
501501

502502
@_semantics("convertToObjectiveC")
503503
public func _bridgeToObjectiveC() -> NSArray {
504-
return unsafeBitCast(self._buffer._asCocoaArray() as AnyObject, to: NSArray.self)
504+
return unsafeBitCast(self._bridgeToObjectiveCImpl(), to: NSArray.self)
505505
}
506506

507507
public static func _forceBridgeFromObjectiveC(

stdlib/public/core/ArrayBuffer.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
8383

8484
extension _ArrayBuffer {
8585
/// Adopt the storage of `source`.
86-
public init(_ source: NativeBuffer, shiftedToStartIndex: Int) {
86+
public init(_buffer source: NativeBuffer, shiftedToStartIndex: Int) {
8787
_sanityCheck(shiftedToStartIndex == 0, "shiftedToStartIndex must be 0")
8888
_storage = _ArrayBridgeStorage(native: source._storage)
8989
}
@@ -232,7 +232,7 @@ extension _ArrayBuffer {
232232
let boundsCount = bounds.count
233233
if boundsCount == 0 {
234234
return _SliceBuffer(
235-
_ContiguousArrayBuffer<Element>(),
235+
_buffer: _ContiguousArrayBuffer<Element>(),
236236
shiftedToStartIndex: bounds.lowerBound)
237237
}
238238

@@ -262,7 +262,8 @@ extension _ArrayBuffer {
262262
.assumingMemoryBound(to: AnyObject.self),
263263
range: _SwiftNSRange(location: bounds.lowerBound, length: boundsCount))
264264

265-
return _SliceBuffer(result, shiftedToStartIndex: bounds.lowerBound)
265+
return _SliceBuffer(
266+
_buffer: result, shiftedToStartIndex: bounds.lowerBound)
266267
}
267268
set {
268269
fatalError("not implemented")

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public protocol _ArrayBufferProtocol
2424
init()
2525

2626
/// Adopt the entire buffer, presenting it at the provided `startIndex`.
27-
init(_ buffer: _ContiguousArrayBuffer<Element>, shiftedToStartIndex: Int)
27+
init(_buffer: _ContiguousArrayBuffer<Element>, shiftedToStartIndex: Int)
2828

2929
/// Copy the elements in `bounds` from this buffer into uninitialized
3030
/// memory starting at `target`. Return a pointer "past the end" of the

stdlib/public/core/Arrays.swift.gyb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,8 @@ public struct ${Self}<Element>
786786
buffer._copyContents(
787787
subRange: Range(buffer.indices),
788788
initializing: newBuffer.firstElementAddress)
789-
buffer = _Buffer(newBuffer, shiftedToStartIndex: buffer.startIndex)
789+
buffer = _Buffer(
790+
_buffer: newBuffer, shiftedToStartIndex: buffer.startIndex)
790791
}
791792

792793
@_semantics("array.make_mutable")
@@ -885,8 +886,8 @@ public struct ${Self}<Element>
885886
/// Initialization from an existing buffer does not have "array.init"
886887
/// semantics because the caller may retain an alias to buffer.
887888
public // @testable
888-
init(_buffer: _ContiguousArrayBuffer<Element>) {
889-
self.init(_buffer: _Buffer(_buffer, shiftedToStartIndex: 0))
889+
init(_buffer buffer: _ContiguousArrayBuffer<Element>) {
890+
self.init(_buffer: _Buffer(_buffer: buffer, shiftedToStartIndex: 0))
890891
}
891892
%end
892893

@@ -1031,7 +1032,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
10311032

10321033
self = ${Self}(
10331034
_buffer: _Buffer(
1034-
s._copyToContiguousArray()._buffer,
1035+
_buffer: s._copyToContiguousArray()._buffer,
10351036
shiftedToStartIndex: 0))
10361037
}
10371038

@@ -1065,7 +1066,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
10651066
) -> _Buffer {
10661067
let newBuffer = _ContiguousArrayBuffer<Element>(
10671068
uninitializedCount: 0, minimumCapacity: minimumCapacity)
1068-
return _Buffer(newBuffer, shiftedToStartIndex: 0)
1069+
return _Buffer(_buffer: newBuffer, shiftedToStartIndex: 0)
10691070
}
10701071

10711072
/// Construct a ${Self} of `count` uninitialized elements.
@@ -1118,7 +1119,8 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
11181119
storage, to: _ContiguousArrayStorage<Element>.self))
11191120

11201121
return (
1121-
Array(_buffer: _Buffer(innerBuffer, shiftedToStartIndex: 0)),
1122+
Array(
1123+
_buffer: _Buffer(_buffer: innerBuffer, shiftedToStartIndex: 0)),
11221124
innerBuffer.firstElementAddress)
11231125
}
11241126

@@ -1203,7 +1205,8 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
12031205
_buffer._copyContents(
12041206
subRange: Range(_buffer.indices),
12051207
initializing: newBuffer.firstElementAddress)
1206-
_buffer = _Buffer(newBuffer, shiftedToStartIndex: _buffer.startIndex)
1208+
_buffer = _Buffer(
1209+
_buffer: newBuffer, shiftedToStartIndex: _buffer.startIndex)
12071210
}
12081211
_sanityCheck(capacity >= minimumCapacity)
12091212
}
@@ -1933,7 +1936,7 @@ internal func _arrayOutOfPlaceUpdate<_Buffer, Initializer>(
19331936
let tailEnd = source.endIndex
19341937
source._copyContents(subRange: tailStart..<tailEnd, initializing: newEnd)
19351938
}
1936-
source = _Buffer(dest, shiftedToStartIndex: source.startIndex)
1939+
source = _Buffer(_buffer: dest, shiftedToStartIndex: source.startIndex)
19371940
}
19381941

19391942
internal struct _InitializePointer<T> : _PointerFunction {
@@ -2091,6 +2094,11 @@ public func != <Element : Equatable>(
20912094

20922095
#if _runtime(_ObjC)
20932096
extension Array {
2097+
public // @SPI(Foundation)
2098+
func _bridgeToObjectiveCImpl() -> AnyObject {
2099+
return _buffer._asCocoaArray()
2100+
}
2101+
20942102
/// Tries to downcast the source `NSArray` as our native buffer type.
20952103
/// If it succeeds, creates a new `Array` around it and returns that.
20962104
/// Returns `nil` otherwise.
@@ -2154,7 +2162,7 @@ extension ArraySlice {
21542162
init(_startIndex: Int) {
21552163
self.init(
21562164
_buffer: _Buffer(
2157-
ContiguousArray()._buffer,
2165+
_buffer: ContiguousArray()._buffer,
21582166
shiftedToStartIndex: _startIndex))
21592167
}
21602168
}

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
288288
_uncheckedUnsafeBufferObject: _emptyArrayStorage)
289289
}
290290

291-
public init(_ buffer: _ContiguousArrayBuffer, shiftedToStartIndex: Int) {
291+
public init(_buffer buffer: _ContiguousArrayBuffer, shiftedToStartIndex: Int) {
292292
_sanityCheck(shiftedToStartIndex == 0, "shiftedToStartIndex must be 0")
293293
self = buffer
294294
}

stdlib/public/core/SliceBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct _SliceBuffer<Element> : _ArrayBufferProtocol, RandomAccessCollection {
3737
_invariantCheck()
3838
}
3939

40-
public init(_ buffer: NativeBuffer, shiftedToStartIndex: Int) {
40+
public init(_buffer buffer: NativeBuffer, shiftedToStartIndex: Int) {
4141
let shift = buffer.startIndex - shiftedToStartIndex
4242
self.init(
4343
owner: buffer.owner,

test/1_stdlib/BridgeNonVerbatim.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
import Swift
2525
import SwiftShims
26-
import ObjectiveC
26+
import Foundation
2727
import StdlibUnittest
28+
import StdlibUnittestFoundationExtras
2829

2930
struct X : _ObjectiveCBridgeable {
3031
init(_ value: Int) {
@@ -65,20 +66,20 @@ print("testing...")
6566

6667
func testScope() {
6768
let a = [X(1), X(2), X(3)]
68-
let nsx = a._buffer._asCocoaArray()
69+
let nsx: NSArray = a._bridgeToObjectiveC()
6970

7071
// construction of these tracked objects is lazy
7172
// CHECK-NEXT: trackedCount = 0 .
7273
print("trackedCount = \(LifetimeTracked.instances) .")
7374

7475
// We can get a single element out
7576
// CHECK-NEXT: nsx[0]: 1 .
76-
let one = nsx.objectAt(0) as! LifetimeTracked
77+
let one = nsx.object(at: 0) as! LifetimeTracked
7778
print("nsx[0]: \(one.value) .")
7879

7980
// We can get the element again, but it may not have the same identity
8081
// CHECK-NEXT: object identity matches?
81-
let anotherOne = nsx.objectAt(0) as! LifetimeTracked
82+
let anotherOne = nsx.object(at: 0) as! LifetimeTracked
8283
print("object identity matches? \(one === anotherOne)")
8384

8485
// Because the elements come back at +0, we really don't want to
@@ -88,9 +89,10 @@ func testScope() {
8889
objects.withUnsafeMutableBufferPointer {
8990
// FIXME: Can't elide signature and use $0 here <rdar://problem/17770732>
9091
(buf: inout UnsafeMutableBufferPointer<Int>) -> () in
91-
let objPtr = UnsafeMutableRawPointer(buf.baseAddress!).bindMemory(
92-
to: AnyObject.self, capacity: 2)
93-
nsx.getObjects(objPtr, range: _SwiftNSRange(location: 1, length: 2))
92+
nsx.available_getObjects(
93+
AutoreleasingUnsafeMutablePointer(buf.baseAddress!),
94+
range: NSRange(location: 1, length: 2))
95+
return
9496
}
9597

9698
// CHECK-NEXT: getObjects yields them at +0: true

test/Interpreter/SDK/objc_fast_enumeration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ for x in s_m {
9191
// CHECK: 2
9292
// CHECK: 1
9393
var a2 = [3, 2, 1]
94-
var nsa2 = (a2._buffer._asCocoaArray() as AnyObject) as! NSArray
94+
var nsa2: NSArray = a2._bridgeToObjectiveC()
9595
for x in nsa2 {
9696
print(x)
9797
}
@@ -107,7 +107,7 @@ class X : CustomStringConvertible {
107107
// CHECK: X(2)
108108
// CHECK: X(1)
109109
var a3 = [X(3), X(2), X(1)]
110-
var nsa3 = (a3._buffer._asCocoaArray() as AnyObject) as! NSArray
110+
var nsa3: NSArray = a3._bridgeToObjectiveC()
111111
for x in nsa3 {
112112
print(x)
113113
}

validation-test/StdlibUnittest/Assertions.swift.gyb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,36 @@ AssertionsTestSuite.test("expectEqual<T : Equatable, U : Equatable, V : Equatabl
219219
}
220220
}
221221

222+
AssertionsTestSuite.test("expectEqual<T : Equatable, U : Equatable, V : Equatable, W : Equatable>") {
223+
let _0 = MinimalEquatableValue(0)
224+
let _1 = MinimalEquatableValue(1)
225+
226+
for a in [_0, _1] {
227+
for b in [_0, _1] {
228+
for c in [_0, _1] {
229+
for d in [_0, _1] {
230+
for e in [_0, _1] {
231+
for f in [_0, _1] {
232+
for g in [_0, _1] {
233+
for h in [_0, _1] {
234+
let lhs = (a, b, c, d)
235+
let rhs = (e, f, g, h)
236+
if lhs == rhs {
237+
expectEqual(lhs, rhs)
238+
} else {
239+
expectFailure {
240+
expectEqual(lhs, rhs)
241+
}
242+
}
243+
}
244+
}
245+
}
246+
}
247+
}
248+
}
249+
}
250+
}
251+
}
252+
222253
runAllTests()
223254

validation-test/compiler_crashers_2_fixed/0022-rdar21625478.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public protocol MySequence {
3535
_ preprocess: (Self) -> R
3636
) -> R?
3737

38-
func _copyToNativeArrayBuffer()
39-
-> _ContiguousArrayBuffer<Iterator.Element>
38+
func _copyToContiguousArray()
39+
-> ContiguousArray<Iterator.Element>
4040

4141
func _copyContents(
4242
initializing ptr: UnsafeMutablePointer<Iterator.Element>
@@ -71,8 +71,8 @@ extension MySequence {
7171
return nil
7272
}
7373

74-
public func _copyToNativeArrayBuffer()
75-
-> _ContiguousArrayBuffer<Iterator.Element> {
74+
public func _copyToContiguousArray()
75+
-> ContiguousArray<Iterator.Element> {
7676
fatalError()
7777
}
7878

@@ -224,7 +224,7 @@ public class SequenceLog {
224224
public static var filter = TypeIndexed(0)
225225
public static var _customContainsEquatableElement = TypeIndexed(0)
226226
public static var _preprocessingPass = TypeIndexed(0)
227-
public static var _copyToNativeArrayBuffer = TypeIndexed(0)
227+
public static var _copyToContiguousArray = TypeIndexed(0)
228228
public static var _copyContents = TypeIndexed(0)
229229
}
230230

@@ -281,10 +281,10 @@ extension LoggingSequenceType
281281

282282
/// Create a native array buffer containing the elements of `self`,
283283
/// in the same order.
284-
public func _copyToNativeArrayBuffer()
285-
-> _ContiguousArrayBuffer<Base.Iterator.Element> {
286-
Log._copyToNativeArrayBuffer[selfType] += 1
287-
return base._copyToNativeArrayBuffer()
284+
public func _copyToContiguousArray()
285+
-> ContiguousArray<Base.Iterator.Element> {
286+
Log._copyToContiguousArray[selfType] += 1
287+
return base._copyToContiguousArray()
288288
}
289289

290290
/// Copy a Sequence into an array.

0 commit comments

Comments
 (0)