Skip to content

Commit 044c181

Browse files
committed
Add constraints to FixedWidthInteger.Stride and .Magnitude
Add the constraint that these associatedtypes themselves conform to FixedWidthInteger and to SignedInteger and UnsignedInteger, respectively. These are effectively part of the semantic requirement of the protocol already, because the requirements on .min and .max effectively force FixedWidthInteger to be twos-complement, which requires Magnitude not be Self for signed types. Also, in practice it's generally necessary to have these two constraints in order to effectively use the FixedWidthInteger protocol anyway; witness that by adding them to the protocol, we eliminate them as constraints from a number of extensions. This also resolves https://bugs.swift.org/browse/SR-8156
1 parent d8e4053 commit 044c181

File tree

4 files changed

+6
-9
lines changed

4 files changed

+6
-9
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
16651665
}
16661666

16671667
public subscript<R: RangeExpression>(_ rangeExpression: R) -> Data
1668-
where R.Bound: FixedWidthInteger, R.Bound.Stride : SignedInteger {
1668+
where R.Bound: FixedWidthInteger {
16691669
get {
16701670
let lower = R.Bound(_sliceRange.lowerBound)
16711671
let upper = R.Bound(_sliceRange.upperBound)

stdlib/public/SDK/Foundation/NSRange.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ extension NSRange {
140140

141141
extension NSRange {
142142
public init<R: RangeExpression>(_ region: R)
143-
where R.Bound: FixedWidthInteger, R.Bound.Stride : SignedInteger {
143+
where R.Bound: FixedWidthInteger {
144144
let r = region.relative(to: 0..<R.Bound.max)
145145
self.init(location: numericCast(r.lowerBound), length: numericCast(r.count))
146146
}

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,9 +2398,7 @@ extension BinaryFloatingPoint {
23982398
% for Range in ['Range', 'ClosedRange']:
23992399
% exampleRange = '10.0..<20.0' if Range == 'Range' else '10.0...20.0'
24002400
extension BinaryFloatingPoint
2401-
where Self.RawSignificand : FixedWidthInteger,
2402-
Self.RawSignificand.Stride : SignedInteger,
2403-
Self.RawSignificand.Magnitude : UnsignedInteger {
2401+
where Self.RawSignificand : FixedWidthInteger {
24042402

24052403
/// Returns a random value within the specified range, using the given
24062404
/// generator as a source for randomness.

stdlib/public/core/Integers.swift.gyb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,8 @@ extension BinaryInteger {
22712271
/// other arithmetic methods and operators.
22722272
public protocol FixedWidthInteger :
22732273
BinaryInteger, LosslessStringConvertible
2274-
where Magnitude : FixedWidthInteger
2274+
where Magnitude : FixedWidthInteger & UnsignedInteger,
2275+
Stride : FixedWidthInteger & SignedInteger
22752276
{
22762277
/// The number of bits used for the underlying binary representation of
22772278
/// values of this type.
@@ -2531,9 +2532,7 @@ ${assignmentOperatorComment(x.operator, False)}
25312532
% for Range in ['Range', 'ClosedRange']:
25322533
% exampleRange = '1..<100' if Range == 'Range' else '1...100'
25332534
2534-
extension FixedWidthInteger
2535-
where Self.Stride : SignedInteger,
2536-
Self.Magnitude : UnsignedInteger {
2535+
extension FixedWidthInteger {
25372536
25382537
/// Returns a random value within the specified range, using the given
25392538
/// generator as a source for randomness.

0 commit comments

Comments
 (0)