mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Since this code was first written, we've added more language features that introduce the opportunity for a materializeForSet protocol witness to have an incompatible polymorphic convention with its concrete implementation: - In a conditional conformance, if a witness comes from a constrained extension with additional protocol requirements, then the witness will require those conformances as additional polymorphic arguments, making its materializeForSet uncallable from code using the protocol witness. - Given a subscript requirement, the witness may be a generic subscript with a more general signature than the witness, making the generic arguments to the concrete materializeForSet callback incompatible with those expected for the witness. Longer term, representing materializeForSet patterns using accessor coroutines should obviate the need for this hack. For now, it's necessary for correctness, addressing rdar://problem/35760754.
130 lines
4.1 KiB
Swift
130 lines
4.1 KiB
Swift
// -*- swift -*-
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Automatically Generated From validation-test/stdlib/Slice/Inputs/Template.swift.gyb
|
|
// Do Not Edit Directly!
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RUN: %target-run-simple-swift
|
|
// REQUIRES: executable_test
|
|
|
|
// FIXME: the test is too slow when the standard library is not optimized.
|
|
// REQUIRES: optimized_stdlib
|
|
|
|
import StdlibUnittest
|
|
import StdlibCollectionUnittest
|
|
|
|
var SliceTests = TestSuite("Collection")
|
|
|
|
let prefix: [Int] = []
|
|
let suffix: [Int] = []
|
|
|
|
func makeCollection(elements: [OpaqueValue<Int>])
|
|
-> MutableSlice<MinimalMutableCollection<OpaqueValue<Int>>> {
|
|
var baseElements = prefix.map(OpaqueValue.init)
|
|
baseElements.append(contentsOf: elements)
|
|
baseElements.append(contentsOf: suffix.map(OpaqueValue.init))
|
|
let base = MinimalMutableCollection(elements: baseElements)
|
|
let startIndex = base.index(
|
|
base.startIndex,
|
|
offsetBy: numericCast(prefix.count))
|
|
let endIndex = base.index(
|
|
base.startIndex,
|
|
offsetBy: numericCast(prefix.count + elements.count))
|
|
return MutableSlice(
|
|
base: base,
|
|
bounds: startIndex..<endIndex)
|
|
}
|
|
|
|
func makeCollectionOfEquatable(elements: [MinimalEquatableValue])
|
|
-> MutableSlice<MinimalMutableCollection<MinimalEquatableValue>> {
|
|
var baseElements = prefix.map(MinimalEquatableValue.init)
|
|
baseElements.append(contentsOf: elements)
|
|
baseElements.append(contentsOf: suffix.map(MinimalEquatableValue.init))
|
|
let base = MinimalMutableCollection(elements: baseElements)
|
|
let startIndex = base.index(
|
|
base.startIndex,
|
|
offsetBy: numericCast(prefix.count))
|
|
let endIndex = base.index(
|
|
base.startIndex,
|
|
offsetBy: numericCast(prefix.count + elements.count))
|
|
return MutableSlice(
|
|
base: base,
|
|
bounds: startIndex..<endIndex)
|
|
}
|
|
|
|
func makeCollectionOfComparable(elements: [MinimalComparableValue])
|
|
-> MutableSlice<MinimalMutableCollection<MinimalComparableValue>> {
|
|
var baseElements = prefix.map(MinimalComparableValue.init)
|
|
baseElements.append(contentsOf: elements)
|
|
baseElements.append(contentsOf: suffix.map(MinimalComparableValue.init))
|
|
let base = MinimalMutableCollection(elements: baseElements)
|
|
let startIndex = base.index(
|
|
base.startIndex,
|
|
offsetBy: numericCast(prefix.count))
|
|
let endIndex = base.index(
|
|
base.startIndex,
|
|
offsetBy: numericCast(prefix.count + elements.count))
|
|
return MutableSlice(
|
|
base: base,
|
|
bounds: startIndex..<endIndex)
|
|
}
|
|
|
|
var resiliencyChecks = CollectionMisuseResiliencyChecks.all
|
|
resiliencyChecks.creatingOutOfBoundsIndicesBehavior = .trap
|
|
resiliencyChecks.subscriptOnOutOfBoundsIndicesBehavior = .trap
|
|
resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior = .trap
|
|
|
|
SliceTests.addMutableCollectionTests(
|
|
"MutableSlice_Of_MinimalMutableCollection_FullWidth.swift.",
|
|
makeCollection: makeCollection,
|
|
wrapValue: identity,
|
|
extractValue: identity,
|
|
makeCollectionOfEquatable: makeCollectionOfEquatable,
|
|
wrapValueIntoEquatable: identityEq,
|
|
extractValueFromEquatable: identityEq,
|
|
makeCollectionOfComparable: makeCollectionOfComparable,
|
|
wrapValueIntoComparable: identityComp,
|
|
extractValueFromComparable: identityComp,
|
|
resiliencyChecks: resiliencyChecks,
|
|
outOfBoundsIndexOffset: 6
|
|
, withUnsafeMutableBufferPointerIsSupported: false,
|
|
isFixedLengthCollection: true
|
|
)
|
|
|
|
// rdar://problem/35760754
|
|
|
|
func f<C : MutableCollection>(
|
|
_ elements: inout C
|
|
) { }
|
|
|
|
var CrashTests = TestSuite("MutableSliceCrash")
|
|
|
|
extension TestSuite {
|
|
public func addMyMutableCollectionTests<C : MutableCollection>(
|
|
_ testNamePrefix: String = "",
|
|
maker: @escaping ([C.Element]) -> C
|
|
) {
|
|
|
|
// this runs just fine
|
|
self.test("\(testNamePrefix).Direct") {
|
|
var c = makeCollection(elements: [])
|
|
f(&c[c.startIndex..<c.endIndex])
|
|
}
|
|
|
|
// this used to crash, even though the only difference is it's calling
|
|
// makeCollection via the argument to the original function
|
|
self.test("\(testNamePrefix).Indirect") {
|
|
var c = maker([])
|
|
f(&c[c.startIndex..<c.endIndex])
|
|
}
|
|
}
|
|
}
|
|
|
|
CrashTests.addMyMutableCollectionTests(
|
|
"Crasher",
|
|
maker: makeCollection
|
|
)
|
|
|
|
runAllTests()
|