Skip to content

Commit f462ce7

Browse files
authored
Deprecating + and - for SignedInteger and its Stride (#6603)
* Removing uses of mixed-type + and - in benchmarks Using type cast or explicit type annotations. * Deprecating use of + and - on SignedInteger As it leads to mixed type arithmetics that is not supposed to work. It was needed before the new collection indexing model to make moving indexes simple. * Test deprecation warning
1 parent f30181f commit f462ce7

File tree

5 files changed

+80
-18
lines changed

5 files changed

+80
-18
lines changed

benchmark/single-source/Hash.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class SHA1 : Hash {
343343

344344
// Append the original message length as 64bit big endian:
345345
let len_in_bits = Int64(messageLength)*8
346-
for i in 0..<8 {
346+
for i in 0..<(8 as Int64) {
347347
let val = (len_in_bits >> ((7-i)*8)) & 0xFF
348348
data[dataLength] = UInt8(val)
349349
dataLength += 1
@@ -481,7 +481,7 @@ class SHA256 : Hash {
481481

482482
// Append the original message length as 64bit big endian:
483483
let len_in_bits = Int64(messageLength)*8
484-
for i in 0..<8 {
484+
for i in 0..<(8 as Int64) {
485485
let val = (len_in_bits >> ((7-i)*8)) & 0xFF
486486
data[dataLength] = UInt8(val)
487487
dataLength += 1

benchmark/single-source/PolymorphicCalls.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public class F3 : B3 {
247247
func test(_ a:A, _ UPTO: Int) -> Int64 {
248248
var cnt: Int64 = 0
249249
for _ in 0..<UPTO {
250-
cnt += a.run2()
250+
cnt += Int64(a.run2())
251251
}
252252
return cnt
253253
}
@@ -258,7 +258,7 @@ func test(_ a:A, _ UPTO: Int) -> Int64 {
258258
func test(_ a:A1, _ UPTO: Int) -> Int64 {
259259
var cnt: Int64 = 0
260260
for _ in 0..<UPTO {
261-
cnt += a.run2()
261+
cnt += Int64(a.run2())
262262
}
263263
return cnt
264264
}
@@ -269,7 +269,7 @@ func test(_ a:A1, _ UPTO: Int) -> Int64 {
269269
func test(_ a:A2, _ UPTO: Int) -> Int64 {
270270
var cnt: Int64 = 0
271271
for _ in 0..<UPTO {
272-
cnt += a.run2()
272+
cnt += Int64(a.run2())
273273
}
274274
return cnt
275275
}
@@ -281,8 +281,8 @@ func test(_ a:A2, _ UPTO: Int) -> Int64 {
281281
func test(_ a2_c2:A2, _ a2_d2:A2, _ UPTO: Int) -> Int64 {
282282
var cnt: Int64 = 0
283283
for _ in 0..<UPTO/2 {
284-
cnt += a2_c2.run2()
285-
cnt += a2_d2.run2()
284+
cnt += Int64(a2_c2.run2())
285+
cnt += Int64(a2_d2.run2())
286286
}
287287
return cnt
288288
}
@@ -294,10 +294,10 @@ func test(_ a2_c2:A2, _ a2_d2:A2, _ UPTO: Int) -> Int64 {
294294
func test(_ a3_c3: A3, _ a3_d3: A3, _ a3_e3: A3, _ a3_f3: A3, _ UPTO: Int) -> Int64 {
295295
var cnt: Int64 = 0
296296
for _ in 0..<UPTO/4 {
297-
cnt += a3_c3.run2()
298-
cnt += a3_d3.run2()
299-
cnt += a3_e3.run2()
300-
cnt += a3_f3.run2()
297+
cnt += Int64(a3_c3.run2())
298+
cnt += Int64(a3_d3.run2())
299+
cnt += Int64(a3_e3.run2())
300+
cnt += Int64(a3_f3.run2())
301301
}
302302
return cnt
303303
}

stdlib/public/core/Stride.swift.gyb

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,57 @@ public func == <T : Strideable>(x: T, y: T) -> Bool {
6262
return x.distance(to: y) == 0
6363
}
6464

65-
public func + <T : Strideable>(lhs: T, rhs: T.Stride) -> T {
65+
% for Base in ['Strideable', 'SignedInteger']:
66+
% deprecated = Base == 'SignedInteger'
67+
% # FIXME: technically the deprecation version is incorrect and should be 3.1
68+
% # instead but due to <rdar://problem/29884401> it does not work as
69+
% # expected.
70+
% DeprecationVersion = '3.0'
71+
72+
% if deprecated:
73+
@available(swift, deprecated: ${DeprecationVersion}, obsoleted: 4.0, message: "Mixed-type addition is deprecated. Please use explicit type conversion.")
74+
% end
75+
public func + <T : ${Base}>(lhs: T, rhs: T.Stride) -> T {
6676
return lhs.advanced(by: rhs)
6777
}
6878

69-
public func + <T : Strideable>(lhs: T.Stride, rhs: T) -> T {
79+
% if deprecated:
80+
@available(swift, deprecated: ${DeprecationVersion}, obsoleted: 4.0, message: "Mixed-type addition is deprecated. Please use explicit type conversion.")
81+
% end
82+
public func + <T : ${Base}>(lhs: T.Stride, rhs: T) -> T {
7083
return rhs.advanced(by: lhs)
7184
}
7285

73-
public func - <T : Strideable>(lhs: T, rhs: T.Stride) -> T {
86+
% if deprecated:
87+
@available(swift, deprecated: ${DeprecationVersion}, obsoleted: 4.0, message: "Mixed-type subtraction is deprecated. Please use explicit type conversion.")
88+
% end
89+
public func - <T : ${Base}>(lhs: T, rhs: T.Stride) -> T {
7490
return lhs.advanced(by: -rhs)
7591
}
7692

77-
public func - <T : Strideable>(lhs: T, rhs: T) -> T.Stride {
93+
% if deprecated:
94+
@available(swift, deprecated: ${DeprecationVersion}, obsoleted: 4.0, message: "Mixed-type subtraction is deprecated. Please use explicit type conversion.")
95+
% end
96+
public func - <T : ${Base}>(lhs: T, rhs: T) -> T.Stride {
7897
return rhs.distance(to: lhs)
7998
}
8099

81-
public func += <T : Strideable>(lhs: inout T, rhs: T.Stride) {
100+
% if deprecated:
101+
@available(swift, deprecated: ${DeprecationVersion}, obsoleted: 4.0, message: "Mixed-type addition is deprecated. Please use explicit type conversion.")
102+
% end
103+
public func += <T : ${Base}>(lhs: inout T, rhs: T.Stride) {
82104
lhs = lhs.advanced(by: rhs)
83105
}
84106

85-
public func -= <T : Strideable>(lhs: inout T, rhs: T.Stride) {
107+
% if deprecated:
108+
@available(swift, deprecated: ${DeprecationVersion}, obsoleted: 4.0, message: "Mixed-type subtraction is deprecated. Please use explicit type conversion.")
109+
% end
110+
public func -= <T : ${Base}>(lhs: inout T, rhs: T.Stride) {
86111
lhs = lhs.advanced(by: -rhs)
87112
}
88113

114+
% end
115+
89116
//===--- Deliberately-ambiguous operators for UnsignedIntegerTypes --------===//
90117
// The UnsignedIntegerTypes all have a signed Stride type. Without these //
91118
// overloads, expressions such as UInt(2) + Int(3) would compile. //

test/ClangImporter/cstring_parse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
import cfuncs
1111

1212
func test_puts(_ s: String) {
13-
_ = puts(s) + 32
13+
_ = puts(s) + (32 as Int32)
1414
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- MixedTypeArithmeticsDiagnostics.swift ----------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// RUN: %target-typecheck-verify-swift
13+
14+
15+
func mixedTypeArithemtics() {
16+
_ = (42 as Int64) + (0 as Int) // expected-warning {{'+' is deprecated:}}
17+
18+
do {
19+
var x = Int8()
20+
x += (42 as Int) // expected-warning {{'+=' is deprecated:}}
21+
}
22+
23+
_ = (42 as Int32) - (0 as Int) // expected-warning {{'-' is deprecated:}}
24+
25+
do {
26+
var x = Int16()
27+
x -= (42 as Int) // expected-warning {{'-=' is deprecated:}}
28+
}
29+
30+
// With Int on both sides should NOT result in warning
31+
do {
32+
var x = Int()
33+
x += (42 as Int)
34+
}
35+
}

0 commit comments

Comments
 (0)