Skip to content

Commit a3ba517

Browse files
Mark legacy overloads of &+ and &- unavailable. (#38723)
These were defined for both FixedWidthInteger and FixedWidthInteger & SignedInteger for source compatibility with Swift 3; the latter set probably should have been removed when we stabilized the ABI, but were not. We can't easily remove them entirely (because we need them for ABI stability now), but we can mark them unavailable so that the typechecker doesn't have to consider them..
1 parent 1509acc commit a3ba517

File tree

3 files changed

+51
-29
lines changed

3 files changed

+51
-29
lines changed

stdlib/public/core/Integers.swift

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3502,8 +3502,19 @@ extension UnsignedInteger where Self: FixedWidthInteger {
35023502

35033503
/// An integer type that can represent both positive and negative values.
35043504
public protocol SignedInteger: BinaryInteger, SignedNumeric {
3505-
// These requirements are for the source code compatibility with Swift 3
3505+
// These requirements are needed for binary compatibility; the following:
3506+
//
3507+
// func foo<T>(_ a: T) -> T
3508+
// where T: SignedInteger & FixedWidthInteger {
3509+
// a &+ 1
3510+
// }
3511+
//
3512+
// generated a call to `static Swift.SignedInteger._maskingAdd(A, A) -> A`
3513+
// when compiled with Swift 5.5 and earlier.
3514+
@available(*, deprecated, message: "Use &+ instead.")
35063515
static func _maskingAdd(_ lhs: Self, _ rhs: Self) -> Self
3516+
3517+
@available(*, deprecated, message: "Use &- instead.")
35073518
static func _maskingSubtract(_ lhs: Self, _ rhs: Self) -> Self
35083519
}
35093520

@@ -3629,49 +3640,48 @@ public func numericCast<T: BinaryInteger, U: BinaryInteger>(_ x: T) -> U {
36293640
return U(x)
36303641
}
36313642

3632-
// FIXME(integers): Absence of &+ causes ambiguity in the code like the
3633-
// following:
3634-
// func f<T: SignedInteger>(_ x: T, _ y: T) {
3635-
// var _ = (x &+ (y - 1)) < x
3636-
// }
3637-
// Compiler output:
3638-
// error: ambiguous reference to member '-'
3639-
// var _ = (x &+ (y - 1)) < x
3640-
// ^
3643+
// Needed to support user-defined types conformance to SignedInteger.
3644+
// We need these defaults to exist, but they are not called.
36413645
extension SignedInteger {
3642-
@_transparent
3646+
@available(*, deprecated, message: "Use &+ instead.")
36433647
public static func _maskingAdd(_ lhs: Self, _ rhs: Self) -> Self {
36443648
fatalError("Should be overridden in a more specific type")
36453649
}
3646-
3647-
@_transparent
3650+
3651+
@available(*, deprecated, message: "Use &- instead.")
36483652
public static func _maskingSubtract(_ lhs: Self, _ rhs: Self) -> Self {
36493653
fatalError("Should be overridden in a more specific type")
36503654
}
36513655
}
36523656

3657+
// These symbols have to exist for ABI compatibility, but should not be used
3658+
// any longer; we want to find the FixedWidthInteger definitions instead.
36533659
extension SignedInteger where Self: FixedWidthInteger {
3654-
// This overload is supposed to break the ambiguity between the
3655-
// implementations on SignedInteger and FixedWidthInteger
3656-
@_transparent
3657-
public static func &+ (lhs: Self, rhs: Self) -> Self {
3658-
return _maskingAdd(lhs, rhs)
3660+
@available(*, unavailable)
3661+
public static func &+(lhs: Self, rhs: Self) -> Self {
3662+
lhs.addingReportingOverflow(rhs).partialValue
36593663
}
3660-
3661-
@_transparent
3664+
3665+
// This may be called in rare situations by binaries compiled with
3666+
// Swift 5.5 and earlier, so we need to keep it around for compatibility.
3667+
// We can't mark it unavailable, because then the concrete signed integer
3668+
// types in the standard library would not satisfy the protocol requirements.
3669+
@available(*, deprecated, message: "Use &+ instead.")
36623670
public static func _maskingAdd(_ lhs: Self, _ rhs: Self) -> Self {
3663-
return lhs.addingReportingOverflow(rhs).partialValue
3671+
lhs.addingReportingOverflow(rhs).partialValue
36643672
}
36653673

3666-
// This overload is supposed to break the ambiguity between the
3667-
// implementations on SignedInteger and FixedWidthInteger
3668-
@_transparent
3669-
public static func &- (lhs: Self, rhs: Self) -> Self {
3670-
return _maskingSubtract(lhs, rhs)
3674+
@available(*, unavailable)
3675+
public static func &-(lhs: Self, rhs: Self) -> Self {
3676+
lhs.subtractingReportingOverflow(rhs).partialValue
36713677
}
3672-
3673-
@_transparent
3678+
3679+
// This may be called in rare situations by binaries compiled with
3680+
// Swift 5.5 and earlier, so we need to keep it around for compatibility.
3681+
// We can't mark it unavailable, because then the concrete signed integer
3682+
// types in the standard library would not satisfy the protocol requirements.
3683+
@available(*, deprecated, message: "Use &- instead.")
36743684
public static func _maskingSubtract(_ lhs: Self, _ rhs: Self) -> Self {
3675-
return lhs.subtractingReportingOverflow(rhs).partialValue
3685+
lhs.subtractingReportingOverflow(rhs).partialValue
36763686
}
36773687
}

test/api-digester/Outputs/stability-stdlib-source-x86_64.swift.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ Protocol CodingKey has generic signature change from <Self : Swift.CustomDebugSt
55
Protocol Error has added inherited protocol Sendable
66
Protocol Error has generic signature change from to <Self : Swift.Sendable>
77
Subscript MutableCollection.subscript(_:) has generic signature change from <Self where Self : Swift.MutableCollection> to <Self where Self : Swift.MutableCollection, Self.SubSequence == Swift.Slice<Self>>
8+
9+
// Not actually a source break; the typechecker will find these operations on
10+
// FixedWidthInteger instead.
11+
Func SignedInteger.&+(_:_:) has been removed
12+
Func SignedInteger.&-(_:_:) has been removed

test/api-digester/stability-stdlib-abi-without-asserts.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ Protocol Error has added inherited protocol Sendable
7272
Protocol Error has generic signature change from to <Self : Swift.Sendable>
7373
Enum Never has added a conformance to an existing protocol Identifiable
7474

75+
// These haven't actually been removed; they are simply marked unavailable.
76+
// This seems to be a false positive in the ABI checker. This is not an ABI
77+
// break because the symbols are still present, and is not a source break
78+
// because FixedWidthInteger still has these operations.
79+
Func SignedInteger.&+(_:_:) has been removed
80+
Func SignedInteger.&-(_:_:) has been removed
81+
7582
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)

0 commit comments

Comments
 (0)