Commit Graph

40 Commits

Author SHA1 Message Date
Ben Cohen
df22c3647c [stdlib] Build the standard library as Swift 5 (#18121)
* Update std lib to Swift 5.0

* Disable Unicode.* warnings for now

* Slow path to resiliently handle the case where an unknown rounding rule is passed

* Remove resilience from Encoding/DecodingError (which should only happen on slow paths anyway)

* internal typealiases now need @usableFromInline

* Force inlining on Array._owner.get
2018-07-24 20:47:58 -07:00
Karoy Lorentey
c19547a2e0 [stdlib] Set: Update methods defined in terms of key-value
insert(value, forKey: key), removeValue(forKey: key) doesn’t make sense for Set. Simplify.
2018-07-17 15:59:51 +01:00
Karoy Lorentey
543e050702 [stdlib] Set, Dictionary: Remove _Mutable*Buffer protocols
They had but a single type conforming them, so they did not pull their weight as internal interface-enforment protocols.
2018-07-17 15:59:51 +01:00
Karoy Lorentey
5a22b9016a [stdlib] Split _HashBuffer into four different protocols
This allows Set’s internal types not to define Key, Value, SequenceElement & SequenceElementWithoutLabels typealiases.

Splitting the protocol on the mutable/immutable axis allows us to remove some obsolete method definitions.
2018-07-17 15:59:51 +01:00
Karoy Lorentey
9756677d72 [stdlib] Set, Dictionary: Remove RawStorage typealiases
These were an artifact of gybbing.
2018-07-17 15:59:51 +01:00
Karoy Lorentey
a339f88a29 [stdlib] NFC: cosmetic changes 2018-07-17 15:59:51 +01:00
Karoy Lorentey
99f348800d [stdlib] Set, Dictionary: Make Cocoa->native conversion non-inlinable 2018-07-17 15:59:50 +01:00
Karoy Lorentey
47cae81c75 [stdlib] Set, Dictionary: Make Cocoa iteration state non-inlinable. 2018-07-17 15:59:50 +01:00
Karoy Lorentey
4ac2fc7517 [stdlib] _SwiftDeferredNS*: Clean up gyb artifacts
Remove typealiases and member functions made obsolete by de-gybbing.
2018-07-17 15:59:50 +01:00
Karoy Lorentey
619db38b5d [stdlib] Make non-verbatim bridged Set and Dictionary implementations internal
There is no reason to expose these in the Swift ABI.
2018-07-17 15:59:50 +01:00
Karoy Lorentey
c72bf4cdfa [stdlib] Make Set & Dictionary enumerators internal
There is no reason to expose these in the Swift ABI.
2018-07-17 15:59:50 +01:00
Ben Cohen
b77b544cdc Remove remaining @ininable from @_transparent (#17968) 2018-07-16 08:12:52 -07:00
Ben Cohen
a6952decab [stdlib] Remove inlineable annotation from transparent functions (#17800)
* Remove inlineable annotation from transparent functions
2018-07-07 08:47:02 -07:00
Karoy Lorentey
a4e9109618 Merge pull request #17396 from lorentey/anyhashable-is-not-hashable
[stdlib] Fix AnyHashable's Equatable/Hashable conformance
2018-06-29 17:38:08 +01:00
Ben Cohen
a4230ab2ad [stdlib] Update stdlib to 4.0 and reorganize compatibility shims (#17580)
* Update stdlib to 4.0 and move all compatibility shims into a dedicated source file
2018-06-29 06:26:52 -07:00
Karoy Lorentey
2f4ad7982d [stdlib] Eliminate _AnyHashableBox._asSet() & ._asDictionary()
_canonicalBox can perform essentially the same job, so there is no reason to have these requirements.
2018-06-28 18:48:56 +01:00
Ben Cohen
92b6d8cb8f Remove inlineability from mirrors (#17476) 2018-06-25 19:54:13 -07:00
Karoy Lorentey
ff91f36a9d [stdlib] Fix AnyHashable's Equatable/Hashable conformance
AnyHashable has numerous edge cases where two AnyHashable values compare equal but produce different hashes. This breaks Set and Dictionary invariants and can cause unexpected behavior and/or traps. This change overhauls AnyHashable's implementation to fix these edge cases, hopefully without introducing new issues.

- Fix transitivity of ==. Previously, comparisons involving AnyHashable values with Objective-C provenance were handled specially, breaking Equatable:

    let a = (42 as Int as AnyHashable)
    let b = (42 as NSNumber as AnyHashable)
    let c = (42 as Double as AnyHashable)
    a == b // true
    b == c // true
    a == c // was false(!), now true

    let d = ("foo" as AnyHashable)
    let e = ("foo" as NSString as AnyHashable)
    let f = ("foo" as NSString as NSAttributedStringKey as AnyHashable)
    d == e // true
    e == f // true
    d == f // was false(!), now true

- Fix Hashable conformance for numeric types boxed into AnyHashable:

    b == c // true
    b.hashValue == c.hashValue // was false(!), now true

  Fixing this required adding a custom AnyHashable box for all standard integer and floating point types. The custom box was needed to ensure that two AnyHashables containing the same number compare equal and hash the same way, no matter what their original type was. (This behavior is required to ensure consistency with NSNumber, which has not been preserving types since SE-0170.

- Add custom AnyHashable representations for Arrays, Sets and Dictionaries, so that when they contain numeric types, they hash correctly under the new rules above.

- Remove AnyHashable._usedCustomRepresentation. The provenance of a value should not affect its behavior.

- Allow AnyHashable values to be downcasted into compatible types more often.

- Forward _rawHashValue(seed:) to AnyHashable box. This fixes AnyHashable hashing for types that customize single-shot hashing.

https://bugs.swift.org/browse/SR-7496
rdar://problem/39648819
2018-06-25 20:14:01 +01:00
Karoy Lorentey
23c630ac92 [stdlib] Add @usableFromInline to internal typealiases that need it
This fixes 3659 warnings in the standard library.
2018-06-18 16:34:19 +01:00
Karoy Lorentey
07c1b74cc4 [stdlib] Audit inlinability of Hashable implementations
As a general rule, it is safe to mark the implementation of hash(into:) and _rawHashValue(seed:) for @_fixed_layout structs as inlinable.

However, some structs (like String guts, Character, KeyPath-related types) have complicated enough hashing that it seems counterproductive to inline them. Mark these with @effects(releasenone) instead.
2018-05-31 18:24:59 -07:00
Nate Cook
7a4e0a32f6 [stdlib] Revise documentation
This includes various revisions to the APIs landing in Swift 4.2, including:
- Random and other randomness APIs
- Hashable changes
- MemoryLayout.offset(of:)
2018-05-18 11:31:54 -05:00
Michael Ilseman
c25188bafd Merge pull request #16140 from milseman/never_inlin
[stdlib] Drop @inlinable if @inline(never).
2018-05-03 17:43:24 -07:00
Karoy Lorentey
5b80cf1d59 [stdlib]: Set,Dictionary: Fix subtle cross-collection hash collision issue
When Set/Dictionary is nested in another Set, the boundaries of the nested collections weren’t correctly delineated in commutative hashing.

For example, these Sets all hashed the same:

[[1, 2], [3, 4]]
[[1, 3], [2, 4]]
[[1, 4], [2, 3]]

Hash collisions could thus be systematically generated.

To fix this, remove collection-level support for one-shot hashing and revert to the previous method of generating hash values. (Set is still able to support one-shot hashing for its members, though.)
2018-04-30 15:40:16 +01:00
Karoy Lorentey
a274a0c92a [stdlib] Implement top-level hashing for stdlib types 2018-04-30 15:40:16 +01:00
Karoy Lorentey
3a162d225a [stdlib] Hashable, Hasher: Add non-public top-level hashing interface
The new _rawHashValue(seed:) requirement allows stdlib types to specialize their hashing when they’re hashed on their own (i.e., not as a component of some composite type).

This makes it possible to get rid of discriminator/terminator values and to eliminate most of Hasher’s resiliency overhead, leading to a measurable speedup, especially for tiny keys.
2018-04-30 15:16:47 +01:00
Karoy Lorentey
0342ce3b96 [SE-0206][stdlib] Remove obsolete hashValue implementations
These are now synthesized by the compiler.
(Inlinability will be different, but that seems fine.)
2018-04-30 10:17:09 +01:00
Karoy Lorentey
239c2c91dd [SE-0206][stdlib] Add missing hash(into:) declarations 2018-04-30 10:17:09 +01:00
Michael Ilseman
e907d11dbc [stdlib] Drop @inlinable if @inline(never).
Aggressively remove all `@inlinable` from any function that's
`@inline(never)` to see the impact.

`@inlinable @inline(never)` is a potential code smell. While it might
expose some optimization and specialization opportunities to the
optimizer, it's most commonly a sign that more thought is needed.
2018-04-25 18:30:33 -07:00
Karoy Lorentey
0fb3e88110 [SE-0206][stdlib] De-underscore Hashable.hash(into:) 2018-04-24 17:42:42 +01:00
Karoy Lorentey
87685aaef9 [SE-0206][stdlib] Make Hasher.finalize() nonmutating but __consuming
As noted in the proposal’s revision, this allows us to get rid of finalization checks, improves API robustness, and paves the way for making Hasher move-only in the future.
2018-04-24 17:42:42 +01:00
Karoy Lorentey
45cb8b7123 [SE-0206][stdlib] De-underscore Hasher 2018-04-24 17:42:42 +01:00
Nate Cook
b1ab7d89db [stdlib] Implement sequence/collection methods for searching from the end (#13337)
This implements the new last(where:), and lastIndex(of/where:) methods as
extensions on `BidirectionalCollection`, which partially implements SE-204.
The protocol requirements for `Sequence` and `Collection` as described
in the proposal need to wait until there's a solution for picking up the
specialized versions in types that conditionally conform to `BidirectionalCollection`.
2018-04-23 20:47:26 -05:00
Nate Cook
58933d88c5 [stdlib] Rename index(...) methods to firstIndex(...)
A la SE-204.
2018-04-21 18:07:25 -05:00
Karoy Lorentey
ccdc218cbd [stdlib] _Hasher: append => combine 2018-04-18 14:18:44 +01:00
Karoy Lorentey
cff4eb77ef [stdlib] {Set,Dictionary}.init(minimumCapacity:) Update docs
Avoid describing the exact algorithm that these collections use to allocate storage. (It is a private implementation detail that we want to be able to change.)

rdar://problem/36619317
2018-04-11 12:25:51 +01:00
Slava Pestov
2e5aef9c8d stdlib: Remove redundant @usableFromInline attributes 2018-04-06 00:02:30 -07:00
Slava Pestov
e1f50b2d36 SE-0193: Rename @_inlineable to @inlinable, @_versioned to @usableFromInline 2018-03-30 21:55:30 -07:00
Karoy Lorentey
96d93a4f94 [stdlib] Set, Dictionary: Clarify assert message when a duplicate key is found
When a duplicate key is found during rehashing, it is usually either because a key was mutated after insertion, or because the dictionary itself was mutated from multiple threads at the same time.

Both of these are serious programmer errors. Promote the sanity check for duplicate keys to a full precondition and improve the error message to point out the most probable cause of the failure.
2018-03-28 11:59:13 +01:00
Karoy Lorentey
b961736318 [stdlib] Set, Dictionary: cosmetic changes (NFC)
Reindent, refresh coding conventions, move some members around.
(This would be disruptive at any other time, but we're right after a degybbing, so...)
2018-03-21 18:33:46 +00:00
Karoy Lorentey
3d0b6e7b2d [stdlib] De-gyb and split HashedCollections.swift.gyb (NFC)
Split HashedCollections.swift.gyb into separate Set.swift and Dictionary.swift files, with some common parts going into Hashing.swift.

This is mostly a mechanical change, in preparation of unification of common parts between Set & Dictionary.

rdar://problem/34038727
2018-03-21 18:33:09 +00:00