Skip to content

stdlib: make Array implementation internal #4083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public func expectEqual<T : Equatable, U : Equatable, V : Equatable>(
expectEqualTest(expected.2, actual.2, ${trace}, showFrame: false) {$0 == $1}
}

public func expectEqual<T : Equatable, U : Equatable, V : Equatable, W : Equatable>(
_ expected: (T, U, V, W), _ actual: (T, U, V, W), ${TRACE}) {
expectEqualTest(expected.0, actual.0, ${trace}, showFrame: false) {$0 == $1}
expectEqualTest(expected.1, actual.1, ${trace}, showFrame: false) {$0 == $1}
expectEqualTest(expected.2, actual.2, ${trace}, showFrame: false) {$0 == $1}
expectEqualTest(expected.3, actual.3, ${trace}, showFrame: false) {$0 == $1}
}

public func expectationFailure(
_ reason: String,
trace message: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ internal func _stdlib_NSArray_getObjects(
rangeLength: Int)

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

extension NSDictionary {
@nonobjc // FIXME: there should be no need in this attribute.
public func available_getObjects(
_ objects: AutoreleasingUnsafeMutablePointer<AnyObject?>?,
andKeys keys: AutoreleasingUnsafeMutablePointer<AnyObject?>?
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ extension Array : _ObjectiveCBridgeable {

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

public static func _forceBridgeFromObjectiveC(
Expand Down
7 changes: 4 additions & 3 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {

extension _ArrayBuffer {
/// Adopt the storage of `source`.
public init(_ source: NativeBuffer, shiftedToStartIndex: Int) {
public init(_buffer source: NativeBuffer, shiftedToStartIndex: Int) {
_sanityCheck(shiftedToStartIndex == 0, "shiftedToStartIndex must be 0")
_storage = _ArrayBridgeStorage(native: source._storage)
}
Expand Down Expand Up @@ -232,7 +232,7 @@ extension _ArrayBuffer {
let boundsCount = bounds.count
if boundsCount == 0 {
return _SliceBuffer(
_ContiguousArrayBuffer<Element>(),
_buffer: _ContiguousArrayBuffer<Element>(),
shiftedToStartIndex: bounds.lowerBound)
}

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

return _SliceBuffer(result, shiftedToStartIndex: bounds.lowerBound)
return _SliceBuffer(
_buffer: result, shiftedToStartIndex: bounds.lowerBound)
}
set {
fatalError("not implemented")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ArrayBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public protocol _ArrayBufferProtocol
init()

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

/// Copy the elements in `bounds` from this buffer into uninitialized
/// memory starting at `target`. Return a pointer "past the end" of the
Expand Down
26 changes: 17 additions & 9 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,8 @@ public struct ${Self}<Element>
buffer._copyContents(
subRange: Range(buffer.indices),
initializing: newBuffer.firstElementAddress)
buffer = _Buffer(newBuffer, shiftedToStartIndex: buffer.startIndex)
buffer = _Buffer(
_buffer: newBuffer, shiftedToStartIndex: buffer.startIndex)
}

@_semantics("array.make_mutable")
Expand Down Expand Up @@ -885,8 +886,8 @@ public struct ${Self}<Element>
/// Initialization from an existing buffer does not have "array.init"
/// semantics because the caller may retain an alias to buffer.
public // @testable
init(_buffer: _ContiguousArrayBuffer<Element>) {
self.init(_buffer: _Buffer(_buffer, shiftedToStartIndex: 0))
init(_buffer buffer: _ContiguousArrayBuffer<Element>) {
self.init(_buffer: _Buffer(_buffer: buffer, shiftedToStartIndex: 0))
}
%end

Expand Down Expand Up @@ -1031,7 +1032,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {

self = ${Self}(
_buffer: _Buffer(
s._copyToContiguousArray()._buffer,
_buffer: s._copyToContiguousArray()._buffer,
shiftedToStartIndex: 0))
}

Expand Down Expand Up @@ -1065,7 +1066,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
) -> _Buffer {
let newBuffer = _ContiguousArrayBuffer<Element>(
uninitializedCount: 0, minimumCapacity: minimumCapacity)
return _Buffer(newBuffer, shiftedToStartIndex: 0)
return _Buffer(_buffer: newBuffer, shiftedToStartIndex: 0)
}

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

return (
Array(_buffer: _Buffer(innerBuffer, shiftedToStartIndex: 0)),
Array(
_buffer: _Buffer(_buffer: innerBuffer, shiftedToStartIndex: 0)),
innerBuffer.firstElementAddress)
}

Expand Down Expand Up @@ -1203,7 +1205,8 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
_buffer._copyContents(
subRange: Range(_buffer.indices),
initializing: newBuffer.firstElementAddress)
_buffer = _Buffer(newBuffer, shiftedToStartIndex: _buffer.startIndex)
_buffer = _Buffer(
_buffer: newBuffer, shiftedToStartIndex: _buffer.startIndex)
}
_sanityCheck(capacity >= minimumCapacity)
}
Expand Down Expand Up @@ -1933,7 +1936,7 @@ internal func _arrayOutOfPlaceUpdate<_Buffer, Initializer>(
let tailEnd = source.endIndex
source._copyContents(subRange: tailStart..<tailEnd, initializing: newEnd)
}
source = _Buffer(dest, shiftedToStartIndex: source.startIndex)
source = _Buffer(_buffer: dest, shiftedToStartIndex: source.startIndex)
}

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

#if _runtime(_ObjC)
extension Array {
public // @SPI(Foundation)
func _bridgeToObjectiveCImpl() -> AnyObject {
return _buffer._asCocoaArray()
}

/// Tries to downcast the source `NSArray` as our native buffer type.
/// If it succeeds, creates a new `Array` around it and returns that.
/// Returns `nil` otherwise.
Expand Down Expand Up @@ -2154,7 +2162,7 @@ extension ArraySlice {
init(_startIndex: Int) {
self.init(
_buffer: _Buffer(
ContiguousArray()._buffer,
_buffer: ContiguousArray()._buffer,
shiftedToStartIndex: _startIndex))
}
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ContiguousArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
_uncheckedUnsafeBufferObject: _emptyArrayStorage)
}

public init(_ buffer: _ContiguousArrayBuffer, shiftedToStartIndex: Int) {
public init(_buffer buffer: _ContiguousArrayBuffer, shiftedToStartIndex: Int) {
_sanityCheck(shiftedToStartIndex == 0, "shiftedToStartIndex must be 0")
self = buffer
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/SliceBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct _SliceBuffer<Element> : _ArrayBufferProtocol, RandomAccessCollection {
_invariantCheck()
}

public init(_ buffer: NativeBuffer, shiftedToStartIndex: Int) {
public init(_buffer buffer: NativeBuffer, shiftedToStartIndex: Int) {
let shift = buffer.startIndex - shiftedToStartIndex
self.init(
owner: buffer.owner,
Expand Down
16 changes: 9 additions & 7 deletions test/1_stdlib/BridgeNonVerbatim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

import Swift
import SwiftShims
import ObjectiveC
import Foundation
import StdlibUnittest
import StdlibUnittestFoundationExtras

struct X : _ObjectiveCBridgeable {
init(_ value: Int) {
Expand Down Expand Up @@ -65,20 +66,20 @@ print("testing...")

func testScope() {
let a = [X(1), X(2), X(3)]
let nsx = a._buffer._asCocoaArray()
let nsx: NSArray = a._bridgeToObjectiveC()

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

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

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

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

// CHECK-NEXT: getObjects yields them at +0: true
Expand Down
4 changes: 2 additions & 2 deletions test/Interpreter/SDK/objc_fast_enumeration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ for x in s_m {
// CHECK: 2
// CHECK: 1
var a2 = [3, 2, 1]
var nsa2 = (a2._buffer._asCocoaArray() as AnyObject) as! NSArray
var nsa2: NSArray = a2._bridgeToObjectiveC()
for x in nsa2 {
print(x)
}
Expand All @@ -107,7 +107,7 @@ class X : CustomStringConvertible {
// CHECK: X(2)
// CHECK: X(1)
var a3 = [X(3), X(2), X(1)]
var nsa3 = (a3._buffer._asCocoaArray() as AnyObject) as! NSArray
var nsa3: NSArray = a3._bridgeToObjectiveC()
for x in nsa3 {
print(x)
}
31 changes: 31 additions & 0 deletions validation-test/StdlibUnittest/Assertions.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,36 @@ AssertionsTestSuite.test("expectEqual<T : Equatable, U : Equatable, V : Equatabl
}
}

AssertionsTestSuite.test("expectEqual<T : Equatable, U : Equatable, V : Equatable, W : Equatable>") {
let _0 = MinimalEquatableValue(0)
let _1 = MinimalEquatableValue(1)

for a in [_0, _1] {
for b in [_0, _1] {
for c in [_0, _1] {
for d in [_0, _1] {
for e in [_0, _1] {
for f in [_0, _1] {
for g in [_0, _1] {
for h in [_0, _1] {
let lhs = (a, b, c, d)
let rhs = (e, f, g, h)
if lhs == rhs {
expectEqual(lhs, rhs)
} else {
expectFailure {
expectEqual(lhs, rhs)
}
}
}
}
}
}
}
}
}
}
}

runAllTests()

Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public protocol MySequence {
_ preprocess: (Self) -> R
) -> R?

func _copyToNativeArrayBuffer()
-> _ContiguousArrayBuffer<Iterator.Element>
func _copyToContiguousArray()
-> ContiguousArray<Iterator.Element>

func _copyContents(
initializing ptr: UnsafeMutablePointer<Iterator.Element>
Expand Down Expand Up @@ -71,8 +71,8 @@ extension MySequence {
return nil
}

public func _copyToNativeArrayBuffer()
-> _ContiguousArrayBuffer<Iterator.Element> {
public func _copyToContiguousArray()
-> ContiguousArray<Iterator.Element> {
fatalError()
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public class SequenceLog {
public static var filter = TypeIndexed(0)
public static var _customContainsEquatableElement = TypeIndexed(0)
public static var _preprocessingPass = TypeIndexed(0)
public static var _copyToNativeArrayBuffer = TypeIndexed(0)
public static var _copyToContiguousArray = TypeIndexed(0)
public static var _copyContents = TypeIndexed(0)
}

Expand Down Expand Up @@ -281,10 +281,10 @@ extension LoggingSequenceType

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

/// Copy a Sequence into an array.
Expand Down
Loading