Skip to content

Commit

Permalink
Merge pull request swiftlang#8851 from moiseev/integer-fixes
Browse files Browse the repository at this point in the history
[stdlib] A few Swift 3 compatibility fixes
  • Loading branch information
moiseev authored Apr 20, 2017
2 parents e25da82 + a1cb7a8 commit 10da98a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 26 deletions.
26 changes: 26 additions & 0 deletions stdlib/public/SDK/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,32 @@ extension Decimal : SignedNumeric {
}
}

extension Decimal {
@available(swift, obsoleted: 4, message: "Please use arithmetic operators instead")
@_transparent
public mutating func add(_ other: Decimal) {
self += other
}

@available(swift, obsoleted: 4, message: "Please use arithmetic operators instead")
@_transparent
public mutating func subtract(_ other: Decimal) {
self -= other
}

@available(swift, obsoleted: 4, message: "Please use arithmetic operators instead")
@_transparent
public mutating func multiply(by other: Decimal) {
self *= other
}

@available(swift, obsoleted: 4, message: "Please use arithmetic operators instead")
@_transparent
public mutating func divide(by other: Decimal) {
self /= other
}
}

extension Decimal : Strideable {
public func distance(to other: Decimal) -> Decimal {
return self - other
Expand Down
48 changes: 25 additions & 23 deletions stdlib/public/core/FloatingPointTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -931,32 +931,34 @@ extension ${Self} {
// tweaking the overload resolution rules, or by removing the other
// definitions in the standard lib, or both.

@_transparent
public func + (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
var lhs = lhs
lhs += rhs
return lhs
}
extension ${Self} {
@_transparent
public static func + (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
var lhs = lhs
lhs += rhs
return lhs
}

@_transparent
public func - (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
var lhs = lhs
lhs -= rhs
return lhs
}
@_transparent
public static func - (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
var lhs = lhs
lhs -= rhs
return lhs
}

@_transparent
public func * (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
var lhs = lhs
lhs *= rhs
return lhs
}
@_transparent
public static func * (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
var lhs = lhs
lhs *= rhs
return lhs
}

@_transparent
public func / (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
var lhs = lhs
lhs /= rhs
return lhs
@_transparent
public static func / (lhs: ${Self}, rhs: ${Self}) -> ${Self} {
var lhs = lhs
lhs /= rhs
return lhs
}
}

//===----------------------------------------------------------------------===//
Expand Down
37 changes: 35 additions & 2 deletions stdlib/public/core/Integers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,8 @@ def overflowOperationComment(operator):
///
/// - SeeAlso: `doubleWidthDivide(_:_:)`
""",
'/': """\
'%': """\
// FIXME(integers): the comment is for division instead of remainder
/// Returns the remainder of dividing this value by the given value along with
/// a flag indicating whether overflow occurred in the operation.
///
Expand Down Expand Up @@ -2034,7 +2035,7 @@ public protocol FixedWidthInteger : BinaryInteger, BitwiseOperations {
/// exponentiation.
static var min: Self { get }

% for x in binaryArithmetic['Numeric'] + binaryArithmetic['BinaryInteger'][:1]:
% for x in binaryArithmetic['Numeric'] + binaryArithmetic['BinaryInteger']:
${overflowOperationComment(x.operator)}
func ${x.name}ReportingOverflow(
${x.firstArg} rhs: Self
Expand Down Expand Up @@ -3090,6 +3091,12 @@ ${operatorComment(x.operator, True)}
public func to${U}IntMax() -> ${U}IntMax {
return numericCast(self)
}

@available(swift, obsoleted: 4, message: "Use bitWidth instead.")
public static var _sizeInBits: ${Self} { return ${bits} }

@available(swift, obsoleted: 4)
public static var _sizeInBytes: ${Self} { return ${bits}/8 }
}
%# end of concrete type: ${Self}

Expand Down Expand Up @@ -3414,6 +3421,11 @@ public struct DoubleWidth<T : FixedWidthInteger>
fatalError()
}

public func remainderReportingOverflow(dividingBy other: DoubleWidth<T>)
-> (partialValue: DoubleWidth<T>, overflow: ArithmeticOverflow) {
fatalError()
}

public func multipliedFullWidth(by other: DoubleWidth<T>)
-> (high: DoubleWidth<T>, low: DoubleWidth<T>.Magnitude) {
fatalError()
Expand Down Expand Up @@ -3592,6 +3604,27 @@ extension FixedWidthInteger {

}

extension FixedWidthInteger {
% for oldPrefix, newPrefix, argLabel in [
% ('add', 'adding', ''),
% ('subtract', 'subtracting', ''),
% ('multiply', 'multiplied', 'by:'),
% ('divide', 'divided', 'by:'),
% ('remainder', 'remainder', 'dividingBy:'),
% ]:
@available(swift, obsoleted: 4, message: "Use ${newPrefix}ReportingOverflow(${argLabel or '_:'}) instead.")
@_transparent
public static func ${oldPrefix}WithOverflow(
_ lhs: Self, _ rhs: Self
) -> (Self, overflow: Bool) {
let (partialValue, overflow) =
lhs.${newPrefix}ReportingOverflow(${argLabel} rhs)
return (partialValue, overflow == .overflow)
}

% end
}

// FIXME(integers): Absence of &+ causes ambiguity in the code like the
// following:
// func f<T : SignedInteger>(_ x: T, _ y: T) {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Stride.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public func == <T : Strideable>(x: T, y: T) -> Bool {
}%
% for Base, VersionInfo in [
% ('Strideable where Self : _Pointer', None),
% ('Strideable where Stride : SignedNumeric', 'deprecated: 3, obsoleted: 4'),
% ('Strideable', 'deprecated: 3, obsoleted: 4'),
% ]:
% Availability = '@available(swift, %s, message: "Please use explicit type conversions or Strideable methods for mixed-type arithmetics.")' % (VersionInfo) if VersionInfo else ''

Expand Down
5 changes: 5 additions & 0 deletions test/Prototypes/BigInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,11 @@ struct Bit : FixedWidthInteger, UnsignedInteger {
return rhs == 0 ? (self, .none) : (self, .overflow)
}

func remainderReportingOverflow(dividingBy rhs: Bit) ->
(partialValue: Bit, overflow: ArithmeticOverflow) {
fatalError()
}

static func +=(lhs: inout Bit, rhs: Bit) {
let result = lhs.addingReportingOverflow(rhs)
assert(result.overflow == .none, "Addition overflow")
Expand Down

0 comments on commit 10da98a

Please sign in to comment.