Skip to content

Commit 53004a1

Browse files
author
Max Moiseev
committed
[stdlib] Renames and tests for Swift 3 compatibility mode
Addresses <rdar://problem/32432481>
1 parent 8022266 commit 53004a1

File tree

3 files changed

+105
-14
lines changed

3 files changed

+105
-14
lines changed

stdlib/public/core/Integers.swift.gyb

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3506,23 +3506,23 @@ internal func _unsafeMinus(_ lhs: Int, _ rhs: Int) -> Int {
35063506

35073507
// Swift 3 compatibility APIs
35083508

3509-
@available(swift, obsoleted: 4)
3509+
@available(swift, obsoleted: 4, renamed: "BinaryInteger")
35103510
public typealias Integer = BinaryInteger
35113511

3512-
@available(swift, obsoleted: 4)
3512+
@available(swift, obsoleted: 4, renamed: "BinaryInteger")
35133513
public typealias IntegerArithmetic = BinaryInteger
35143514

3515-
@available(swift, obsoleted: 4)
3515+
@available(swift, obsoleted: 4, message: "Please use 'SignedNumeric & Comparable' instead.")
35163516
public typealias SignedNumber = SignedNumeric & Comparable
35173517

3518-
@available(swift, obsoleted: 4)
3518+
@available(swift, obsoleted: 4, message: "Please use 'SignedNumeric & Comparable' instead.")
35193519
public typealias AbsoluteValuable = SignedNumeric & Comparable
35203520

3521-
@available(swift, obsoleted: 4)
3521+
@available(swift, obsoleted: 4, renamed: "SignedInteger")
35223522
public typealias _SignedInteger = SignedInteger
35233523

35243524
extension SignedNumeric where Self : Comparable {
3525-
@available(swift, obsoleted: 4)
3525+
@available(swift, obsoleted: 4, message: "Please use the 'abs(_:)' free function.")
35263526
@_transparent
35273527
public static func abs(_ x: Self) -> Self {
35283528
return Swift.abs(x)
@@ -3576,14 +3576,18 @@ extension FixedWidthInteger {
35763576

35773577
}
35783578

3579+
%{
3580+
overflowingOps = [
3581+
('add', 'adding', ''),
3582+
('subtract', 'subtracting', ''),
3583+
('multiply', 'multiplied', 'by:'),
3584+
('divide', 'divided', 'by:'),
3585+
('remainder', 'remainder', 'dividingBy:'),
3586+
]
3587+
}%
3588+
35793589
extension FixedWidthInteger {
3580-
% for oldPrefix, newPrefix, argLabel in [
3581-
% ('add', 'adding', ''),
3582-
% ('subtract', 'subtracting', ''),
3583-
% ('multiply', 'multiplied', 'by:'),
3584-
% ('divide', 'divided', 'by:'),
3585-
% ('remainder', 'remainder', 'dividingBy:'),
3586-
% ]:
3590+
% for oldPrefix, newPrefix, argLabel in overflowingOps:
35873591
@available(swift, obsoleted: 4, message: "Use ${newPrefix}ReportingOverflow(${argLabel or '_:'}) instead.")
35883592
@_transparent
35893593
public static func ${oldPrefix}WithOverflow(
@@ -3597,6 +3601,18 @@ extension FixedWidthInteger {
35973601
% end
35983602
}
35993603

3604+
extension BinaryInteger {
3605+
% for oldPrefix, newPrefix, argLabel in overflowingOps:
3606+
@available(swift, obsoleted: 3.2,
3607+
message: "Please use FixedWidthInteger protocol as a generic constraint and ${newPrefix}ReportingOverflow(${argLabel or '_:'}) method instead.")
3608+
public static func ${oldPrefix}WithOverflow(
3609+
_ lhs: Self, _ rhs: Self
3610+
) -> (Self, overflow: Bool) {
3611+
fatalError("Unavailable")
3612+
}
3613+
% end
3614+
}
3615+
36003616
// FIXME(integers): Absence of &+ causes ambiguity in the code like the
36013617
// following:
36023618
// func f<T : SignedInteger>(_ x: T, _ y: T) {
@@ -3615,7 +3631,8 @@ extension SignedInteger {
36153631
fatalError("Should be overridden in a more specific type")
36163632
}
36173633

3618-
@available(swift, obsoleted: 4.0)
3634+
@available(swift, obsoleted: 4.0,
3635+
message: "Please use 'FixedWidthInteger' instead of 'SignedInteger' to get '${op}' in generic code.")
36193636
public static func ${op} (lhs: Self, rhs: Self) -> Self {
36203637
return ${helper}(lhs, rhs)
36213638
}

test/stdlib/IntegerRenames3.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 3
2+
3+
func f<T : SignedInteger>(x: T) {
4+
_ = T.addWithOverflow(x, x) // expected-error {{'addWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and addingReportingOverflow(_:) method instead.}}
5+
_ = T.subtractWithOverflow(x, x) // expected-error {{'subtractWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and subtractingReportingOverflow(_:) method instead.}}
6+
_ = T.multiplyWithOverflow(x, x) // expected-error {{'multiplyWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and multipliedReportingOverflow(by:) method instead.}}
7+
_ = T.divideWithOverflow(x, x) // expected-error {{'divideWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and dividedReportingOverflow(by:) method instead.}}
8+
_ = T.remainderWithOverflow(x, x) // expected-error {{'remainderWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and remainderReportingOverflow(dividingBy:) method instead.}}
9+
}
10+
11+
func f<T : FixedWidthInteger>(x: T) {
12+
_ = T.addWithOverflow(x, x) // no error
13+
_ = T.subtractWithOverflow(x, x) // no error
14+
_ = T.multiplyWithOverflow(x, x) // no error
15+
_ = T.divideWithOverflow(x, x) // no error
16+
_ = T.remainderWithOverflow(x, x) // no error
17+
}
18+
19+
do {
20+
let _: IntMax = 0 // no error
21+
let _: UIntMax = 0 // no error
22+
}
23+
24+
func integer<T : Integer>(x: T) {} // no error
25+
func integerArithmetic<T : IntegerArithmetic>(x: T) {} // no error
26+
func signedNumber<T : SignedNumber>(x: T) {} // no error
27+
func absoluteValuable<T : AbsoluteValuable>(x: T) {} // no error
28+
func _signedInteger<T : _SignedInteger>(x: T) {} // no error
29+
30+
func absolutaValuable<T : SignedNumeric & Comparable>(x: T) {
31+
_ = T.abs(x) // no error
32+
}
33+
34+
func signedIntegerMaskingArithmetics<T : SignedInteger>(x: T) {
35+
_ = x &+ x // no error
36+
_ = x &- x // no error
37+
}

test/stdlib/IntegerRenames4.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 4
2+
3+
func f<T : SignedInteger>(x: T) {
4+
_ = T.addWithOverflow(x, x) // expected-error {{'addWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and addingReportingOverflow(_:) method instead.}}
5+
_ = T.subtractWithOverflow(x, x) // expected-error {{'subtractWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and subtractingReportingOverflow(_:) method instead.}}
6+
_ = T.multiplyWithOverflow(x, x) // expected-error {{'multiplyWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and multipliedReportingOverflow(by:) method instead.}}
7+
_ = T.divideWithOverflow(x, x) // expected-error {{'divideWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and dividedReportingOverflow(by:) method instead.}}
8+
_ = T.remainderWithOverflow(x, x) // expected-error {{'remainderWithOverflow' is unavailable: Please use FixedWidthInteger protocol as a generic constraint and remainderReportingOverflow(dividingBy:) method instead.}}
9+
}
10+
11+
func f<T : FixedWidthInteger>(x: T) {
12+
_ = T.addWithOverflow(x, x) // expected-error {{'addWithOverflow' is unavailable: Use addingReportingOverflow(_:) instead.}}
13+
_ = T.subtractWithOverflow(x, x) // expected-error {{'subtractWithOverflow' is unavailable: Use subtractingReportingOverflow(_:) instead.}}
14+
_ = T.multiplyWithOverflow(x, x) // expected-error {{'multiplyWithOverflow' is unavailable: Use multipliedReportingOverflow(by:) instead.}}
15+
_ = T.divideWithOverflow(x, x) // expected-error {{'divideWithOverflow' is unavailable: Use dividedReportingOverflow(by:) instead.}}
16+
_ = T.remainderWithOverflow(x, x) // expected-error {{'remainderWithOverflow' is unavailable: Use remainderReportingOverflow(dividingBy:) instead.}}
17+
}
18+
19+
do {
20+
let _: IntMax = 0 // expected-error {{'IntMax' has been renamed to 'Int64'}}
21+
let _: UIntMax = 0 // expected-error {{'UIntMax' has been renamed to 'UInt64'}}
22+
}
23+
24+
func integer<T : Integer>(x: T) {} // expected-error {{'Integer' has been renamed to 'BinaryInteger'}}
25+
func integerArithmetic<T : IntegerArithmetic>(x: T) {} // expected-error {{'IntegerArithmetic' has been renamed to 'BinaryInteger'}}
26+
func signedNumber<T : SignedNumber>(x: T) {} // expected-error {{Please use 'SignedNumeric & Comparable' instead.}}
27+
func absoluteValuable<T : AbsoluteValuable>(x: T) {} // expected-error {{Please use 'SignedNumeric & Comparable' instead.}}
28+
func _signedInteger<T : _SignedInteger>(x: T) {} // expected-error {{'_SignedInteger' has been renamed to 'SignedInteger'}}
29+
30+
func absolutaValuable<T : SignedNumeric & Comparable>(x: T) {
31+
_ = T.abs(x) // expected-error {{use the 'abs(_:)' free function}}
32+
}
33+
34+
func signedIntegerMaskingArithmetics<T : SignedInteger>(x: T) {
35+
_ = x &+ x // expected-error {{use 'FixedWidthInteger' instead of 'SignedInteger' to get '&+' in generic code}}
36+
_ = x &- x // expected-error {{use 'FixedWidthInteger' instead of 'SignedInteger' to get '&-' in generic code}}
37+
}

0 commit comments

Comments
 (0)