Skip to content

Commit c88d4e4

Browse files
Remove unsafe arithmetic operations deprecated in 4.2 (#19426)
1 parent 649d310 commit c88d4e4

File tree

4 files changed

+29
-129
lines changed

4 files changed

+29
-129
lines changed

stdlib/public/core/IntegerTypes.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ def assignmentOperatorComment(operator, fixedWidth):
911911
# by the related operator:
912912
# + addingReportingOverflow(_:)
913913
# - subtractingReportingOverflow(_:)
914-
# * multiplyingReportingOverflow(_:)
914+
# * multipliedReportingOverflow(by:)
915915
# / dividedReportingOverflow(by:)
916916
# % remainderReportingOverflow(dividingBy:)
917917
def overflowOperationComment(operator):

stdlib/public/core/Integers.swift

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,134 +2995,6 @@ extension FixedWidthInteger {
29952995
else { self = Self(truncatingIfNeeded: source) }
29962996
}
29972997

2998-
/// Returns the sum of this value and the given value without checking for
2999-
/// arithmetic overflow.
3000-
///
3001-
/// Use this function only to avoid the cost of overflow checking when you
3002-
/// are certain that the operation won't overflow. In optimized builds (`-O`)
3003-
/// the compiler is free to assume that overflow won't occur. Failure to
3004-
/// satisfy that assumption is a serious programming error and could lead to
3005-
/// statements being unexpectedly executed or skipped.
3006-
///
3007-
/// In debug builds (`-Onone`) a runtime error is still triggered if the
3008-
/// operation overflows.
3009-
///
3010-
/// This method is not a synonym for the masking addition operator (`&+`).
3011-
/// Use that operator instead of this method when you want to discard any
3012-
/// overflow that results from an addition operation.
3013-
///
3014-
/// - Parameter rhs: The value to add to this value.
3015-
/// - Returns: The sum of this value and `rhs`.
3016-
@inline(__always)
3017-
public func unsafeAdding(_ other: Self) -> Self {
3018-
let (result, overflow) = self.addingReportingOverflow(other)
3019-
3020-
if overflow {
3021-
if (_isDebugAssertConfiguration()) {
3022-
_preconditionFailure("Overflow in unsafeAdding")
3023-
}
3024-
else {
3025-
Builtin.conditionallyUnreachable()
3026-
}
3027-
}
3028-
return result
3029-
}
3030-
3031-
/// Returns the difference obtained by subtracting the given value from this
3032-
/// value without checking for arithmetic overflow.
3033-
///
3034-
/// Use this function only to avoid the cost of overflow checking when you
3035-
/// are certain that the operation won't overflow. In optimized builds (`-O`)
3036-
/// the compiler is free to assume that overflow won't occur. Failure to
3037-
/// satisfy that assumption is a serious programming error and could lead to
3038-
/// statements being unexpectedly executed or skipped.
3039-
///
3040-
/// In debug builds (`-Onone`) a runtime error is still triggered if the
3041-
/// operation overflows.
3042-
///
3043-
/// This method is not a synonym for the masking subtraction operator (`&-`).
3044-
/// Use that operator instead of this method when you want to discard any
3045-
/// overflow that results from a subtraction operation.
3046-
///
3047-
/// - Parameter rhs: The value to subtract from this value.
3048-
/// - Returns: The result of subtracting `rhs` from this value.
3049-
@inline(__always)
3050-
public func unsafeSubtracting(_ other: Self) -> Self {
3051-
let (result, overflow) = self.subtractingReportingOverflow(other)
3052-
3053-
if overflow {
3054-
if (_isDebugAssertConfiguration()) {
3055-
_preconditionFailure("Overflow in unsafeSubtracting")
3056-
}
3057-
else {
3058-
Builtin.conditionallyUnreachable()
3059-
}
3060-
}
3061-
return result
3062-
}
3063-
3064-
/// Returns the product of this value and the given value without checking
3065-
/// for arithmetic overflow.
3066-
///
3067-
/// Use this function only to avoid the cost of overflow checking when you
3068-
/// are certain that the operation won't overflow. In optimized builds (`-O`)
3069-
/// the compiler is free to assume that overflow won't occur. Failure to
3070-
/// satisfy that assumption is a serious programming error and could lead to
3071-
/// statements being unexpectedly executed or skipped.
3072-
///
3073-
/// In debug builds (`-Onone`) a runtime error is still triggered if the
3074-
/// operation overflows.
3075-
///
3076-
/// This method is not a synonym for the masking multiplication operator
3077-
/// (`&*`). Use that operator instead of this method when you want to discard
3078-
/// any overflow that results from an addition operation.
3079-
///
3080-
/// - Parameter rhs: The value to multiply by this value.
3081-
/// - Returns: The product of this value and `rhs`.
3082-
@inline(__always)
3083-
public func unsafeMultiplied(by other: Self) -> Self {
3084-
let (result, overflow) = self.multipliedReportingOverflow(by: other)
3085-
3086-
if overflow {
3087-
if (_isDebugAssertConfiguration()) {
3088-
_preconditionFailure("Overflow in unsafeMultiplied")
3089-
}
3090-
else {
3091-
Builtin.conditionallyUnreachable()
3092-
}
3093-
}
3094-
return result
3095-
}
3096-
3097-
/// Returns the quotient obtained by dividing this value by the given value
3098-
/// without checking for arithmetic overflow.
3099-
///
3100-
/// Use this function only to avoid the cost of overflow checking when you
3101-
/// are certain that the operation won't overflow. In optimized builds (`-O`)
3102-
/// the compiler is free to assume that overflow won't occur. Failure to
3103-
/// satisfy that assumption is a serious programming error and could lead to
3104-
/// statements being unexpectedly executed or skipped.
3105-
///
3106-
/// In debug builds (`-Onone`) a runtime error is still triggered if the
3107-
/// operation overflows.
3108-
///
3109-
/// - Parameter rhs: The value to divide this value by.
3110-
/// - Returns: The result of dividing this value by `rhs`.
3111-
@inline(__always)
3112-
public func unsafeDivided(by other: Self) -> Self {
3113-
let (result, overflow) = self.dividedReportingOverflow(by: other)
3114-
3115-
if overflow {
3116-
if (_isDebugAssertConfiguration()) {
3117-
_preconditionFailure("Overflow in unsafeDivided")
3118-
}
3119-
else {
3120-
Builtin.conditionallyUnreachable()
3121-
}
3122-
}
3123-
return result
3124-
}
3125-
31262998
/// Creates a new instance from the bit pattern of the given instance by
31272999
/// truncating or sign-extending if needed to fit this type.
31283000
///

stdlib/public/core/MigrationSupport.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,30 @@ extension FixedWidthInteger {
383383
/// The empty bitset.
384384
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Use 0")
385385
public static var allZeros: Self { return 0 }
386+
387+
@available(*, unavailable, message:
388+
"Use operators or addingReportingOverflow instead.")
389+
public func unsafeAdding(_ other: Self) -> Self {
390+
fatalError("unavailable")
391+
}
392+
393+
@available(*, unavailable, message:
394+
"Use operators or subtractingReportingOverflow instead.")
395+
public func unsafeSubtracting(_ other: Self) -> Self {
396+
fatalError("unavailable")
397+
}
398+
399+
@available(*, unavailable, message:
400+
"Use operators or multipliedReportingOverflow(by:) instead.")
401+
public func unsafeMultiplied(by other: Self) -> Self {
402+
fatalError("unavailable")
403+
}
404+
405+
@available(*, unavailable, message:
406+
"Use operators or dividedReportingOverflow(by:) instead.")
407+
public func unsafeDivided(by other: Self) -> Self {
408+
fatalError("unavailable")
409+
}
386410
}
387411

388412
extension LazyCollectionProtocol {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Func FixedWidthInteger.divideWithOverflow(_:_:) has been removed
2020
Func FixedWidthInteger.multiplyWithOverflow(_:_:) has been removed
2121
Func FixedWidthInteger.remainderWithOverflow(_:_:) has been removed
2222
Func FixedWidthInteger.subtractWithOverflow(_:_:) has been removed
23+
Func FixedWidthInteger.unsafeAdding(_:) has been removed
24+
Func FixedWidthInteger.unsafeDivided(by:) has been removed
25+
Func FixedWidthInteger.unsafeMultiplied(by:) has been removed
26+
Func FixedWidthInteger.unsafeSubtracting(_:) has been removed
2327
Func FloatingPoint.abs(_:) has been removed (deprecated)
2428
Func FloatingPoint.add(_:) has been removed
2529
Func FloatingPoint.adding(_:) has been removed

0 commit comments

Comments
 (0)