Skip to content

Remove unsafe arithmetic operations deprecated in 4.2 #19426

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
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
2 changes: 1 addition & 1 deletion stdlib/public/core/IntegerTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ def assignmentOperatorComment(operator, fixedWidth):
# by the related operator:
# + addingReportingOverflow(_:)
# - subtractingReportingOverflow(_:)
# * multiplyingReportingOverflow(_:)
# * multipliedReportingOverflow(by:)
# / dividedReportingOverflow(by:)
# % remainderReportingOverflow(dividingBy:)
def overflowOperationComment(operator):
Expand Down
128 changes: 0 additions & 128 deletions stdlib/public/core/Integers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2995,134 +2995,6 @@ extension FixedWidthInteger {
else { self = Self(truncatingIfNeeded: source) }
}

/// Returns the sum of this value and the given value without checking for
/// arithmetic overflow.
///
/// Use this function only to avoid the cost of overflow checking when you
/// are certain that the operation won't overflow. In optimized builds (`-O`)
/// the compiler is free to assume that overflow won't occur. Failure to
/// satisfy that assumption is a serious programming error and could lead to
/// statements being unexpectedly executed or skipped.
///
/// In debug builds (`-Onone`) a runtime error is still triggered if the
/// operation overflows.
///
/// This method is not a synonym for the masking addition operator (`&+`).
/// Use that operator instead of this method when you want to discard any
/// overflow that results from an addition operation.
///
/// - Parameter rhs: The value to add to this value.
/// - Returns: The sum of this value and `rhs`.
@inline(__always)
public func unsafeAdding(_ other: Self) -> Self {
let (result, overflow) = self.addingReportingOverflow(other)

if overflow {
if (_isDebugAssertConfiguration()) {
_preconditionFailure("Overflow in unsafeAdding")
}
else {
Builtin.conditionallyUnreachable()
}
}
return result
}

/// Returns the difference obtained by subtracting the given value from this
/// value without checking for arithmetic overflow.
///
/// Use this function only to avoid the cost of overflow checking when you
/// are certain that the operation won't overflow. In optimized builds (`-O`)
/// the compiler is free to assume that overflow won't occur. Failure to
/// satisfy that assumption is a serious programming error and could lead to
/// statements being unexpectedly executed or skipped.
///
/// In debug builds (`-Onone`) a runtime error is still triggered if the
/// operation overflows.
///
/// This method is not a synonym for the masking subtraction operator (`&-`).
/// Use that operator instead of this method when you want to discard any
/// overflow that results from a subtraction operation.
///
/// - Parameter rhs: The value to subtract from this value.
/// - Returns: The result of subtracting `rhs` from this value.
@inline(__always)
public func unsafeSubtracting(_ other: Self) -> Self {
let (result, overflow) = self.subtractingReportingOverflow(other)

if overflow {
if (_isDebugAssertConfiguration()) {
_preconditionFailure("Overflow in unsafeSubtracting")
}
else {
Builtin.conditionallyUnreachable()
}
}
return result
}

/// Returns the product of this value and the given value without checking
/// for arithmetic overflow.
///
/// Use this function only to avoid the cost of overflow checking when you
/// are certain that the operation won't overflow. In optimized builds (`-O`)
/// the compiler is free to assume that overflow won't occur. Failure to
/// satisfy that assumption is a serious programming error and could lead to
/// statements being unexpectedly executed or skipped.
///
/// In debug builds (`-Onone`) a runtime error is still triggered if the
/// operation overflows.
///
/// This method is not a synonym for the masking multiplication operator
/// (`&*`). Use that operator instead of this method when you want to discard
/// any overflow that results from an addition operation.
///
/// - Parameter rhs: The value to multiply by this value.
/// - Returns: The product of this value and `rhs`.
@inline(__always)
public func unsafeMultiplied(by other: Self) -> Self {
let (result, overflow) = self.multipliedReportingOverflow(by: other)

if overflow {
if (_isDebugAssertConfiguration()) {
_preconditionFailure("Overflow in unsafeMultiplied")
}
else {
Builtin.conditionallyUnreachable()
}
}
return result
}

/// Returns the quotient obtained by dividing this value by the given value
/// without checking for arithmetic overflow.
///
/// Use this function only to avoid the cost of overflow checking when you
/// are certain that the operation won't overflow. In optimized builds (`-O`)
/// the compiler is free to assume that overflow won't occur. Failure to
/// satisfy that assumption is a serious programming error and could lead to
/// statements being unexpectedly executed or skipped.
///
/// In debug builds (`-Onone`) a runtime error is still triggered if the
/// operation overflows.
///
/// - Parameter rhs: The value to divide this value by.
/// - Returns: The result of dividing this value by `rhs`.
@inline(__always)
public func unsafeDivided(by other: Self) -> Self {
let (result, overflow) = self.dividedReportingOverflow(by: other)

if overflow {
if (_isDebugAssertConfiguration()) {
_preconditionFailure("Overflow in unsafeDivided")
}
else {
Builtin.conditionallyUnreachable()
}
}
return result
}

/// Creates a new instance from the bit pattern of the given instance by
/// truncating or sign-extending if needed to fit this type.
///
Expand Down
24 changes: 24 additions & 0 deletions stdlib/public/core/MigrationSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,30 @@ extension FixedWidthInteger {
/// The empty bitset.
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Use 0")
public static var allZeros: Self { return 0 }

@available(*, unavailable, message:
"Use operators or addingReportingOverflow instead.")
public func unsafeAdding(_ other: Self) -> Self {
fatalError("unavailable")
}

@available(*, unavailable, message:
"Use operators or subtractingReportingOverflow instead.")
public func unsafeSubtracting(_ other: Self) -> Self {
fatalError("unavailable")
}

@available(*, unavailable, message:
"Use operators or multipliedReportingOverflow(by:) instead.")
public func unsafeMultiplied(by other: Self) -> Self {
fatalError("unavailable")
}

@available(*, unavailable, message:
"Use operators or dividedReportingOverflow(by:) instead.")
public func unsafeDivided(by other: Self) -> Self {
fatalError("unavailable")
}
}

extension LazyCollectionProtocol {
Expand Down
4 changes: 4 additions & 0 deletions test/api-digester/source-stability.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Func FixedWidthInteger.divideWithOverflow(_:_:) has been removed
Func FixedWidthInteger.multiplyWithOverflow(_:_:) has been removed
Func FixedWidthInteger.remainderWithOverflow(_:_:) has been removed
Func FixedWidthInteger.subtractWithOverflow(_:_:) has been removed
Func FixedWidthInteger.unsafeAdding(_:) has been removed
Func FixedWidthInteger.unsafeDivided(by:) has been removed
Func FixedWidthInteger.unsafeMultiplied(by:) has been removed
Func FixedWidthInteger.unsafeSubtracting(_:) has been removed
Func FloatingPoint.abs(_:) has been removed (deprecated)
Func FloatingPoint.add(_:) has been removed
Func FloatingPoint.adding(_:) has been removed
Expand Down