Skip to content

Commit 54e5f5d

Browse files
author
Max Moiseev
committed
Handling the differences in << and >> for Swift 3 vs Swift 4
In Swift 3 shifts used to be defined on the concrete integer types, so the right-hand-side value in the shift expression could define a type for the result, as in `1 << i32` would have the type Int32. Swift 4 makes shift operators heterogeneous, so now `1 << i32` will result in an Int, according to the type of the left-hand-side value, which gets a default type for integer literals.
1 parent f106bd9 commit 54e5f5d

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

stdlib/public/core/Integers.swift.gyb

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,12 +2191,13 @@ extension FixedWidthInteger {
21912191
//===----------------------------------------------------------------------===//
21922192

21932193
${operatorComment(x.nonMaskingOperator, True)}
2194+
@available(swift, introduced: 4)
21942195
@_transparent
21952196
public static func ${x.nonMaskingOperator} <
21962197
Other : BinaryInteger
21972198
>(lhs: Self, rhs: Other) -> Self {
21982199
var lhs = lhs
2199-
lhs ${x.nonMaskingOperator}= rhs
2200+
${x.helper}Generic(&lhs, rhs)
22002201
return lhs
22012202
}
22022203

@@ -2214,6 +2215,7 @@ ${operatorComment(x.nonMaskingOperator, True)}
22142215
}
22152216
#endif
22162217

2218+
@available(swift, introduced: 4)
22172219
@_transparent
22182220
public static func ${x.nonMaskingOperator}= <
22192221
Other : BinaryInteger
@@ -3182,29 +3184,6 @@ public func _assumeNonNegative(_ x: ${Self}) -> ${Self} {
31823184
}
31833185
% end
31843186

3185-
extension ${Self} {
3186-
3187-
% for op in maskingShifts:
3188-
3189-
public static func ${op.nonMaskingOperator}(
3190-
lhs: ${Self}, rhs: ${Self}
3191-
) -> ${Self} {
3192-
var lhs = lhs
3193-
lhs ${op.nonMaskingOperator}= rhs
3194-
return lhs
3195-
}
3196-
3197-
public static func ${op.nonMaskingOperator}=(
3198-
lhs: inout ${Self}, rhs: ${Self}
3199-
) {
3200-
${op.helper}Generic(&lhs, rhs)
3201-
}
3202-
3203-
% end
3204-
3205-
}
3206-
3207-
32083187
//===--- end of FIXME(integers) -------------------------------------------===//
32093188

32103189
% end # end of concrete FixedWidthInteger section
@@ -3572,6 +3551,38 @@ extension UnsignedInteger {
35723551
}
35733552
}
35743553

3554+
// FIXME(integers): These overloads allow expressions like the following in
3555+
// Swift 3 compatibility mode:
3556+
// let x = 1 << i32
3557+
// f(i32: x)
3558+
// At the same time, since they are obsolete in Swift 4, this will not cause
3559+
// `u8 << -1` to fail due to an overflow in an unsigned value.
3560+
extension FixedWidthInteger {
3561+
3562+
% for op in maskingShifts:
3563+
3564+
@available(swift, obsoleted: 4)
3565+
@_transparent
3566+
public static func ${op.nonMaskingOperator}(
3567+
lhs: Self, rhs: Self
3568+
) -> Self {
3569+
var lhs = lhs
3570+
${op.helper}Generic(&lhs, rhs)
3571+
return lhs
3572+
}
3573+
3574+
@available(swift, obsoleted: 4)
3575+
@_transparent
3576+
public static func ${op.nonMaskingOperator}=(
3577+
lhs: inout Self, rhs: Self
3578+
) {
3579+
${op.helper}Generic(&lhs, rhs)
3580+
}
3581+
3582+
% end
3583+
3584+
}
3585+
35753586
// FIXME(integers): Absense of &+ causes ambiguity in the code like the
35763587
// following:
35773588
// func f<T : SignedInteger>(_ x: T, _ y: T) {

test/Interpreter/bitvector.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ struct BitVector64 {
88

99
subscript (bit : Int) -> Bool {
1010
get {
11-
if (bits & (1 << Int64(bit))) != 0 {
11+
if (bits & (1 &<< Int64(bit))) != 0 {
1212
return true
1313
}
1414
return false
1515
}
1616
set {
17-
var mask: Int64 = 1 << bit
17+
var mask: Int64 = 1 &<< bit
1818
if newValue {
1919
bits = bits | mask
2020
} else {

test/Prototypes/BigInt.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
// XFAIL: linux
13-
// RUN: %target-run-simple-swift
13+
// RUN: rm -rf %t ; mkdir -p %t
14+
// RUN: %target-build-swift -swift-version 4 -o %t/a.out %s
15+
// RUN: %target-run %t/a.out
1416
// REQUIRES: executable_test
1517

1618
import StdlibUnittest

0 commit comments

Comments
 (0)