@@ -19,23 +19,14 @@ import StdlibCollectionUnittest
19
19
all_array_types = ['ContiguousArray', 'ArraySlice', 'Array']
20
20
}%
21
21
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)
37
27
}
38
28
}
29
+ %end
39
30
40
31
var ArrayTestSuite = TestSuite("Array")
41
32
@@ -64,21 +55,21 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Int)/COW") {
64
55
${element_type}(40), ${element_type}(50), ${element_type}(60),
65
56
${element_type}(70)
66
57
]]
67
- let identityOuter = a.identity
68
- var identityInner = a[0].identity
58
+ let identityOuter = a._bufferID
59
+ var identityInner = a[0]._bufferID
69
60
70
61
func checkIdentity(_ stackTrace: SourceLocStack) {
71
62
% if element_type == 'TestValueTy':
72
63
// Does not reallocate storage because we changed a property based on a
73
64
// reference; array storage was not changed. Writeback of the inner array
74
65
// 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)
77
68
% else:
78
- expectEqual(identityOuter, a.identity , stackTrace: stackTrace)
69
+ expectEqual(identityOuter, a._bufferID , stackTrace: stackTrace)
79
70
80
71
// Should not reallocate storage.
81
- expectEqual(identityInner, a[0].identity , stackTrace: stackTrace)
72
+ expectEqual(identityInner, a[0]._bufferID , stackTrace: stackTrace)
82
73
% end
83
74
}
84
75
@@ -134,33 +125,33 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Range<Int>)/COW
134
125
${element_type}(40), ${element_type}(50), ${element_type}(60),
135
126
${element_type}(70), ${element_type}(80), ${element_type}(90),
136
127
]]
137
- let identityOuter = a.identity
138
- var identityInner = a[0].identity
128
+ let identityOuter = a._bufferID
129
+ var identityInner = a[0]._bufferID
139
130
140
131
func checkIdentity(_ stackTrace: SourceLocStack) {
141
132
% if element_type == 'TestValueTy':
142
133
// Does not reallocate storage because we changed a property based on a
143
134
// 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)
146
137
% else:
147
- expectEqual(identityOuter, a.identity , stackTrace: stackTrace)
138
+ expectEqual(identityOuter, a._bufferID , stackTrace: stackTrace)
148
139
// Writeback happens in subscript(Range<Int>), but array notices that the new
149
140
// value did not change.
150
141
// Another writeback happens in Array.subscript(Int), but this is not what we
151
142
// 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
154
145
% end
155
146
}
156
147
157
148
// Mutating through a subscript expression.
158
149
a[0..<1][0][0] = ${element_type}(1010)
159
150
160
151
// 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
164
155
165
156
a[0..<1][0][1].value = 1020
166
157
@@ -195,9 +186,9 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Range<Int>)/COW
195
186
// Reallocates storage because of the writeback in Array.subscript(Int)
196
187
// (writeback is being requested for the array element even though it is not
197
188
// 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
201
192
202
193
withInoutT(&a[0..<1][0][6].value) {
203
194
(x: inout Int) in
0 commit comments