Skip to content

stdlib: two small Array improvements #28486

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 2 commits into from
Dec 2, 2019
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
6 changes: 6 additions & 0 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,12 @@ extension Array: RandomAccessCollection, MutableCollection {
public var count: Int {
return _getCount()
}

@inlinable
@_alwaysEmitIntoClient
public var first: Element? {
_getCount() == 0 ? nil : _buffer[0]
}
}

extension Array: ExpressibleByArrayLiteral {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ extension _ArrayBuffer {
internal var count: Int {
@inline(__always)
get {
return _fastPath(_isNative) ? _native.count : _nonNative.count
return _fastPath(_isNative) ? _native.count : _nonNative.endIndex
}
set {
_internalInvariant(_isNative, "attempting to update count of Cocoa array")
Expand Down
4 changes: 2 additions & 2 deletions test/IDE/complete_from_stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func testArchetypeReplacement1<FOO : Equatable>(_ a: [FOO]) {
// PRIVATE_NOMINAL_MEMBERS_5-DAG: Decl[InstanceMethod]/CurrNominal: append({#(newElement): Equatable#})[#Void#]{{; name=.+}}
// PRIVATE_NOMINAL_MEMBERS_5-DAG: Decl[InstanceMethod]/CurrNominal: insert({#(newElement): Equatable#}, {#at: Int#})[#Void#]{{; name=.+}}
// PRIVATE_NOMINAL_MEMBERS_5-DAG: Decl[InstanceVar]/Super: isEmpty[#Bool#]{{; name=.+}}
// PRIVATE_NOMINAL_MEMBERS_5-DAG: Decl[InstanceVar]/Super: first[#Equatable?#]{{; name=.+}}
// PRIVATE_NOMINAL_MEMBERS_5-DAG: Decl[InstanceVar]/CurrNominal: first[#Equatable?#]{{; name=.+}}
// PRIVATE_NOMINAL_MEMBERS_5-DAG: Decl[InstanceMethod]/Super: dropFirst({#(k): Int#})[#ArraySlice<Equatable>#]{{; name=.+}}
// PRIVATE_NOMINAL_MEMBERS_5-DAG: Decl[InstanceMethod]/Super: dropLast({#(k): Int#})[#ArraySlice<Equatable>#]{{; name=.+}}
// PRIVATE_NOMINAL_MEMBERS_5-DAG: Decl[InstanceMethod]/Super: prefix({#(maxLength): Int#})[#ArraySlice<Equatable>#]{{; name=.+}}
Expand Down Expand Up @@ -164,7 +164,7 @@ func testArchetypeReplacement3 (_ a : [Int]) {
// PRIVATE_NOMINAL_MEMBERS_7: Begin completions
// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/CurrNominal: append({#(newElement): Int#})[#Void#]
// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: removeLast()[#Int#]
// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceVar]/Super: first[#Int?#]
// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceVar]/CurrNominal: first[#Int?#]
// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: map({#(transform): (Int) throws -> T##(Int) throws -> T#})[' rethrows'][#[T]#]
// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: dropLast({#(k): Int#})[#ArraySlice<Int>#]
// PRIVATE_NOMINAL_MEMBERS_7-DAG: Decl[InstanceMethod]/Super: elementsEqual({#(other): Sequence#}, {#by: (Int, Sequence.Element) throws -> Bool##(Int, Sequence.Element) throws -> Bool#})[' rethrows'][#Bool#]; name=elementsEqual(other: Sequence, by: (Int, Sequence.Element) throws -> Bool) rethrows
Expand Down
2 changes: 1 addition & 1 deletion test/IDE/complete_literal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func testArray(f1: Float) {
_ = [1, 2, f1] #^LITERAL8^#
}
// LITERAL8-DAG: Decl[InstanceVar]/CurrNominal: .count[#Int#]; name=count
// LITERAL8-DAG: Decl[InstanceVar]/Super: .first[#Float?#]; name=first
// LITERAL8-DAG: Decl[InstanceVar]/CurrNominal: .first[#Float?#]; name=first

func testDict(f1: Float) {
_ = ["foo": f1, "bar": "baz"] #^LITERAL9^#
Expand Down
8 changes: 4 additions & 4 deletions test/IDE/complete_swift_key_path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ let _ = \Person.friends#^ARRAY_NODOT^#
// ARRAY-NODOT: Begin completions
// ARRAY-NODOT-DAG: Decl[Subscript]/CurrNominal: [{#(index): Int#}][#Person#]; name=[index: Int]
// ARRAY-NODOT-DAG: Decl[InstanceVar]/CurrNominal: .count[#Int#]; name=count
// ARRAY-NODOT-DAG: Decl[InstanceVar]/Super: .first[#Person?#]; name=first
// ARRAY-NODOT-DAG: Decl[InstanceVar]/CurrNominal: .first[#Person?#]; name=first

let _ = \Person.friends.#^ARRAY_DOT^#
// ARRAY-DOT: Begin completions
// ARRAY-DOT-NOT: Decl[Subscript]/CurrNominal: [{#(index): Int#}][#Element#]; name=[Int]
// ARRAY-DOT-DAG: Decl[InstanceVar]/CurrNominal: count[#Int#]; name=count
// ARRAY-DOT-DAG: Decl[InstanceVar]/Super: first[#Person?#]; name=first
// ARRAY-DOT-DAG: Decl[InstanceVar]/CurrNominal: first[#Person?#]; name=first
// ARRAY-DOT-NOT: Decl[Subscript]/CurrNominal: [{#(index): Int#}][#Element#]; name=[Int]

let _ = \Person.friends[0]#^OBJ_NODOT^#
Expand Down Expand Up @@ -114,13 +114,13 @@ let _ = \[Person]#^ARRAYTYPE_NODOT^#
// ARRAYTYPE-NODOT: Begin completions
// ARRAYTYPE-NODOT-DAG: Decl[Subscript]/CurrNominal: .[{#(index): Int#}][#Person#]; name=[index: Int]
// ARRAYTYPE-NODOT-DAG: Decl[InstanceVar]/CurrNominal: .count[#Int#]; name=count
// ARRAYTYPE-NODOT-DAG: Decl[InstanceVar]/Super: .first[#Person?#]; name=first
// ARRAYTYPE-NODOT-DAG: Decl[InstanceVar]/CurrNominal: .first[#Person?#]; name=first

let _ = \[Person].#^ARRAYTYPE_DOT^#
// ARRAYTYPE-DOT: Begin completions
// ARRAYTYPE-DOT-DAG: Decl[Subscript]/CurrNominal: [{#(index): Int#}][#Person#]; name=[index: Int]
// ARRAYTYPE-DOT-DAG: Decl[InstanceVar]/CurrNominal: count[#Int#]; name=count
// ARRAYTYPE-DOT-DAG: Decl[InstanceVar]/Super: first[#Person?#]; name=first
// ARRAYTYPE-DOT-DAG: Decl[InstanceVar]/CurrNominal: first[#Person?#]; name=first

func test(_ p: Person) {
let _ = p[keyPath: \Person.#^APPLY_TYPE_DOT^#]
Expand Down