Skip to content

Commit 987acd3

Browse files
committed
stdlib: make tests rely on Array implementation details less
1 parent f45565c commit 987acd3

File tree

7 files changed

+99
-91
lines changed

7 files changed

+99
-91
lines changed

stdlib/private/StdlibUnittestFoundationExtras/StdlibUnittestFoundationExtras.swift

+2
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?>?

test/1_stdlib/BridgeNonVerbatim.swift

+9-7
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

+2-2
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/compiler_crashers_2_fixed/0022-rdar21625478.swift

+9-9
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.

validation-test/stdlib/ArrayNew.swift.gyb

+25-34
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,14 @@ import StdlibCollectionUnittest
1919
all_array_types = ['ContiguousArray', 'ArraySlice', 'Array']
2020
}%
2121

22-
extension Array {
23-
var identity: UnsafeRawPointer {
24-
return self._buffer.identity
25-
}
26-
}
27-
28-
extension ArraySlice {
29-
var identity: UnsafeRawPointer {
30-
return self._buffer.identity
31-
}
32-
}
33-
34-
extension ContiguousArray {
35-
var identity: UnsafeRawPointer {
36-
return self._buffer.identity
22+
%for Self in all_array_types:
23+
extension ${Self} {
24+
typealias _BufferID = UnsafeRawPointer?
25+
var _bufferID: _BufferID {
26+
return unsafeBitCast(_owner, to: _BufferID.self)
3727
}
3828
}
29+
%end
3930

4031
var ArrayTestSuite = TestSuite("Array")
4132

@@ -64,21 +55,21 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Int)/COW") {
6455
${element_type}(40), ${element_type}(50), ${element_type}(60),
6556
${element_type}(70)
6657
]]
67-
let identityOuter = a.identity
68-
var identityInner = a[0].identity
58+
let identityOuter = a._bufferID
59+
var identityInner = a[0]._bufferID
6960

7061
func checkIdentity(_ stackTrace: SourceLocStack) {
7162
% if element_type == 'TestValueTy':
7263
// Does not reallocate storage because we changed a property based on a
7364
// reference; array storage was not changed. Writeback of the inner array
7465
// does not happen.
75-
expectEqual(identityOuter, a.identity, stackTrace: stackTrace)
76-
expectEqual(identityInner, a[0].identity, stackTrace: stackTrace)
66+
expectEqual(identityOuter, a._bufferID, stackTrace: stackTrace)
67+
expectEqual(identityInner, a[0]._bufferID, stackTrace: stackTrace)
7768
% else:
78-
expectEqual(identityOuter, a.identity, stackTrace: stackTrace)
69+
expectEqual(identityOuter, a._bufferID, stackTrace: stackTrace)
7970

8071
// Should not reallocate storage.
81-
expectEqual(identityInner, a[0].identity, stackTrace: stackTrace)
72+
expectEqual(identityInner, a[0]._bufferID, stackTrace: stackTrace)
8273
% end
8374
}
8475

@@ -134,33 +125,33 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Range<Int>)/COW
134125
${element_type}(40), ${element_type}(50), ${element_type}(60),
135126
${element_type}(70), ${element_type}(80), ${element_type}(90),
136127
]]
137-
let identityOuter = a.identity
138-
var identityInner = a[0].identity
128+
let identityOuter = a._bufferID
129+
var identityInner = a[0]._bufferID
139130

140131
func checkIdentity(_ stackTrace: SourceLocStack) {
141132
% if element_type == 'TestValueTy':
142133
// Does not reallocate storage because we changed a property based on a
143134
// reference; array storage was not changed.
144-
expectEqual(identityOuter, a.identity, stackTrace: stackTrace)
145-
expectEqual(identityInner, a[0].identity, stackTrace: stackTrace)
135+
expectEqual(identityOuter, a._bufferID, stackTrace: stackTrace)
136+
expectEqual(identityInner, a[0]._bufferID, stackTrace: stackTrace)
146137
% else:
147-
expectEqual(identityOuter, a.identity, stackTrace: stackTrace)
138+
expectEqual(identityOuter, a._bufferID, stackTrace: stackTrace)
148139
// Writeback happens in subscript(Range<Int>), but array notices that the new
149140
// value did not change.
150141
// Another writeback happens in Array.subscript(Int), but this is not what we
151142
// want.
152-
expectNotEqual(identityInner, a[0].identity, stackTrace: stackTrace)
153-
identityInner = a[0].identity
143+
expectNotEqual(identityInner, a[0]._bufferID, stackTrace: stackTrace)
144+
identityInner = a[0]._bufferID
154145
% end
155146
}
156147

157148
// Mutating through a subscript expression.
158149
a[0..<1][0][0] = ${element_type}(1010)
159150

160151
// Reallocates storage because of the writeback in Array.subscript(Int).
161-
expectEqual(identityOuter, a.identity)
162-
expectNotEqual(identityInner, a[0].identity)
163-
identityInner = a[0].identity
152+
expectEqual(identityOuter, a._bufferID)
153+
expectNotEqual(identityInner, a[0]._bufferID)
154+
identityInner = a[0]._bufferID
164155

165156
a[0..<1][0][1].value = 1020
166157

@@ -195,9 +186,9 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Range<Int>)/COW
195186
// Reallocates storage because of the writeback in Array.subscript(Int)
196187
// (writeback is being requested for the array element even though it is not
197188
// needed).
198-
expectEqual(identityOuter, a.identity)
199-
expectNotEqual(identityInner, a[0].identity)
200-
identityInner = a[0].identity
189+
expectEqual(identityOuter, a._bufferID)
190+
expectNotEqual(identityInner, a[0]._bufferID)
191+
identityInner = a[0]._bufferID
201192

202193
withInoutT(&a[0..<1][0][6].value) {
203194
(x: inout Int) in

validation-test/stdlib/Arrays.swift.gyb

+8-17
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,14 @@ CopyToNativeArrayBufferTests.test("Collection._copyToContiguousArray()") {
7373
all_array_types = ['ContiguousArray', 'ArraySlice', 'Array']
7474
}%
7575

76-
extension Array {
77-
var identity: UnsafeRawPointer {
78-
return self._buffer.identity
79-
}
80-
}
81-
82-
extension ArraySlice {
83-
var identity: UnsafeRawPointer {
84-
return self._buffer.identity
85-
}
86-
}
87-
88-
extension ContiguousArray {
89-
var identity: UnsafeRawPointer {
90-
return self._buffer.identity
76+
%for Self in all_array_types:
77+
extension ${Self} {
78+
typealias _BufferID = UnsafeRawPointer?
79+
var _bufferID: _BufferID {
80+
return unsafeBitCast(_owner, to: _BufferID.self)
9181
}
9282
}
83+
%end
9384

9485
var ArrayTestSuite = TestSuite("Array")
9586

@@ -246,10 +237,10 @@ ArrayTestSuite.test("${array_type}/emptyAllocation") {
246237
let arr0 = ${array_type}<Int>()
247238
let arr1 = ${array_type}<LifetimeTracked>(repeating: LifetimeTracked(0), count: 0)
248239
// Empty arrays all use the same buffer
249-
expectEqual(arr0._buffer.identity, arr1._buffer.identity)
240+
expectEqual(arr0._bufferID, arr1._bufferID)
250241

251242
let arr2: ${array_type}<LifetimeTracked> = []
252-
let emptyLiteralsShareBuffer = arr0._buffer.identity == arr2._buffer.identity
243+
let emptyLiteralsShareBuffer = arr0._bufferID == arr2._bufferID
253244
expectTrue(emptyLiteralsShareBuffer)
254245
}
255246

0 commit comments

Comments
 (0)