Skip to content

Commit a527e53

Browse files
masters3dairspeedswift
authored andcommitted
Renamed DictionaryLiteral to KeyValuePairs (#16577)
* renamed DictionaryLiteral to KeyValuePairs per SE-0214 * renamed DictionaryLiteral type tests to KeyValuePairs * [SE-0214] Move changelog entry (Swift 4.2 => 5.0) * [SE-0214] Update comment in AST/Expr.h * [SE-0214] Use generic typealias See also <#17711> * [SE-0214] Update source-stability.swift.expected
1 parent 75d9e56 commit a527e53

File tree

10 files changed

+62
-39
lines changed

10 files changed

+62
-39
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ CHANGELOG
2424
Swift 5.0
2525
---------
2626

27+
* [SE-0214][]:
28+
29+
Renamed the `DictionaryLiteral` type to `KeyValuePairs`.
30+
A typealias preserves the old name for compatibility.
31+
2732
* [SR-2608][]
2833

2934
Default arguments are now printed in SourceKit-generated interfaces for Swift
@@ -7147,6 +7152,20 @@ Swift 1.0
71477152
[SE-0210]: <https://github.com/apple/swift-evolution/blob/master/proposals/0210-key-path-offset.md>
71487153
[SE-0211]: <https://github.com/apple/swift-evolution/blob/master/proposals/0211-unicode-scalar-properties.md>
71497154
[SE-0212]: <https://github.com/apple/swift-evolution/blob/master/proposals/0212-compiler-version-directive.md>
7155+
[SE-0213]: <https://github.com/apple/swift-evolution/blob/master/proposals/0213-literal-init-via-coercion.md>
7156+
[SE-0214]: <https://github.com/apple/swift-evolution/blob/master/proposals/0214-DictionaryLiteral.md>
7157+
[SE-0215]: <https://github.com/apple/swift-evolution/blob/master/proposals/0215-conform-never-to-hashable-and-equatable.md>
7158+
[SE-0216]: <https://github.com/apple/swift-evolution/blob/master/proposals/0216-dynamic-callable.md>
7159+
[SE-0217]: <https://github.com/apple/swift-evolution/blob/master/proposals/0217-bangbang.md>
7160+
[SE-0218]: <https://github.com/apple/swift-evolution/blob/master/proposals/0218-introduce-compact-map-values.md>
7161+
[SE-0219]: <https://github.com/apple/swift-evolution/blob/master/proposals/0219-package-manager-dependency-mirroring.md>
7162+
[SE-0220]: <https://github.com/apple/swift-evolution/blob/master/proposals/0220-count-where.md>
7163+
[SE-0221]: <https://github.com/apple/swift-evolution/blob/master/proposals/0221-character-properties.md>
7164+
[SE-0222]: <https://github.com/apple/swift-evolution/blob/master/proposals/0222-lazy-compactmap-sequence.md>
7165+
[SE-0223]: <https://github.com/apple/swift-evolution/blob/master/proposals/0223-array-uninitialized-initializer.md>
7166+
[SE-0224]: <https://github.com/apple/swift-evolution/blob/master/proposals/0224-ifswift-lessthan-operator.md>
7167+
[SE-0225]: <https://github.com/apple/swift-evolution/blob/master/proposals/0225-binaryinteger-iseven-isodd-ismultiple.md>
7168+
[SE-0226]: <https://github.com/apple/swift-evolution/blob/master/proposals/0226-package-manager-target-based-dep-resolution.md>
71507169

71517170
[SR-106]: <https://bugs.swift.org/browse/SR-106>
71527171
[SR-419]: <https://bugs.swift.org/browse/SR-419>

benchmark/single-source/RomanNumbers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public let RomanNumbers = [
2525
tags: [.api, .String, .algorithm])
2626
]
2727

28-
let romanTable: DictionaryLiteral<String, Int> = [
28+
let romanTable: KeyValuePairs<String, Int> = [
2929
"M": 1000,
3030
"CM": 900,
3131
"D": 500,

benchmark/single-source/StringMatch.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func matchStar(_ c: Character, _ regexp: Substring, _ text: Substring) -> Bool {
7676
}
7777
}
7878

79-
let tests: DictionaryLiteral = [
79+
let tests: KeyValuePairs = [
8080
"^h..lo*!$":"hellooooo!",
8181
"^h..lo*!$":"hella noms",
8282
".ab":"abracadabra!",

include/swift/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class alignas(8) Expr {
144144
IsTypeDefaulted : 1,
145145
/// Number of comma source locations.
146146
NumCommas : 32 - 1 - NumExprBits,
147-
/// Number of entries in the collection. If this is a DictionaryLiteral,
147+
/// Number of entries in the collection. If this is a DictionaryExpr,
148148
/// each entry is a Tuple with the key and value pair.
149149
NumSubExprs : 32
150150
);

stdlib/private/StdlibUnittest/TypeIndexed.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public func <=> <T: Comparable>(
9898
}
9999

100100
public func expectEqual<V: Comparable>(
101-
_ expected: DictionaryLiteral<Any.Type, V>, _ actual: TypeIndexed<V>,
101+
_ expected: KeyValuePairs<Any.Type, V>, _ actual: TypeIndexed<V>,
102102
_ message: @autoclosure () -> String = "",
103103
showFrame: Bool = true,
104104
stackTrace: SourceLocStack = SourceLocStack(),

stdlib/public/core/CompilerProtocols.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,10 @@ public protocol ExpressibleByArrayLiteral {
660660
/// print(frequencies.count)
661661
/// // Prints "0"
662662
///
663-
/// - Note: A dictionary literal is *not* the same as an instance of
664-
/// `Dictionary` or the similarly named `DictionaryLiteral` type. You can't
665-
/// initialize a type that conforms to `ExpressibleByDictionaryLiteral` simply
666-
/// by assigning an instance of one of these types.
663+
/// - Note:
664+
/// A dictionary literal is *not* the same as an instance of `Dictionary`.
665+
/// You can't initialize a type that conforms to `ExpressibleByDictionaryLiteral`
666+
/// simply by assigning an instance of `Dictionary`, `KeyValuePairs`, or similar.
667667
///
668668
/// Conforming to the ExpressibleByDictionaryLiteral Protocol
669669
/// =========================================================

stdlib/public/core/Dictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ import SwiftShims
325325
/// The order of key-value pairs in a dictionary is stable between mutations
326326
/// but is otherwise unpredictable. If you need an ordered collection of
327327
/// key-value pairs and don't need the fast key lookup that `Dictionary`
328-
/// provides, see the `DictionaryLiteral` type for an alternative.
328+
/// provides, see the `KeyValuePairs` type for an alternative.
329329
///
330330
/// You can search a dictionary's contents for a particular value using the
331331
/// `contains(where:)` or `firstIndex(where:)` methods supplied by default

stdlib/public/core/Mirror.swift

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public struct Mirror {
309309
/// `subject` is not a class instance. The default is `.generated`.
310310
public init<Subject>(
311311
_ subject: Subject,
312-
children: DictionaryLiteral<String, Any>,
312+
children: KeyValuePairs<String, Any>,
313313
displayStyle: DisplayStyle? = nil,
314314
ancestorRepresentation: AncestorRepresentation = .generated
315315
) {
@@ -464,25 +464,25 @@ extension Mirror {
464464

465465
/// A lightweight collection of key-value pairs.
466466
///
467-
/// Use a `DictionaryLiteral` instance when you need an ordered collection of
467+
/// Use a `KeyValuePairs` instance when you need an ordered collection of
468468
/// key-value pairs and don't require the fast key lookup that the
469469
/// `Dictionary` type provides. Unlike key-value pairs in a true dictionary,
470-
/// neither the key nor the value of a `DictionaryLiteral` instance must
470+
/// neither the key nor the value of a `KeyValuePairs` instance must
471471
/// conform to the `Hashable` protocol.
472472
///
473-
/// You initialize a `DictionaryLiteral` instance using a Swift dictionary
473+
/// You initialize a `KeyValuePairs` instance using a Swift dictionary
474474
/// literal. Besides maintaining the order of the original dictionary literal,
475-
/// `DictionaryLiteral` also allows duplicates keys. For example:
475+
/// `KeyValuePairs` also allows duplicates keys. For example:
476476
///
477-
/// let recordTimes: DictionaryLiteral = ["Florence Griffith-Joyner": 10.49,
477+
/// let recordTimes: KeyValuePairs = ["Florence Griffith-Joyner": 10.49,
478478
/// "Evelyn Ashford": 10.76,
479479
/// "Evelyn Ashford": 10.79,
480480
/// "Marlies Gohr": 10.81]
481481
/// print(recordTimes.first!)
482482
/// // Prints "("Florence Griffith-Joyner", 10.49)"
483483
///
484484
/// Some operations that are efficient on a dictionary are slower when using
485-
/// `DictionaryLiteral`. In particular, to find the value matching a key, you
485+
/// `KeyValuePairs`. In particular, to find the value matching a key, you
486486
/// must search through every element of the collection. The call to
487487
/// `firstIndex(where:)` in the following example must traverse the whole
488488
/// collection to find the element that matches the predicate:
@@ -499,67 +499,70 @@ extension Mirror {
499499
/// Dictionary Literals as Function Parameters
500500
/// ------------------------------------------
501501
///
502-
/// When calling a function with a `DictionaryLiteral` parameter, you can pass
502+
/// When calling a function with a `KeyValuePairs` parameter, you can pass
503503
/// a Swift dictionary literal without causing a `Dictionary` to be created.
504504
/// This capability can be especially important when the order of elements in
505505
/// the literal is significant.
506506
///
507507
/// For example, you could create an `IntPairs` structure that holds a list of
508508
/// two-integer tuples and use an initializer that accepts a
509-
/// `DictionaryLiteral` instance.
509+
/// `KeyValuePairs` instance.
510510
///
511511
/// struct IntPairs {
512512
/// var elements: [(Int, Int)]
513513
///
514-
/// init(_ elements: DictionaryLiteral<Int, Int>) {
514+
/// init(_ elements: KeyValuePairs<Int, Int>) {
515515
/// self.elements = Array(elements)
516516
/// }
517517
/// }
518518
///
519519
/// When you're ready to create a new `IntPairs` instance, use a dictionary
520520
/// literal as the parameter to the `IntPairs` initializer. The
521-
/// `DictionaryLiteral` instance preserves the order of the elements as
521+
/// `KeyValuePairs` instance preserves the order of the elements as
522522
/// passed.
523523
///
524524
/// let pairs = IntPairs([1: 2, 1: 1, 3: 4, 2: 1])
525525
/// print(pairs.elements)
526526
/// // Prints "[(1, 2), (1, 1), (3, 4), (2, 1)]"
527527
@_fixed_layout // FIXME(sil-serialize-all)
528-
public struct DictionaryLiteral<Key, Value> : ExpressibleByDictionaryLiteral {
529-
/// Creates a new `DictionaryLiteral` instance from the given dictionary
528+
public struct KeyValuePairs<Key, Value> : ExpressibleByDictionaryLiteral {
529+
/// Creates a new `KeyValuePairs` instance from the given dictionary
530530
/// literal.
531531
///
532532
/// The order of the key-value pairs is kept intact in the resulting
533-
/// `DictionaryLiteral` instance.
533+
/// `KeyValuePairs` instance.
534534
public init(dictionaryLiteral elements: (Key, Value)...) {
535535
self._elements = elements
536536
}
537537
@usableFromInline // FIXME(sil-serialize-all)
538538
internal let _elements: [(Key, Value)]
539539
}
540540

541-
/// `Collection` conformance that allows `DictionaryLiteral` to
541+
@available(swift, deprecated: 5.0, renamed: "KeyValuePairs")
542+
public typealias DictionaryLiteral<Key, Value> = KeyValuePairs<Key, Value>
543+
544+
/// `Collection` conformance that allows `KeyValuePairs` to
542545
/// interoperate with the rest of the standard library.
543-
extension DictionaryLiteral : RandomAccessCollection {
546+
extension KeyValuePairs : RandomAccessCollection {
544547
public typealias Indices = Range<Int>
545548

546549
/// The position of the first element in a nonempty collection.
547550
///
548-
/// If the `DictionaryLiteral` instance is empty, `startIndex` is equal to
551+
/// If the `KeyValuePairs` instance is empty, `startIndex` is equal to
549552
/// `endIndex`.
550553
@inlinable // FIXME(sil-serialize-all)
551554
public var startIndex: Int { return 0 }
552555

553556
/// The collection's "past the end" position---that is, the position one
554557
/// greater than the last valid subscript argument.
555558
///
556-
/// If the `DictionaryLiteral` instance is empty, `endIndex` is equal to
559+
/// If the `KeyValuePairs` instance is empty, `endIndex` is equal to
557560
/// `startIndex`.
558561
@inlinable // FIXME(sil-serialize-all)
559562
public var endIndex: Int { return _elements.endIndex }
560563

561564
// FIXME(ABI)#174 (Type checker): a typealias is needed to prevent <rdar://20248032>
562-
/// The element type of a `DictionaryLiteral`: a tuple containing an
565+
/// The element type of a `KeyValuePairs`: a tuple containing an
563566
/// individual key-value pair.
564567
public typealias Element = (key: Key, value: Value)
565568

test/api-digester/source-stability.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Func Dictionary.filter(_:obsoletedInSwift4:) has been renamed to Func Dictionary
7272
Func Set.filter(_:obsoletedInSwift4:) has been renamed to Func Set.filter(_:)
7373

7474
/* Type Changes */
75+
Constructor Mirror.init(_:children:displayStyle:ancestorRepresentation:) has parameter 1 type change from DictionaryLiteral<String, Any> to KeyValuePairs<String, Any>
7576
Constructor String.init(_:) has return type change from String? to String
7677
Func Dictionary.filter(_:obsoletedInSwift4:) has return type change from [Dictionary<Key, Value>.Element] to [Dictionary<Key, Value>.Key : Dictionary<Key, Value>.Value]
7778
Func Dictionary.makeIterator() has return type change from DictionaryIterator<Dictionary<Key, Value>.Key, Dictionary<Key, Value>.Value> to Dictionary<Key, Value>.Iterator

test/stdlib/DictionaryLiteral.swift renamed to test/stdlib/KeyValuePairs.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- DictionaryLiteral.swift ------------------------------------------===//
1+
//===--- KeyValuePairs.swift ------------------------------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -21,14 +21,14 @@ import StdlibUnittest
2121
// Check that the generic parameters are called 'Key' and 'Value'.
2222
protocol TestProtocol1 {}
2323

24-
extension DictionaryLiteral where Key : TestProtocol1, Value : TestProtocol1 {
24+
extension KeyValuePairs where Key : TestProtocol1, Value : TestProtocol1 {
2525
var _keyAndValueAreTestProtocol1: Bool {
2626
fatalError("not implemented")
2727
}
2828
}
2929

3030
func checkAssociatedTypes() {
31-
typealias Subject = DictionaryLiteral<MinimalHashableValue, OpaqueValue<Int>>
31+
typealias Subject = KeyValuePairs<MinimalHashableValue, OpaqueValue<Int>>
3232
expectRandomAccessCollectionAssociatedTypes(
3333
collectionType: Subject.self,
3434
iteratorType: IndexingIterator<Subject>.self,
@@ -37,17 +37,17 @@ func checkAssociatedTypes() {
3737
indicesType: CountableRange<Int>.self)
3838
}
3939

40-
var strings: DictionaryLiteral = ["a": "1", "b": "Foo"]
41-
expectType(DictionaryLiteral<String, String>.self, &strings)
40+
var strings: KeyValuePairs = ["a": "1", "b": "Foo"]
41+
expectType(KeyValuePairs<String, String>.self, &strings)
4242

43-
var stringNSStringLiteral: DictionaryLiteral = [
43+
var stringNSStringLiteral: KeyValuePairs = [
4444
"a": "1", "b": "Foo" as NSString]
45-
expectType(DictionaryLiteral<String, NSString>.self, &stringNSStringLiteral)
45+
expectType(KeyValuePairs<String, NSString>.self, &stringNSStringLiteral)
4646

4747
let aString = "1"
4848
let anNSString = "Foo" as NSString
49-
var stringNSStringLet: DictionaryLiteral = [ "a": aString as NSString, "b": anNSString]
50-
expectType(DictionaryLiteral<String, NSString>.self, &stringNSStringLet)
49+
var stringNSStringLet: KeyValuePairs = [ "a": aString as NSString, "b": anNSString]
50+
expectType(KeyValuePairs<String, NSString>.self, &stringNSStringLet)
5151

52-
var hetero: DictionaryLiteral = ["a": 1 as NSNumber, "b": "Foo" as NSString]
53-
expectType(DictionaryLiteral<String, NSObject>.self, &hetero)
52+
var hetero: KeyValuePairs = ["a": 1 as NSNumber, "b": "Foo" as NSString]
53+
expectType(KeyValuePairs<String, NSObject>.self, &hetero)

0 commit comments

Comments
 (0)