Skip to content

Commit a710c2f

Browse files
authored
Merge pull request #18596 from natecook1000/nc-4.2-fixes-87-2
[4.2] [stdlib] Documentation revisions
2 parents 3c2eb97 + 49f0295 commit a710c2f

10 files changed

+168
-77
lines changed

stdlib/public/core/ClosedRange.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ extension ClosedRange: Equatable {
377377
///
378378
/// Two ranges are equal when they have the same lower and upper bounds.
379379
///
380-
/// let x: ClosedRange = 5...15
380+
/// let x = 5...15
381381
/// print(x == 5...15)
382382
/// // Prints "true"
383383
/// print(x == 10...20)

stdlib/public/core/Codable.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ public enum DecodingError : Error {
12451245
/// for debugging.
12461246
case typeMismatch(Any.Type, Context)
12471247

1248-
/// An indication that a non-optional value of the given type was expected,
1248+
/// An indication that a nonoptional value of the given type was expected,
12491249
/// but a null value was found.
12501250
///
12511251
/// As associated values, this case contains the attempted type and context

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ extension BidirectionalCollection {
141141
///
142142
/// let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
143143
/// if let lastNegative = numbers.last(where: { $0 < 0 }) {
144-
/// print("The last negative number is \(firstNegative).")
144+
/// print("The last negative number is \(lastNegative).")
145145
/// }
146146
/// // Prints "The last negative number is -6."
147147
///
@@ -165,7 +165,7 @@ extension BidirectionalCollection {
165165
/// You can use the predicate to find an element of a type that doesn't
166166
/// conform to the `Equatable` protocol or to find an element that matches
167167
/// particular criteria. This example finds the index of the last name that
168-
/// begins with the letter "A":
168+
/// begins with the letter *A:*
169169
///
170170
/// let students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
171171
/// if let i = students.lastIndex(where: { $0.hasPrefix("A") }) {
@@ -213,7 +213,7 @@ extension BidirectionalCollection where Element : Equatable {
213213
///
214214
/// - Parameter element: An element to search for in the collection.
215215
/// - Returns: The last index where `element` is found. If `element` is not
216-
/// found in the collection, returns `nil`.
216+
/// found in the collection, this method returns `nil`.
217217
///
218218
/// - Complexity: O(*n*), where *n* is the length of the collection.
219219
@inlinable

stdlib/public/core/Dictionary.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ import SwiftShims
342342
///
343343
/// Note that in this example, `imagePaths` is subscripted using a dictionary
344344
/// index. Unlike the key-based subscript, the index-based subscript returns
345-
/// the corresponding key-value pair as a non-optional tuple.
345+
/// the corresponding key-value pair as a nonoptional tuple.
346346
///
347347
/// print(imagePaths[glyphIndex!])
348348
/// // Prints "("star", "/glyphs/star.png")"
@@ -879,6 +879,8 @@ extension Dictionary {
879879
/// transformed value of the same or of a different type.
880880
/// - Returns: A dictionary containing the keys and transformed values of
881881
/// this dictionary.
882+
///
883+
/// - Complexity: O(*n*), where *n* is the length of the dictionary.
882884
@inlinable // FIXME(sil-serialize-all)
883885
public func mapValues<T>(
884886
_ transform: (Value) throws -> T

stdlib/public/core/Integers.swift.gyb

Lines changed: 109 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ def operatorComment(operator, fixedWidth):
124124
///
125125
/// - Note: Overflow checking is not performed in `-Ounchecked` builds.
126126
///
127-
/// If you want to opt out of overflow checking and ignore any overflow, use
128-
/// the overflow addition operator (`&+`).
127+
/// If you want to opt out of overflow checking and wrap the result in case
128+
/// of any overflow, use the overflow addition operator (`&+`).
129129
///
130130
/// x &+ 120 // -115
131131
///
@@ -161,8 +161,8 @@ def operatorComment(operator, fixedWidth):
161161
///
162162
/// - Note: Overflow checking is not performed in `-Ounchecked` builds.
163163
///
164-
/// If you want to opt out of overflow checking and ignore any overflow, use
165-
/// the overflow subtraction operator (`&-`).
164+
/// If you want to opt out of overflow checking and wrap the result in case
165+
/// of any overflow, use the overflow subtraction operator (`&-`).
166166
///
167167
/// x &- 50 // 227
168168
///
@@ -198,8 +198,8 @@ def operatorComment(operator, fixedWidth):
198198
///
199199
/// - Note: Overflow checking is not performed in `-Ounchecked` builds.
200200
///
201-
/// If you want to opt out of overflow checking and ignore any overflow, use
202-
/// the overflow multiplication operator (`&*`).
201+
/// If you want to opt out of overflow checking and wrap the result in case
202+
/// of any overflow, use the overflow multiplication operator (`&*`).
203203
///
204204
/// x &* 21 // -115
205205
///
@@ -224,7 +224,7 @@ def operatorComment(operator, fixedWidth):
224224
/// Returns the remainder of dividing the first value by the second.
225225
///
226226
/// The result of the remainder operator (`%`) has the same sign as `lhs` and
227-
/// is less than `rhs.magnitude`.
227+
/// has a magnitude less than `rhs.magnitude`.
228228
///
229229
/// let x = 22 % 5
230230
/// // x == 2
@@ -241,52 +241,76 @@ def operatorComment(operator, fixedWidth):
241241
/// - rhs: The value to divide `lhs` by. `rhs` must not be zero.
242242
""",
243243
'&+': """\
244-
/// Returns the sum of the two given values, discarding any overflow.
244+
/// Returns the sum of the two given values, wrapping the result in case of
245+
/// any overflow.
245246
///
246-
/// The masking addition operator (`&+`) silently discards any overflow that
247-
/// occurs during the operation. In the following example, the sum of `100`
248-
/// and `121` is greater than the maximum representable `Int8` value, so the
249-
/// result is the overflowed value:
247+
/// The overflow addition operator (`&+`) discards any bits that overflow the
248+
/// fixed width of the integer type. In the following example, the sum of
249+
/// `100` and `121` is greater than the maximum representable `Int8` value,
250+
/// so the result is the partial value after discarding the overflowing
251+
/// bits.
250252
///
251253
/// let x: Int8 = 10 &+ 21
252254
/// // x == 31
253255
/// let y: Int8 = 100 &+ 121
254256
/// // y == -35 (after overflow)
255257
///
258+
/// For more about arithmetic with overflow operators, see [Overflow
259+
/// Operators][overflow] in *[The Swift Programming Language][tspl]*.
260+
///
261+
/// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
262+
/// [tspl]: https://docs.swift.org/swift-book/
263+
///
256264
/// - Parameters:
257265
/// - lhs: The first value to add.
258266
/// - rhs: The second value to add.
259267
""",
260268
'&-': """\
261-
/// Returns the difference of the two given values, discarding any overflow.
269+
/// Returns the difference of the two given values, wrapping the result in
270+
/// case of any overflow.
262271
///
263-
/// The masking subtraction operator (`&-`) silently discards any overflow
264-
/// that occurs during the operation. In the following example, the
272+
/// The overflow subtraction operator (`&-`) discards any bits that overflow
273+
/// the fixed width of the integer type. In the following example, the
265274
/// difference of `10` and `21` is less than zero, the minimum representable
266-
/// `UInt` value, so the result is the overflowed value:
275+
/// `UInt` value, so the result is the partial value after discarding the
276+
/// overflowing bits.
267277
///
268278
/// let x: UInt8 = 21 &- 10
269279
/// // x == 11
270280
/// let y: UInt8 = 10 &- 21
271281
/// // y == 245 (after overflow)
272282
///
283+
/// For more about arithmetic with overflow operators, see [Overflow
284+
/// Operators][overflow] in *[The Swift Programming Language][tspl]*.
285+
///
286+
/// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
287+
/// [tspl]: https://docs.swift.org/swift-book/
288+
///
273289
/// - Parameters:
274290
/// - lhs: A numeric value.
275291
/// - rhs: The value to subtract from `lhs`.
276292
""",
277293
'&*': """\
278-
/// Returns the product of the two given values, discarding any overflow.
294+
/// Returns the product of the two given values, wrapping the result in case
295+
/// of any overflow.
279296
///
280-
/// The masking multiplication operator (`&*`) silently discards any overflow
281-
/// that occurs during the operation. In the following example, the product
282-
/// of `10` and `50` is greater than the maximum representable `Int8` value,
283-
/// so the result is the overflowed value:
297+
/// The overflow multiplication operator (`&*`) discards any bits that
298+
/// overflow the fixed width of the integer type. In the following example,
299+
/// the product of `10` and `50` is greater than the maximum representable
300+
/// `Int8` value, so the result is the partial value after discarding the
301+
/// overflowing bits.
284302
///
285303
/// let x: Int8 = 10 &* 5
286304
/// // x == 50
287305
/// let y: Int8 = 10 &* 50
288306
/// // y == -12 (after overflow)
289307
///
308+
/// For more about arithmetic with overflow operators, see [Overflow
309+
/// Operators][overflow] in *[The Swift Programming Language][tspl]*.
310+
///
311+
/// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
312+
/// [tspl]: https://docs.swift.org/swift-book/
313+
///
290314
/// - Parameters:
291315
/// - lhs: The first value to multiply.
292316
/// - rhs: The second value to multiply.
@@ -587,7 +611,8 @@ def assignmentOperatorComment(operator, fixedWidth):
587611
/// Divides the first value by the second and stores the remainder in the
588612
/// left-hand-side variable.
589613
///
590-
/// The result has the same sign as `lhs` and is less than `rhs.magnitude`.
614+
/// The result has the same sign as `lhs` and has a magnitude less than
615+
/// `rhs.magnitude`.
591616
///
592617
/// var x = 22
593618
/// x %= 5
@@ -606,13 +631,14 @@ def assignmentOperatorComment(operator, fixedWidth):
606631
/// - rhs: The value to divide `lhs` by. `rhs` must not be zero.
607632
""",
608633
'&+': """\
609-
/// Adds two values and stores the result in the left-hand-side variable,
610-
/// discarding any overflow.
634+
/// Adds two values and stores the result in the left-hand-side variable,
635+
/// wrapping any overflow.
611636
///
612-
/// The masking addition assignment operator (`&+=`) silently discards any
613-
/// overflow that occurs during the operation. In the following example, the
614-
/// sum of `100` and `121` is greater than the maximum representable `Int8`
615-
/// value, so the result is the overflowed value:
637+
/// The masking addition assignment operator (`&+=`) silently wraps any
638+
/// overflow that occurs during the operation. In the following example, the
639+
/// sum of `100` and `121` is greater than the maximum representable `Int8`
640+
/// value, so the result is the partial value after discarding the
641+
/// overflowing bits.
616642
///
617643
/// var x: Int8 = 10
618644
/// x &+= 21
@@ -621,18 +647,25 @@ def assignmentOperatorComment(operator, fixedWidth):
621647
/// y &+= 121
622648
/// // y == -35 (after overflow)
623649
///
650+
/// For more about arithmetic with overflow operators, see [Overflow
651+
/// Operators][overflow] in *[The Swift Programming Language][tspl]*.
652+
///
653+
/// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
654+
/// [tspl]: https://docs.swift.org/swift-book/
655+
///
624656
/// - Parameters:
625657
/// - lhs: The first value to add.
626658
/// - rhs: The second value to add.
627659
""",
628660
'&-': """\
629661
/// Subtracts the second value from the first and stores the difference in the
630-
/// left-hand-side variable, discarding any overflow.
662+
/// left-hand-side variable, wrapping any overflow.
631663
///
632-
/// The masking subtraction assignment operator (`&-=`) silently discards any
664+
/// The masking subtraction assignment operator (`&-=`) silently wraps any
633665
/// overflow that occurs during the operation. In the following example, the
634666
/// difference of `10` and `21` is less than zero, the minimum representable
635-
/// `UInt` value, so the result is the overflowed value:
667+
/// `UInt` value, so the result is the result is the partial value after
668+
/// discarding the overflowing bits.
636669
///
637670
/// var x: Int8 = 21
638671
/// x &-= 10
@@ -641,18 +674,25 @@ def assignmentOperatorComment(operator, fixedWidth):
641674
/// y &-= 21
642675
/// // y == 245 (after overflow)
643676
///
677+
/// For more about arithmetic with overflow operators, see [Overflow
678+
/// Operators][overflow] in *[The Swift Programming Language][tspl]*.
679+
///
680+
/// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
681+
/// [tspl]: https://docs.swift.org/swift-book/
682+
///
644683
/// - Parameters:
645684
/// - lhs: A numeric value.
646685
/// - rhs: The value to subtract from `lhs`.
647686
""",
648687
'&*': """\
649688
/// Multiplies two values and stores the result in the left-hand-side
650-
/// variable, discarding any overflow.
689+
/// variable, wrapping any overflow.
651690
///
652-
/// The masking multiplication assignment operator (`&*=`) silently discards
653-
/// any overflow that occurs during the operation. In the following example,
654-
/// the product of `10` and `50` is greater than the maximum representable
655-
/// `Int8` value, so the result is the overflowed value:
691+
/// The masking multiplication assignment operator (`&*=`) silently wraps
692+
/// any overflow that occurs during the operation. In the following example,
693+
/// the product of `10` and `50` is greater than the maximum representable
694+
/// `Int8` value, so the result is the partial value after discarding the
695+
/// overflowing bits.
656696
///
657697
/// var x: Int8 = 10
658698
/// x &*= 5
@@ -661,6 +701,12 @@ def assignmentOperatorComment(operator, fixedWidth):
661701
/// y &*= 50
662702
/// // y == -12 (after overflow)
663703
///
704+
/// For more about arithmetic with overflow operators, see [Overflow
705+
/// Operators][overflow] in *[The Swift Programming Language][tspl]*.
706+
///
707+
/// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
708+
/// [tspl]: https://docs.swift.org/swift-book/
709+
///
664710
/// - Parameters:
665711
/// - lhs: The first value to multiply.
666712
/// - rhs: The second value to multiply.
@@ -1173,6 +1219,15 @@ ${operatorComment(x.operator, False)}
11731219
/// declaring conformance to the protocol and ensuring that the values of your
11741220
/// type support negation. To customize your type's implementation, provide
11751221
/// your own mutating `negate()` method.
1222+
///
1223+
/// When the additive inverse of a value is unrepresentable in a conforming
1224+
/// type, the operation should either trap or return an exceptional value. For
1225+
/// example, using the negation operator (prefix `-`) with `Int.min` results in
1226+
/// a runtime error.
1227+
///
1228+
/// let x = Int.min
1229+
/// let y = -x
1230+
/// // Overflow error
11761231
public protocol SignedNumeric : Numeric {
11771232
/// Returns the additive inverse of the specified value.
11781233
///
@@ -1201,6 +1256,14 @@ public protocol SignedNumeric : Numeric {
12011256
/// var x = 21
12021257
/// x.negate()
12031258
/// // x == -21
1259+
///
1260+
/// The resulting value must be representable within the value's type. In
1261+
/// particular, negating a signed, fixed-width integer type's minimum
1262+
/// results in a value that cannot be represented.
1263+
///
1264+
/// var y = Int8.min
1265+
/// y.negate()
1266+
/// // Overflow error
12041267
mutating func negate()
12051268
}
12061269

@@ -1238,6 +1301,14 @@ extension SignedNumeric {
12381301
/// var x = 21
12391302
/// x.negate()
12401303
/// // x == -21
1304+
///
1305+
/// The resulting value must be representable within the value's type. In
1306+
/// particular, negating a signed, fixed-width integer type's minimum
1307+
/// results in a value that cannot be represented.
1308+
///
1309+
/// var y = Int8.min
1310+
/// y.negate()
1311+
/// // Overflow error
12411312
@inlinable // FIXME(sil-serialize-all)
12421313
@_transparent
12431314
public mutating func negate() {
@@ -1680,7 +1751,7 @@ ${assignmentOperatorComment(x.nonMaskingOperator, False)}
16801751
///
16811752
/// - Parameter rhs: The value to divide this value by.
16821753
/// - Returns: A tuple containing the quotient and remainder of this value
1683-
/// divided by `rhs`.
1754+
/// divided by `rhs`. The remainder has the same sign as `rhs`.
16841755
func quotientAndRemainder(dividingBy rhs: Self)
16851756
-> (quotient: Self, remainder: Self)
16861757

@@ -2618,7 +2689,7 @@ extension FixedWidthInteger {
26182689
/// // Prints "64"
26192690
/// // Prints "5"
26202691
///
2621-
/// This method is equivalent to calling the version that takes a generator,
2692+
/// This method is equivalent to calling the version that takes a generator,
26222693
/// passing in the system's default random generator.
26232694
///
26242695
/// - Parameter range: The range in which to create a random value.

0 commit comments

Comments
 (0)