Skip to content

Commit 8494217

Browse files
authored
Revert "SE-0138: Proposed amendment to SE-0138: Normalize UnsafeRawBufferPointer Slices (#8222)"
This reverts commit 1d32586.
1 parent 1d32586 commit 8494217

16 files changed

+373
-700
lines changed

CHANGELOG.md

-42
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,6 @@ CHANGELOG
2121
Swift 4.0
2222
---------
2323

24-
* [SE-0138](https://github.com/apple/swift-evolution/blob/master/proposals/0138-unsaferawbufferpointer.md#amendment-to-normalize-the-slice-type):
25-
26-
Slicing a raw buffer no longer results in the same raw buffer
27-
type. Specifically, `Unsafe[Mutable]BufferPointer.SubSequence` now has type
28-
`[Mutable]RandomAccessSlice<Unsafe[Mutable]RawBufferPointer>`. Therefore,
29-
indexing into a raw buffer slice is no longer zero-based. This is required for
30-
raw buffers to fully conform to generic `Collection`. Changing the slice type
31-
resulted in the following behavioral changes:
32-
33-
Passing a region within buffer to another function that takes a buffer can no
34-
longer be done via subscript:
35-
36-
Incorrect: `takesRawBuffer(buffer[i..<j])`
37-
38-
This now requires explicit initialization, using a `rebasing:` initializer,
39-
which converts from a slice to a zero-based `Unsafe[Mutable]RawBufferPointer`:
40-
41-
Correct: `takesRawBuffer(UnsafeRawBufferPointer(rebasing: buffer[i..<j]))`
42-
43-
Subscript assignment directly from a buffer no longer compiles:
44-
45-
Incorrect: `buffer[n..<m] = smaller_buffer`
46-
47-
This now requires creation of a slice from the complete source buffer:
48-
49-
Correct: `buffer[n..<m] = smaller_buffer.suffix(from: 0)`
50-
51-
`UnsafeRawBufferPointer`'s slice type no longer has a nonmutating subscript
52-
setter. So assigning into a mutable `let` buffer no longer compiles:
53-
54-
```
55-
let slice = buffer[n..<m]
56-
slice[i..<j] = buffer[k..<l]
57-
```
58-
59-
The assigned buffer slice now needs to be a `var`.
60-
61-
```
62-
var slice = buffer[n..<m]
63-
slice[i..<j] = buffer[k..<l]
64-
```
65-
6624
* [SR-1529](https://bugs.swift.org/browse/SR-1529):
6725

6826
Covariant method overrides are now fully supported, fixing many crashes

stdlib/private/StdlibCollectionUnittest/CheckCollectionInstance.swift.gyb

+42-125
Original file line numberDiff line numberDiff line change
@@ -269,104 +269,18 @@ public func checkAdvancesAndDistances<Instances, Distances, BaseCollection>(
269269
% ('Expected: Collection', 'Expected.Iterator.Element', 'Expected'),
270270
% ('Element' , 'Element' , 'Array<Element>')]:
271271

272-
// Top-level check for Collection instances. Alias for checkForwardCollection.
273-
// Checks all slices: O(n^2).
274-
public func checkCollection<${genericParam}, C : Collection>(
275-
_ expected: ${Expected},
276-
_ collection: C,
277-
${TRACE},
278-
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
279-
sameValue: (${Element}, ${Element}) -> Bool
280-
) where C.Iterator.Element == ${Element},
281-
// FIXME(ABI) (Associated Types with where clauses): these constraints should be applied to
282-
// associated types of Collection.
283-
C.Indices.Iterator.Element == C.Index,
284-
C.SubSequence : Collection,
285-
C.SubSequence.Iterator.Element == ${Element},
286-
C.SubSequence.Indices.Iterator.Element == C.Index,
287-
C.SubSequence.Index == C.Index {
288-
289-
checkForwardCollection(expected, collection, message(),
290-
stackTrace: stackTrace, showFrame: showFrame, file: file, line: line,
291-
resiliencyChecks: resiliencyChecks,
292-
sameValue: sameValue)
293-
}
294-
295272
% for Traversal in TRAVERSALS:
296273
% TraversalCollection = collectionForTraversal(Traversal)
297274

298-
// Calls check${Traversal}Collection with default `sameValue`.
299275
public func check${Traversal}Collection<
300276
${genericParam}, C : ${TraversalCollection}
301-
>(
302-
_ expected: ${Expected}, _ collection: C,
303-
${TRACE},
304-
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
305-
) where
306-
C.Iterator.Element == ${Element},
307-
C.Indices.Iterator.Element == C.Index,
308-
C.SubSequence : ${TraversalCollection},
309-
C.SubSequence.Iterator.Element == ${Element},
310-
C.SubSequence.Indices.Iterator.Element == C.Index,
311-
C.SubSequence.Index == C.Index,
312-
${Element} : Equatable {
313-
314-
check${Traversal}Collection(
315-
expected, collection, ${trace},
316-
resiliencyChecks: resiliencyChecks) { $0 == $1 }
317-
}
318-
319-
// Top-Level check for all ${TraversalCollection} semantics on a single
320-
// instance. This constrains SubSequence associated types in order to check
321-
// slice semantics.
322-
// Checks all slices: O(n^2).
323-
public func check${Traversal}Collection<
324-
${genericParam}, C : ${TraversalCollection}
325-
>(
326-
_ expected: ${Expected}, _ collection: C,
327-
${TRACE},
328-
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
329-
sameValue: (${Element}, ${Element}) -> Bool
330-
) where
331-
C.Iterator.Element == ${Element},
332-
// FIXME(ABI) (Associated Types with where clauses): these constraints should be applied to
333-
// associated types of Collection.
334-
C.Indices.Iterator.Element == C.Index,
335-
C.SubSequence : ${TraversalCollection},
336-
C.SubSequence.Iterator.Element == ${Element},
337-
C.SubSequence.Index == C.Index,
338-
C.SubSequence.Indices.Iterator.Element == C.Index {
339-
340-
checkOneLevelOf${Traversal}Collection(expected, collection, ${trace},
341-
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
342-
343-
// Avoid validation of all possible (n^2) slices on large collection.
344-
// Test cases should call checkOneLevelOf${Traversal}Collection instead.
345-
expectLT(expected.count, 30)
346-
347-
_checkSliceableWith${Traversal}Index(expected, collection, ${trace},
348-
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
349-
}
350-
351-
// Helper for check${Traversal}Collection. Check that instance of `C`,
352-
// `collection`, upholds the semantics of `${TraversalCollection}`,
353-
// non-recursively. This does not check subsequences. It may be called for each
354-
// subsequence without combinatorial explosion. Also, since recursive protocol
355-
// contraints are not supported, our second level of checks cannot depend on the
356-
// associated type properties of SubSequence.
357-
//
358-
// Checks all slices: O(n^2).
359-
public func checkOneLevelOf${Traversal}Collection<
360-
${genericParam}, C : ${TraversalCollection}
361277
>(
362278
_ expected: ${Expected}, _ collection: C,
363279
${TRACE},
364280
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
365281
sameValue: (${Element}, ${Element}) -> Bool
366282
) where
367283
C.Iterator.Element == ${Element},
368-
// FIXME(ABI) (Associated Types with where clauses): these constraints should be applied to
369-
// associated types of Collection.
370284
C.Indices.Iterator.Element == C.Index {
371285

372286
// A `Collection` is a multi-pass `Sequence`.
@@ -376,10 +290,6 @@ public func checkOneLevelOf${Traversal}Collection<
376290
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
377291
}
378292

379-
//===------------------------------------------------------------------===//
380-
// Check Index semantics
381-
//===------------------------------------------------------------------===//
382-
383293
let succ = { collection.index(after: $0) }
384294
% if Traversal != 'Forward':
385295
let pred = { collection.index(before: $0) }
@@ -446,14 +356,9 @@ public func checkOneLevelOf${Traversal}Collection<
446356

447357
let expectedArray = Array(expected)
448358

449-
// Check `count`.
450359
expectEqual(
451360
expectedArray.count.toIntMax(), collection.count.toIntMax(), ${trace})
452361

453-
//===------------------------------------------------------------------===//
454-
// Check Iteration behavior.
455-
//===------------------------------------------------------------------===//
456-
457362
for _ in 0..<3 {
458363
do {
459364
let startIndex = collection.startIndex
@@ -509,7 +414,7 @@ public func checkOneLevelOf${Traversal}Collection<
509414
expectedArray[i], collection[index], ${trace}, sameValue: sameValue)
510415
}
511416

512-
% end # Traversal == "Bidirectional"
417+
% end
513418

514419
} // end of `if expectedArray.count >= 2`
515420

@@ -535,59 +440,65 @@ public func checkOneLevelOf${Traversal}Collection<
535440
// FIXME: more checks for bidirectional and random access collections.
536441
}
537442

538-
// Helper for check${Traversal}Collection to check Slices.
539-
//
540-
// Checks all slices: O(n^2).
541-
internal func _checkSliceableWith${Traversal}Index<
542-
${genericParam}, S : ${TraversalCollection}
443+
public func check${Traversal}Collection<
444+
${genericParam}, C : ${TraversalCollection}
543445
>(
544-
_ expected: ${Expected}, _ sliceable: S, ${TRACE},
545-
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
546-
sameValue: (${Element}, ${Element}) -> Bool
446+
_ expected: ${Expected}, _ collection: C,
447+
${TRACE},
448+
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
449+
) where
450+
C.Iterator.Element == ${Element},
451+
C.Indices.Iterator.Element == C.Index,
452+
${Element} : Equatable {
453+
454+
check${Traversal}Collection(
455+
expected, collection, ${trace},
456+
resiliencyChecks: resiliencyChecks) { $0 == $1 }
457+
}
458+
459+
% end
460+
461+
// FIXME: merge into checkCollection()
462+
public func checkSliceableWithBidirectionalIndex<
463+
${genericParam}, S : BidirectionalCollection
464+
>(
465+
_ expected: ${Expected}, _ sliceable: S, ${TRACE}
547466
) where
548467
S.Iterator.Element == ${Element},
549-
S.SubSequence : ${TraversalCollection},
468+
S.Indices.Iterator.Element == S.Index,
469+
S.SubSequence : BidirectionalCollection,
550470
S.SubSequence.Iterator.Element == ${Element},
551-
S.SubSequence.Index == S.Index,
552-
S.SubSequence.Indices.Iterator.Element == S.Index {
471+
S.SubSequence.Indices.Iterator.Element == S.SubSequence.Index,
472+
${Element} : Equatable {
473+
474+
// A `Sliceable` is a `Collection`.
475+
checkBidirectionalCollection(expected, sliceable, ${trace})
553476

554477
let expectedArray = Array(expected)
555478

556479
let succ = { sliceable.index(after: $0) }
557-
% if Traversal != "Forward":
558480
let pred = { sliceable.index(before: $0) }
559-
% end
560481

561482
var start = sliceable.startIndex
562483
for startNumericIndex in 0...expectedArray.count {
563-
% if Traversal != "Forward":
564484
if start != sliceable.endIndex {
565485
start = succ(start)
566486
start = pred(start)
567487
start = succ(start)
568488
start = pred(start)
569489
}
570-
% end
571490
var end = start
572491
for endNumericIndex in startNumericIndex...expectedArray.count {
573-
% if Traversal != "Forward":
574492
if end != sliceable.endIndex {
575493
end = succ(end)
576494
end = pred(end)
577495
end = succ(end)
578496
end = pred(end)
579497
}
580-
% end
581498
let expectedSlice = expectedArray[startNumericIndex..<endNumericIndex]
582499
let slice = sliceable[start..<end]
583-
// For every possible slice, verify that the slice's bounds are identical
584-
// to the indices used to form the slice.
585-
expectEqual(start, slice.startIndex)
586-
expectEqual(end, slice.endIndex)
587500

588-
checkOneLevelOf${Traversal}Collection(expectedSlice, slice, ${trace},
589-
resiliencyChecks: resiliencyChecks,
590-
sameValue: sameValue)
501+
checkBidirectionalCollection(expectedSlice, slice, ${trace})
591502

592503
if end != sliceable.endIndex {
593504
end = succ(end)
@@ -599,13 +510,9 @@ ${genericParam}, S : ${TraversalCollection}
599510
}
600511
}
601512

602-
% end # Traversal
513+
% end
603514

604-
% end # genericParam, Elements, Expected
605515

606-
// Check RangeReplaceableCollection using a factory.
607-
//
608-
// Note: This does not invoke other collection tests.
609516
public func checkRangeReplaceable<C, N>(
610517
_ makeCollection: @escaping () -> C,
611518
_ makeNewValues: (Int) -> N
@@ -667,3 +574,13 @@ public func checkRangeReplaceable<C, N>(
667574
}
668575
}
669576
}
577+
578+
public func checkCollection<C : Collection, Expected : Collection>(
579+
_ subject: C,
580+
expected: Expected,
581+
${TRACE},
582+
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
583+
sameValue: (Expected.Iterator.Element, Expected.Iterator.Element) -> Bool
584+
) where C.Iterator.Element == Expected.Iterator.Element {
585+
}
586+

0 commit comments

Comments
 (0)