diff --git a/stdlib/private/StdlibUnicodeUnittest/Collation.swift b/stdlib/private/StdlibUnicodeUnittest/Collation.swift index 0080995f60745..d1e04eecd4f6a 100644 --- a/stdlib/private/StdlibUnicodeUnittest/Collation.swift +++ b/stdlib/private/StdlibUnicodeUnittest/Collation.swift @@ -338,13 +338,13 @@ public struct StringComparisonTest { public func sortKey(forCollationElements ces: [UInt64]) -> ([UInt16], [UInt16], [UInt16]) { func L1(_ ce: UInt64) -> UInt16 { - return UInt16(extendingOrTruncating: ce &>> 32) + return UInt16(truncatingIfNeeded: ce &>> 32) } func L2(_ ce: UInt64) -> UInt16 { - return UInt16(extendingOrTruncating: ce &>> 16) + return UInt16(truncatingIfNeeded: ce &>> 16) } func L3(_ ce: UInt64) -> UInt16 { - return UInt16(extendingOrTruncating: ce) + return UInt16(truncatingIfNeeded: ce) } var result1: [UInt16] = [] diff --git a/stdlib/public/core/ASCII.swift b/stdlib/public/core/ASCII.swift index e4cf6179709b8..577cd26138884 100644 --- a/stdlib/public/core/ASCII.swift +++ b/stdlib/public/core/ASCII.swift @@ -41,7 +41,7 @@ extension Unicode.ASCII : Unicode.Encoding { _ source: Unicode.Scalar ) -> EncodedScalar? { guard source.value < (1&<<7) else { return nil } - return EncodedScalar(UInt8(extendingOrTruncating: source.value)) + return EncodedScalar(UInt8(truncatingIfNeeded: source.value)) } @inline(__always) @@ -80,7 +80,7 @@ extension Unicode.ASCII.Parser : Unicode.Parser { where I.Element == Encoding.CodeUnit { let n = input.next() if _fastPath(n != nil), let x = n { - guard _fastPath(Int8(extendingOrTruncating: x) >= 0) + guard _fastPath(Int8(truncatingIfNeeded: x) >= 0) else { return .error(length: 1) } return .valid(Unicode.ASCII.EncodedScalar(x)) } diff --git a/stdlib/public/core/ArrayBody.swift b/stdlib/public/core/ArrayBody.swift index acfe75c933e86..76268adf98a3b 100644 --- a/stdlib/public/core/ArrayBody.swift +++ b/stdlib/public/core/ArrayBody.swift @@ -32,7 +32,7 @@ internal struct _ArrayBody { _storage = _SwiftArrayBodyStorage( count: count, _capacityAndFlags: - (UInt(extendingOrTruncating: capacity) &<< 1) | + (UInt(truncatingIfNeeded: capacity) &<< 1) | (elementTypeIsBridgedVerbatim ? 1 : 0)) } diff --git a/stdlib/public/core/Builtin.swift b/stdlib/public/core/Builtin.swift index e2a987125e7db..022bfe57a0cf7 100644 --- a/stdlib/public/core/Builtin.swift +++ b/stdlib/public/core/Builtin.swift @@ -90,8 +90,7 @@ func _canBeClass(_: T.Type) -> Int8 { /// - Value conversion from one integer type to another. Use the destination /// type's initializer or the `numericCast(_:)` function. /// - Bitwise conversion from one integer type to another. Use the destination -/// type's `init(extendingOrTruncating:)` or `init(bitPattern:)` -/// initializer. +/// type's `init(truncatingIfNeeded:)` or `init(bitPattern:)` initializer. /// - Conversion from a pointer to an integer value with the bit pattern of the /// pointer's address in memory, or vice versa. Use the `init(bitPattern:)` /// initializer for the destination type. diff --git a/stdlib/public/core/Character.swift b/stdlib/public/core/Character.swift index 2d0b783497505..adea11d12bc9c 100644 --- a/stdlib/public/core/Character.swift +++ b/stdlib/public/core/Character.swift @@ -130,7 +130,7 @@ public struct Character : shift += 16 } } - guard _fastPath(Int64(extendingOrTruncating: bits) >= 0) else { + guard _fastPath(Int64(truncatingIfNeeded: bits) >= 0) else { break FastPath } _representation = .smallUTF16(Builtin.trunc_Int64_Int63(bits._value)) @@ -224,7 +224,7 @@ public struct Character : if _fastPath(s._core.count <= 4) { let b = _UIntBuffer(s._core) - if _fastPath(Int64(extendingOrTruncating: b._storage) >= 0) { + if _fastPath(Int64(truncatingIfNeeded: b._storage) >= 0) { _representation = .smallUTF16( Builtin.trunc_Int64_Int63(b._storage._value)) return @@ -300,7 +300,7 @@ extension Character { return _UIntBuffer( _storage: bits, _bitCount: UInt8( - extendingOrTruncating: 16 * Swift.max(1, (minBitWidth + 15) / 16)) + truncatingIfNeeded: 16 * Swift.max(1, (minBitWidth + 15) / 16)) ) } diff --git a/stdlib/public/core/CharacterUnicodeScalars.swift b/stdlib/public/core/CharacterUnicodeScalars.swift index 51b0f79f02164..be6786a569168 100644 --- a/stdlib/public/core/CharacterUnicodeScalars.swift +++ b/stdlib/public/core/CharacterUnicodeScalars.swift @@ -101,7 +101,7 @@ extension Character.UnicodeScalarView : Collection { case .valid(let s): return Index( _encodedOffset: startOfNextScalar, _scalar: s, - _stride: UInt8(extendingOrTruncating: s.count)) + _stride: UInt8(truncatingIfNeeded: s.count)) case .error: return Index( _encodedOffset: startOfNextScalar, @@ -138,7 +138,7 @@ extension Character.UnicodeScalarView : BidirectionalCollection { case .valid(let s): return Index( _encodedOffset: i._encodedOffset - s.count, _scalar: s, - _stride: UInt8(extendingOrTruncating: s.count)) + _stride: UInt8(truncatingIfNeeded: s.count)) case .error: return Index( _encodedOffset: i._encodedOffset - 1, diff --git a/stdlib/public/core/DoubleWidth.swift.gyb b/stdlib/public/core/DoubleWidth.swift.gyb index f7a61093bb271..0e95abb490895 100644 --- a/stdlib/public/core/DoubleWidth.swift.gyb +++ b/stdlib/public/core/DoubleWidth.swift.gyb @@ -97,14 +97,14 @@ public struct DoubleWidth : } } - public init(_ source: T) { + public init(_ source: T) { fatalError() } - public init?(exactly source: T) { + public init?(exactly source: T) { fatalError() } - + public init(_ source: T) where T.RawSignificand : FixedWidthInteger { @@ -269,27 +269,23 @@ public struct DoubleWidth : % for (operator, name) in [('+', 'adding'), ('-', 'subtracting')]: % highAffectedByLowOverflow = 'Base.max' if operator == '+' else 'Base.min' public func ${name}ReportingOverflow(_ rhs: DoubleWidth) - -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) { + -> (partialValue: DoubleWidth, overflow: Bool) { let (low, lowOverflow) = _storage.low.${name}ReportingOverflow(rhs._storage.low) let (high, highOverflow) = _storage.high.${name}ReportingOverflow(rhs._storage.high) - let isLowOverflow = lowOverflow == .overflow - let result = (high &${operator} (isLowOverflow ? 1 : 0), low) - let overflow = ArithmeticOverflow( - highOverflow == .overflow || - high == ${highAffectedByLowOverflow} && isLowOverflow - ) - return (partialValue: DoubleWidth(result), - overflow: overflow) + let result = (high &${operator} (lowOverflow ? 1 : 0), low) + let overflow = highOverflow || + high == ${highAffectedByLowOverflow} && lowOverflow + return (partialValue: DoubleWidth(result), overflow: overflow) } % end public func multipliedReportingOverflow( by rhs: DoubleWidth - ) -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) { + ) -> (partialValue: DoubleWidth, overflow: Bool) { let (carry, product) = multipliedFullWidth(by: rhs) - let result = DoubleWidth(extendingOrTruncating: product) + let result = DoubleWidth(truncatingIfNeeded: product) let isNegative = (self < (0 as DoubleWidth)) != (rhs < (0 as DoubleWidth)) let didCarry = isNegative @@ -298,7 +294,7 @@ public struct DoubleWidth : let hadPositiveOverflow = !isNegative && DoubleWidth.isSigned && product.leadingZeroBitCount == 0 - return (result, ArithmeticOverflow(didCarry || hadPositiveOverflow)) + return (result, didCarry || hadPositiveOverflow) } public func quotientAndRemainder(dividingBy other: DoubleWidth) @@ -350,25 +346,25 @@ public struct DoubleWidth : } public func dividedReportingOverflow(by other: DoubleWidth) - -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) { + -> (partialValue: DoubleWidth, overflow: Bool) { if other == (0 as DoubleWidth) || (DoubleWidth.isSigned && other == (-1 as Int) && self == .min) { - return (self, .overflow) + return (self, true) } - return (quotientAndRemainder(dividingBy: other).quotient, .none) + return (quotientAndRemainder(dividingBy: other).quotient, false) } public func remainderReportingOverflow(dividingBy other: DoubleWidth) - -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) { + -> (partialValue: DoubleWidth, overflow: Bool) { if other == 0 || (DoubleWidth.isSigned && other == -1 && self == .min) { - return (self, .overflow) + return (self, true) } - return (quotientAndRemainder(dividingBy: other).remainder, .none) + return (quotientAndRemainder(dividingBy: other).remainder, false) } public func multipliedFullWidth(by other: DoubleWidth) @@ -384,8 +380,7 @@ public struct DoubleWidth : func sum(_ x: Low, _ y: Low, _ z: Low) -> (partial: Low, carry: Low) { let (sum1, overflow1) = x.addingReportingOverflow(y) let (sum2, overflow2) = sum1.addingReportingOverflow(z) - let carry: Low = (overflow1 == .overflow ? 1 : 0) + - (overflow2 == .overflow ? 1 : 0) + let carry: Low = (overflow1 ? 1 : 0) + (overflow2 ? 1 : 0) return (sum2, carry) } @@ -406,7 +401,7 @@ public struct DoubleWidth : if isNegative { let (lowComplement, overflow) = (~low).addingReportingOverflow(1) - return (~high + (overflow == .overflow ? 1 : 0), lowComplement) + return (~high + (overflow ? 1 : 0), lowComplement) } else { return (high, low) } @@ -447,7 +442,7 @@ public struct DoubleWidth : // Shift is exactly the width of `Base`, so low -> high. if rhs._storage.low == Base.bitWidth { - lhs = DoubleWidth((High(extendingOrTruncating: lhs._storage.low), 0)) + lhs = DoubleWidth((High(truncatingIfNeeded: lhs._storage.low), 0)) return } @@ -472,7 +467,7 @@ public struct DoubleWidth : if rhs._storage.low == Base.bitWidth { lhs = DoubleWidth(( lhs < (0 as DoubleWidth) ? ~0 : 0, - Low(extendingOrTruncating: lhs._storage.high) + Low(truncatingIfNeeded: lhs._storage.high) )) return } @@ -485,10 +480,10 @@ public struct DoubleWidth : lhs._storage.high <<= High(rhs._storage.low) if Base.bitWidth > rhs._storage.low { - lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low >> + lhs._storage.high |= High(truncatingIfNeeded: lhs._storage.low >> (numericCast(Base.bitWidth) - rhs._storage.low)) } else { - lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low << + lhs._storage.high |= High(truncatingIfNeeded: lhs._storage.low << (rhs._storage.low - numericCast(Base.bitWidth))) } lhs._storage.low <<= rhs._storage.low @@ -500,14 +495,14 @@ public struct DoubleWidth : lhs._storage.low >>= rhs._storage.low if Base.bitWidth > rhs._storage.low { lhs._storage.low |= Low( - extendingOrTruncating: + truncatingIfNeeded: lhs._storage.high << (numericCast(Base.bitWidth) - rhs._storage.low)) } else { lhs._storage.low |= Low( - extendingOrTruncating: lhs._storage.high >> + truncatingIfNeeded: lhs._storage.high >> (rhs._storage.low - numericCast(Base.bitWidth))) } - lhs._storage.high >>= High(extendingOrTruncating: rhs._storage.low) + lhs._storage.high >>= High(truncatingIfNeeded: rhs._storage.low) } %{ @@ -535,7 +530,7 @@ binaryOperators = [ lhs: inout DoubleWidth, rhs: DoubleWidth ) { let (result, overflow) = lhs.${name}ReportingOverflow(${argumentLabel}rhs) - _precondition(overflow == .none, "Overflow in ${operator}=") + _precondition(!overflow, "Overflow in ${operator}=") lhs = result } % end @@ -578,8 +573,8 @@ binaryOperators = [ @_transparent public var byteSwapped: DoubleWidth { - return DoubleWidth((High(extendingOrTruncating: low.byteSwapped), - Low(extendingOrTruncating: high.byteSwapped))) + return DoubleWidth((High(truncatingIfNeeded: low.byteSwapped), + Low(truncatingIfNeeded: high.byteSwapped))) } } diff --git a/stdlib/public/core/FloatingPointTypes.swift.gyb b/stdlib/public/core/FloatingPointTypes.swift.gyb index 5b4548b5505b2..3a17f479d2987 100644 --- a/stdlib/public/core/FloatingPointTypes.swift.gyb +++ b/stdlib/public/core/FloatingPointTypes.swift.gyb @@ -754,11 +754,11 @@ extension ${Self} : Hashable { %if bits <= word_bits: return Int(bitPattern: UInt(bitPattern)) %elif bits == 64: # Double -> 32-bit Int - return Int(extendingOrTruncating: bitPattern &>> 32) ^ - Int(extendingOrTruncating: bitPattern) + return Int(truncatingIfNeeded: bitPattern &>> 32) ^ + Int(truncatingIfNeeded: bitPattern) %elif word_bits == 32: # Float80 -> 32-bit Int - return Int(extendingOrTruncating: significandBitPattern &>> 32) ^ - Int(extendingOrTruncating: significandBitPattern) ^ + return Int(truncatingIfNeeded: significandBitPattern &>> 32) ^ + Int(truncatingIfNeeded: significandBitPattern) ^ Int(_representation.signAndExponent) %else: # Float80 -> 64-bit Int return Int(bitPattern: UInt(significandBitPattern)) ^ diff --git a/stdlib/public/core/IntegerParsing.swift b/stdlib/public/core/IntegerParsing.swift index d41926ed90c2f..0032892abbd42 100644 --- a/stdlib/public/core/IntegerParsing.swift +++ b/stdlib/public/core/IntegerParsing.swift @@ -18,14 +18,14 @@ internal func _asciiDigit( let lower = _ascii16("a")..._ascii16("z") let upper = _ascii16("A")..._ascii16("Z") - let u = UInt16(extendingOrTruncating: u_) + let u = UInt16(truncatingIfNeeded: u_) let d: UInt16 if _fastPath(digit ~= u) { d = u &- digit.lowerBound } else if _fastPath(upper ~= u) { d = u &- upper.lowerBound &+ 10 } else if _fastPath(lower ~= u) { d = u &- lower.lowerBound &+ 10 } else { return nil } guard _fastPath(d < radix) else { return nil } - return Result(extendingOrTruncating: d) + return Result(truncatingIfNeeded: d) } @inline(__always) @@ -40,7 +40,7 @@ where Rest.Element : UnsignedInteger { if !positive { let (result0, overflow0) = (0 as Result).subtractingReportingOverflow(result) - guard _fastPath(overflow0 == .none) else { return nil } + guard _fastPath(!overflow0) else { return nil } result = result0 } @@ -51,7 +51,7 @@ where Rest.Element : UnsignedInteger { let (result2, overflow2) = positive ? result1.addingReportingOverflow(d) : result1.subtractingReportingOverflow(d) - guard _fastPath(overflow1 == .none && overflow2 == .none) + guard _fastPath(!overflow1 && !overflow2) else { return nil } result = result2 } diff --git a/stdlib/public/core/Integers.swift.gyb b/stdlib/public/core/Integers.swift.gyb index 5adb52aa622d0..a6096181b8a91 100644 --- a/stdlib/public/core/Integers.swift.gyb +++ b/stdlib/public/core/Integers.swift.gyb @@ -819,8 +819,8 @@ def overflowOperationComment(operator): /// - Parameter rhs: The value to add to this value. /// - Returns: A tuple containing the result of the addition along with a /// flag indicating whether overflow occurred. If the `overflow` component - /// is `.none`, the `partialValue` component contains the entire sum. If - /// the `overflow` component is `.overflow`, an overflow occurred and the + /// is `false`, the `partialValue` component contains the entire sum. If + /// the `overflow` component is `true`, an overflow occurred and the /// `partialValue` component contains the truncated sum of this value and /// `rhs`. """, @@ -831,8 +831,8 @@ def overflowOperationComment(operator): /// - Parameter rhs: The value to subtract from this value. /// - Returns: A tuple containing the result of the subtraction along with a /// flag indicating whether overflow occurred. If the `overflow` component - /// is `.none`, the `partialValue` component contains the entire - /// difference. If the `overflow` component is `.overflow`, an overflow + /// is `false`, the `partialValue` component contains the entire + /// difference. If the `overflow` component is `true`, an overflow /// occurred and the `partialValue` component contains the truncated /// result of `rhs` subtracted from this value. """, @@ -843,8 +843,8 @@ def overflowOperationComment(operator): /// - Parameter rhs: The value to multiply by this value. /// - Returns: A tuple containing the result of the multiplication along with /// a flag indicating whether overflow occurred. If the `overflow` - /// component is `.none`, the `partialValue` component contains the entire - /// product. If the `overflow` component is `.overflow`, an overflow + /// component is `false`, the `partialValue` component contains the entire + /// product. If the `overflow` component is `true`, an overflow /// occurred and the `partialValue` component contains the truncated /// product of this value and `rhs`. """, @@ -853,13 +853,13 @@ def overflowOperationComment(operator): /// a flag indicating whether overflow occurred in the operation. /// /// Dividing by zero is not an error when using this method. For a value `x`, - /// the result of `x.dividedReportingOverflow(by: 0)` is `(x, .overflow)`. + /// the result of `x.dividedReportingOverflow(by: 0)` is `(x, true)`. /// /// - Parameter rhs: The value to divide this value by. /// - Returns: A tuple containing the result of the division along with a /// flag indicating whether overflow occurred. If the `overflow` component - /// is `.none`, the `partialValue` component contains the entire quotient. - /// If the `overflow` component is `.overflow`, an overflow occurred and + /// is `false`, the `partialValue` component contains the entire quotient. + /// If the `overflow` component is `true`, an overflow occurred and /// the `partialValue` component contains the truncated quotient. """, '%': """\ @@ -868,13 +868,13 @@ def overflowOperationComment(operator): /// a flag indicating whether overflow occurred in the operation. /// /// Dividing by zero is not an error when using this method. For a value `x`, - /// the result of `x.dividedReportingOverflow(by: 0)` is `(x, .overflow)`. + /// the result of `x.dividedReportingOverflow(by: 0)` is `(x, true)`. /// /// - Parameter rhs: The value to divide this value by. /// - Returns: A tuple containing the result of the division along with a /// flag indicating whether overflow occurred. If the `overflow` component - /// is `.none`, the `partialValue` component contains the entire quotient. - /// If the `overflow` component is `.overflow`, an overflow occurred and + /// is `false`, the `partialValue` component contains the entire quotient. + /// If the `overflow` component is `true`, an overflow occurred and /// the `partialValue` component contains the truncated quotient. """, } @@ -1242,7 +1242,7 @@ extension Numeric { /// Bit Pattern Conversion /// ---------------------- /// -/// Use the `init(extendingOrTruncating:)` initializer to create a new instance +/// Use the `init(truncatingIfNeeded:)` initializer to create a new instance /// with the same bit pattern as the passed value, extending or truncating the /// value's representation as necessary. Note that the value may not be /// preserved, particularly when converting between signed to unsigned integer @@ -1253,11 +1253,11 @@ extension Numeric { /// let q: Int16 = 850 /// // q == 0b00000011_01010010 /// -/// let r = Int8(extendingOrTruncating: q) // truncate 'q' to fit in 8 bits +/// let r = Int8(truncatingIfNeeded: q) // truncate 'q' to fit in 8 bits /// // r == 82 /// // == 0b01010010 /// -/// let s = Int16(extendingOrTruncating: s) // extend 'r' to fill 16 bits +/// let s = Int16(truncatingIfNeeded: s) // extend 'r' to fill 16 bits /// // s == 82 /// // == 0b00000000_01010010 /// @@ -1272,15 +1272,15 @@ extension Numeric { /// // t == -100 /// // t's binary representation == 0b10011100 /// -/// let u = UInt8(extendingOrTruncating: t) +/// let u = UInt8(truncatingIfNeeded: t) /// // u == 156 /// // u's binary representation == 0b10011100 /// -/// let v = Int16(extendingOrTruncating: t) +/// let v = Int16(truncatingIfNeeded: t) /// // v == -100 /// // v's binary representation == 0b11111111_10011100 /// -/// let w = UInt16(extendingOrTruncating: t) +/// let w = UInt16(truncatingIfNeeded: t) /// // w == 65436 /// // w's binary representation == 0b11111111_10011100 /// @@ -1330,7 +1330,7 @@ public protocol BinaryInteger : /// // y == nil /// /// - Parameter source: A floating-point value to convert to an integer. - init?(exactly source: T) + init?(exactly source: T) /// Creates an integer from the given floating-point value, rounding toward /// zero. @@ -1348,7 +1348,7 @@ public protocol BinaryInteger : /// /// - Parameter source: A floating-point value to convert to an integer. /// `source` must be representable in this type after rounding toward zero. - init(_ source: T) + init(_ source: T) /// Creates a new instance from the given integer. /// @@ -1378,7 +1378,7 @@ public protocol BinaryInteger : /// /// let p: Int16 = -500 /// // 'p' has a binary representation of 11111110_00001100 - /// let q = Int8(extendingOrTruncating: p) + /// let q = Int8(truncatingIfNeeded: p) /// // q == 12 /// // 'q' has a binary representation of 00001100 /// @@ -1389,21 +1389,21 @@ public protocol BinaryInteger : /// /// let u: Int8 = 21 /// // 'u' has a binary representation of 00010101 - /// let v = Int16(extendingOrTruncating: u) + /// let v = Int16(truncatingIfNeeded: u) /// // v == 21 /// // 'v' has a binary representation of 00000000_00010101 /// /// let w: Int8 = -21 /// // 'w' has a binary representation of 11101011 - /// let x = Int16(extendingOrTruncating: w) + /// let x = Int16(truncatingIfNeeded: w) /// // x == -21 /// // 'x' has a binary representation of 11111111_11101011 - /// let y = UInt16(extendingOrTruncating: w) + /// let y = UInt16(truncatingIfNeeded: w) /// // y == 65515 /// // 'y' has a binary representation of 11111111_11101011 /// /// - Parameter source: An integer to convert to this type. - init(extendingOrTruncating source: T) + init(truncatingIfNeeded source: T) /// Creates a new instance with the representable value that's closest to the /// given integer. @@ -1539,7 +1539,7 @@ extension BinaryInteger { self = 0 } - public init?(exactly source: T) { + public init?(exactly source: T) { // FIXME(integers): implement fatalError() } @@ -1676,16 +1676,16 @@ extension BinaryInteger { // sign bit. Therefore it is safe to convert to the unsigned type. if lhs.bitWidth < rhs.bitWidth { - return Other(extendingOrTruncating: lhs) == rhs + return Other(truncatingIfNeeded: lhs) == rhs } if lhs.bitWidth > rhs.bitWidth { - return lhs == Self(extendingOrTruncating: rhs) + return lhs == Self(truncatingIfNeeded: rhs) } if Self.isSigned { - return Other(extendingOrTruncating: lhs) == rhs + return Other(truncatingIfNeeded: lhs) == rhs } - return lhs == Self(extendingOrTruncating: rhs) + return lhs == Self(truncatingIfNeeded: rhs) } /// Returns a Boolean value indicating whether the two given values are not @@ -1736,14 +1736,14 @@ extension BinaryInteger { // values of the other type. Otherwise, lhs and rhs are positive, and one // of Self, Other may be signed and the other unsigned. - let rhsAsSelf = Self(extendingOrTruncating: rhs) + let rhsAsSelf = Self(truncatingIfNeeded: rhs) let rhsAsSelfNegative = rhsAsSelf < (0 as Self) // Can we round-trip rhs through Other? - if Other(extendingOrTruncating: rhsAsSelf) == rhs && + if Other(truncatingIfNeeded: rhsAsSelf) == rhs && // This additional check covers the `Int8.max < (128 as UInt8)` case. - // Since the types are of the same width, init(extendingOrTruncating:) + // Since the types are of the same width, init(truncatingIfNeeded:) // will result in a simple bitcast, so that rhsAsSelf would be -128, and // `lhs < rhsAsSelf` will return false. // We basically guard against that bitcast by requiring rhs and rhsAsSelf @@ -1752,7 +1752,7 @@ extension BinaryInteger { return lhs < rhsAsSelf } - return Other(extendingOrTruncating: lhs) < rhs + return Other(truncatingIfNeeded: lhs) < rhs } /// Returns a Boolean value indicating whether the value of the first @@ -1845,28 +1845,6 @@ extension BinaryInteger { //===--- FixedWidthInteger ------------------------------------------------===// //===----------------------------------------------------------------------===// -/// An indicator of whether an arithmetic operation overflowed. -/// -/// Some arithmetic operations on fixed-width integers return an -/// `ArithmeticOverflow` instance to indicate whether an overflow has -/// occurred. For example, adding `UInt8.max` to itself results in a value that -/// can't be represented by an `UInt8` instance without overflowing. -/// -/// let x = UInt8.max -/// // x == 255 -/// let (y, overflow) = x.addingReportingOverflow(x) -/// // y == 254 -/// // overflow == ArithmeticOverflow.overflow -@_fixed_layout -public enum ArithmeticOverflow { - @_transparent - public init(_ overflow: Bool) { self = overflow ? .overflow : .none } - /// An indication that no overflow occurred in the operation. - case none - /// An indication that an overflow did occur in the operation. - case overflow -} - /// An integer type that uses a fixed size for every instance. /// /// The `FixedWidthInteger` protocol adds binary bitwise operations, bit @@ -1884,7 +1862,7 @@ public enum ArithmeticOverflow { /// var binaryString: String { /// var result: [String] = [] /// for i in 0..<(Self.bitWidth / 8) { -/// let byte = UInt8(extendingOrTruncating: self >> (i * 8)) +/// let byte = UInt8(truncatingIfNeeded: self >> (i * 8)) /// let byteString = String(byte, radix: 2) /// let padding = String(repeating: "0", /// count: 8 - byteString.count) @@ -1910,7 +1888,7 @@ public enum ArithmeticOverflow { /// /// func squared(_ x: T) -> T? { /// let (result, overflow) = x.multipliedReportingOverflow(by: x) -/// guard overflow == .none else { +/// if overflow { /// return nil /// } /// return result @@ -1963,7 +1941,7 @@ public protocol FixedWidthInteger : BinaryInteger, _BitwiseOperations ${overflowOperationComment(x.operator)} func ${x.name}ReportingOverflow( ${x.firstArg} rhs: Self - ) -> (partialValue: Self, overflow: ArithmeticOverflow) + ) -> (partialValue: Self, overflow: Bool) % end /// Returns a tuple containing the high and low parts of the result of @@ -2151,7 +2129,7 @@ ${operatorComment(x.operator, False)} public static func ${x.operator} < Other : BinaryInteger >(lhs: Self, rhs: Other) -> Self { - return lhs ${x.operator} Self(extendingOrTruncating: rhs) + return lhs ${x.operator} Self(truncatingIfNeeded: rhs) } // Heterogeneous masking shift assignment @@ -2217,7 +2195,7 @@ ${operatorComment(x.nonMaskingOperator, True)} let overshiftL: Self = 0 if _fastPath(rhs >= 0) { if _fastPath(rhs < Self.bitWidth) { - return lhs ${x.operator} Self(extendingOrTruncating: rhs) + return lhs ${x.operator} Self(truncatingIfNeeded: rhs) } return overshift${'LR'[isRightShift]} } @@ -2240,7 +2218,7 @@ extension FixedWidthInteger { else if _slowPath(source > Self.max) { self = Self.max } - else { self = Self(extendingOrTruncating: source) } + else { self = Self(truncatingIfNeeded: source) } } % for x in binaryArithmetic['Numeric'] + binaryArithmetic["BinaryInteger"][:1]: @@ -2255,7 +2233,7 @@ ${assignmentOperatorComment(x.operator, True)} @_transparent public static func ${x.operator}=(_ lhs: inout Self, _ rhs: Self) { let (result, overflow) = lhs.${x.name}ReportingOverflow(${callLabel}rhs) - _precondition(overflow == .none, "Overflow in ${x.operator}=") + _precondition(!overflow, "Overflow in ${x.operator}=") lhs = result } #endif @@ -2266,7 +2244,7 @@ ${unsafeOperationComment(x.operator)} public func unsafe${capitalize(x.name)}(${x.firstArg} other: Self) -> Self { let (result, overflow) = self.${x.name}ReportingOverflow(${callLabel}other) - if (overflow != .none) { + if overflow { if (_isDebugAssertConfiguration()) { _preconditionFailure("overflow in unsafe${capitalize(x.name)}") } @@ -2279,7 +2257,7 @@ ${unsafeOperationComment(x.operator)} % end @inline(__always) - public init(extendingOrTruncating source: T) { + public init(truncatingIfNeeded source: T) { if Self.bitWidth <= ${word_bits} { self = Self.init(_truncatingBits: source._lowWord) } @@ -2347,7 +2325,7 @@ extension UnsignedInteger { /// A textual representation of this value. public var description: String { if self.bitWidth <= ${word_bits} { - return _uint64ToString(UInt64(extendingOrTruncating: self)) + return _uint64ToString(UInt64(truncatingIfNeeded: self)) } if self == (0 as Self) { return "0" @@ -2367,7 +2345,7 @@ extension UnsignedInteger { x /= 10 buf.append( Unicode.Scalar( - ascii0 + Int(UInt(extendingOrTruncating: r)._value))!) + ascii0 + Int(UInt(truncatingIfNeeded: r)._value))!) } while x != (0 as Self) return String(buf.reversed().lazy.map { Character($0) }) @@ -2387,7 +2365,7 @@ extension UnsignedInteger where Self : FixedWidthInteger { _precondition(source <= Self.max, "Not enough bits to represent a signed value") } - self.init(extendingOrTruncating: source) + self.init(truncatingIfNeeded: source) } @_semantics("optimize.sil.specialize.generic.partial.never") @@ -2402,7 +2380,7 @@ extension UnsignedInteger where Self : FixedWidthInteger { source > Self.max { return nil } - self.init(extendingOrTruncating: source) + self.init(truncatingIfNeeded: source) } /// The maximum representable integer in this type. @@ -2439,7 +2417,7 @@ extension SignedInteger { /// A textual representation of this value. public var description: String { if self.bitWidth <= ${word_bits} { - return _int64ToString(Int64(extendingOrTruncating: self)) + return _int64ToString(Int64(truncatingIfNeeded: self)) } let base = magnitude.description @@ -2468,7 +2446,7 @@ extension SignedInteger where Self : FixedWidthInteger { _precondition(source <= Self.max, "Not enough bits to represent a signed value") } - self.init(extendingOrTruncating: source) + self.init(truncatingIfNeeded: source) } @_semantics("optimize.sil.specialize.generic.partial.never") @@ -2484,7 +2462,7 @@ extension SignedInteger where Self : FixedWidthInteger { source > Self.max { return nil } - self.init(extendingOrTruncating: source) + self.init(truncatingIfNeeded: source) } /// The maximum representable integer in this type. @@ -2648,7 +2626,7 @@ public struct ${Self} @_transparent public func ${x.name}ReportingOverflow( ${x.firstArg} other: ${Self} - ) -> (partialValue: ${Self}, overflow: ArithmeticOverflow) { + ) -> (partialValue: ${Self}, overflow: Bool) { % if x.kind == '/': // No LLVM primitives for checking overflow of division @@ -2657,7 +2635,7 @@ public struct ${Self} other == (0 as ${Self}) ${'|| self == %s.min && other == (-1 as %s)' % (Self, Self) if signed else ''} ) { - return (partialValue: self, overflow: .overflow) + return (partialValue: self, overflow: true) } let (newStorage, overflow) = ( @@ -2673,7 +2651,7 @@ public struct ${Self} return ( partialValue: ${Self}(newStorage), - overflow: ArithmeticOverflow(Bool(overflow))) + overflow: Bool(overflow)) } % end @@ -2940,7 +2918,7 @@ ${assignmentOperatorComment(x.operator, True)} extension ${Self} { // FIXME(integers): implement me in a less terrible way - public init(_ source: T) { + public init(_ source: T) { % for (FloatType, FloatBits) in [ % ('Float', 32), ('Double', 64), ('Float80', 80)]: % if FloatType == 'Float80': @@ -2977,8 +2955,8 @@ extension ${Self} : Hashable { % elif bits == word_bits * 2: // We have twice as many bits as we need to return. return - Int(extendingOrTruncating: self) ^ - Int(extendingOrTruncating: self &>> 32) + Int(truncatingIfNeeded: self) ^ + Int(truncatingIfNeeded: self &>> 32) % else: _Unimplemented() % end @@ -3009,7 +2987,7 @@ extension ${Self} { /// /// - Parameter source: An integer to use as the source of the new value's /// bit pattern. - @available(swift, obsoleted: 4.0, renamed: "init(extendingOrTruncating:)") + @available(swift, obsoleted: 4.0, renamed: "init(truncatingIfNeeded:)") @_transparent public init(truncatingBitPattern source: ${Src}) { let src = source._value @@ -3218,7 +3196,7 @@ extension FixedWidthInteger { ) -> (Self, overflow: Bool) { let (partialValue, overflow) = lhs.${newPrefix}ReportingOverflow(${argLabel} rhs) - return (partialValue, overflow == .overflow) + return (partialValue, overflow: overflow) } % end diff --git a/stdlib/public/core/SipHash.swift.gyb b/stdlib/public/core/SipHash.swift.gyb index 2670dd17de856..60913d0490e8a 100644 --- a/stdlib/public/core/SipHash.swift.gyb +++ b/stdlib/public/core/SipHash.swift.gyb @@ -248,7 +248,7 @@ struct ${Self} { internal mutating func _finalizeAndReturnIntHash() -> Int { let hash: UInt64 = finalizeAndReturnHash() #if arch(i386) || arch(arm) - return Int(extendingOrTruncating: hash) + return Int(truncatingIfNeeded: hash) #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) return Int(Int64(bitPattern: hash)) #endif diff --git a/stdlib/public/core/String.swift b/stdlib/public/core/String.swift index c2b5a876a6751..ef5c27b768a01 100644 --- a/stdlib/public/core/String.swift +++ b/stdlib/public/core/String.swift @@ -1084,7 +1084,7 @@ extension String { // Since we are left with either 0x0 or 0x20, we can safely truncate to // a UInt8 and add to our ASCII value (this will not overflow numbers in // the ASCII range). - dest.storeBytes(of: value &+ UInt8(extendingOrTruncating: add), + dest.storeBytes(of: value &+ UInt8(truncatingIfNeeded: add), toByteOffset: i, as: UInt8.self) } return String(_storage: buffer) @@ -1123,7 +1123,7 @@ extension String { _asciiLowerCaseTable &>> UInt64(((value &- 1) & 0b0111_1111) &>> 1) let add = (isLower & 0x1) &<< 5 - dest.storeBytes(of: value &- UInt8(extendingOrTruncating: add), + dest.storeBytes(of: value &- UInt8(truncatingIfNeeded: add), toByteOffset: i, as: UInt8.self) } return String(_storage: buffer) diff --git a/stdlib/public/core/StringComparable.swift b/stdlib/public/core/StringComparable.swift index afac15e2bb97c..1aceeec0a4c4d 100644 --- a/stdlib/public/core/StringComparable.swift +++ b/stdlib/public/core/StringComparable.swift @@ -59,7 +59,7 @@ extension String { compare = 0 } else { - compare = Int(extendingOrTruncating: _swift_stdlib_memcmp( + compare = Int(truncatingIfNeeded: _swift_stdlib_memcmp( self._core.startASCII, rhs._core.startASCII, Swift.min(self._core.count, rhs._core.count))) } diff --git a/stdlib/public/core/StringCore.swift b/stdlib/public/core/StringCore.swift index d2cd6b43f0932..49485f84330be 100644 --- a/stdlib/public/core/StringCore.swift +++ b/stdlib/public/core/StringCore.swift @@ -151,9 +151,9 @@ public struct _StringCore { self._baseAddress = baseAddress self._countAndFlags - = (UInt(extendingOrTruncating: elementShift) &<< (UInt.bitWidth - 1)) + = (UInt(truncatingIfNeeded: elementShift) &<< (UInt.bitWidth - 1)) | ((hasCocoaBuffer ? 1 : 0) &<< (UInt.bitWidth - 2)) - | UInt(extendingOrTruncating: count) + | UInt(truncatingIfNeeded: count) self._owner = owner _sanityCheck(UInt(count) & _flagMask == (0 as UInt), @@ -375,7 +375,7 @@ public struct _StringCore { || encoding == Unicode.UTF16.self || encoding == Unicode.UTF32.self { bytes.forEach { - processCodeUnit(Encoding.CodeUnit(extendingOrTruncating: $0)) + processCodeUnit(Encoding.CodeUnit(truncatingIfNeeded: $0)) } } else { @@ -667,7 +667,7 @@ extension _StringCore : RangeReplaceableCollection { if _fastPath(elementWidth == 1) { var dst = rangeStart.assumingMemoryBound(to: UTF8.CodeUnit.self) for u in newElements { - dst.pointee = UInt8(extendingOrTruncating: u) + dst.pointee = UInt8(truncatingIfNeeded: u) dst += 1 } } diff --git a/stdlib/public/core/StringUTF8.swift b/stdlib/public/core/StringUTF8.swift index a02c3191f1b4d..15d00b916658e 100644 --- a/stdlib/public/core/StringUTF8.swift +++ b/stdlib/public/core/StringUTF8.swift @@ -454,7 +454,7 @@ extension String.UTF8View.Iterator : IteratorProtocol { public mutating func next() -> Unicode.UTF8.CodeUnit? { if _fastPath(_buffer != 0) { - let r = UInt8(extendingOrTruncating: _buffer) &- 1 + let r = UInt8(truncatingIfNeeded: _buffer) &- 1 _buffer >>= 8 return r } @@ -494,7 +494,7 @@ extension String.UTF8View.Iterator : IteratorProtocol { while _sourceIndex != _source.endIndex && shift < _OutputBuffer.bitWidth { let u = _source[_sourceIndex] if u >= 0x80 { break } - _buffer |= _OutputBuffer(UInt8(extendingOrTruncating: u &+ 1)) &<< shift + _buffer |= _OutputBuffer(UInt8(truncatingIfNeeded: u &+ 1)) &<< shift _sourceIndex += 1 shift = shift &+ 8 } @@ -522,7 +522,7 @@ extension String.UTF8View.Iterator : IteratorProtocol { _sourceIndex = i._position &- parser._buffer.count } guard _fastPath(_buffer != 0) else { return nil } - let result = UInt8(extendingOrTruncating: _buffer) &- 1 + let result = UInt8(truncatingIfNeeded: _buffer) &- 1 _buffer >>= 8 return result } diff --git a/stdlib/public/core/UIntBuffer.swift b/stdlib/public/core/UIntBuffer.swift index f04677d48d02b..5f7c368929db6 100644 --- a/stdlib/public/core/UIntBuffer.swift +++ b/stdlib/public/core/UIntBuffer.swift @@ -30,8 +30,8 @@ public struct _UIntBuffer< @inline(__always) public init(containing e: Element) { - _storage = Storage(extendingOrTruncating: e) - _bitCount = UInt8(extendingOrTruncating: Element.bitWidth) + _storage = Storage(truncatingIfNeeded: e) + _bitCount = UInt8(truncatingIfNeeded: Element.bitWidth) } } @@ -50,7 +50,7 @@ extension _UIntBuffer : Sequence { _impl._storage = _impl._storage &>> Element.bitWidth _impl._bitCount = _impl._bitCount &- _impl._elementWidth } - return Element(extendingOrTruncating: _impl._storage) + return Element(truncatingIfNeeded: _impl._storage) } public var _impl: _UIntBuffer @@ -95,13 +95,13 @@ extension _UIntBuffer : Collection { @_versioned internal var _elementWidth : UInt8 { - return UInt8(extendingOrTruncating: Element.bitWidth) + return UInt8(truncatingIfNeeded: Element.bitWidth) } public subscript(i: Index) -> Element { @inline(__always) get { - return Element(extendingOrTruncating: _storage &>> i.bitOffset) + return Element(truncatingIfNeeded: _storage &>> i.bitOffset) } } } @@ -120,7 +120,7 @@ extension _UIntBuffer : RandomAccessCollection { @inline(__always) public func index(_ i: Index, offsetBy n: IndexDistance) -> Index { let x = IndexDistance(i.bitOffset) &+ n &* Element.bitWidth - return Index(bitOffset: UInt8(extendingOrTruncating: x)) + return Index(bitOffset: UInt8(truncatingIfNeeded: x)) } @inline(__always) @@ -206,6 +206,6 @@ extension _UIntBuffer : RangeReplaceableCollection { _storage |= replacement1._storage &<< (headCount &* w) _storage |= tailBits &<< ((tailOffset &+ growth) &* w) _bitCount = UInt8( - extendingOrTruncating: IndexDistance(_bitCount) &+ growth &* w) + truncatingIfNeeded: IndexDistance(_bitCount) &+ growth &* w) } } diff --git a/stdlib/public/core/UTF16.swift b/stdlib/public/core/UTF16.swift index 5103e9b21bce5..7271c6e6e447a 100644 --- a/stdlib/public/core/UTF16.swift +++ b/stdlib/public/core/UTF16.swift @@ -112,7 +112,7 @@ extension UTF16.ReverseParser : Unicode.Parser, _UTFParser { public func _parseMultipleCodeUnits() -> (isValid: Bool, bitCount: UInt8) { _sanityCheck( // this case handled elsewhere - !Encoding._isScalar(UInt16(extendingOrTruncating: _buffer._storage))) + !Encoding._isScalar(UInt16(truncatingIfNeeded: _buffer._storage))) if _fastPath(_buffer._storage & 0xFC00_FC00 == 0xD800_DC00) { return (true, 2*16) } @@ -133,7 +133,7 @@ extension Unicode.UTF16.ForwardParser : Unicode.Parser, _UTFParser { public func _parseMultipleCodeUnits() -> (isValid: Bool, bitCount: UInt8) { _sanityCheck( // this case handled elsewhere - !Encoding._isScalar(UInt16(extendingOrTruncating: _buffer._storage))) + !Encoding._isScalar(UInt16(truncatingIfNeeded: _buffer._storage))) if _fastPath(_buffer._storage & 0xFC00_FC00 == 0xDC00_D800) { return (true, 2*16) } diff --git a/stdlib/public/core/UTF8.swift b/stdlib/public/core/UTF8.swift index afd2fd9683e86..6c8cf96ac77fe 100644 --- a/stdlib/public/core/UTF8.swift +++ b/stdlib/public/core/UTF8.swift @@ -93,9 +93,9 @@ extension Unicode.UTF8 : _UnicodeEncoding { ) -> EncodedScalar? { if _fastPath(FromEncoding.self == UTF16.self) { let c = _identityCast(content, to: UTF16.EncodedScalar.self) - var u0 = UInt16(extendingOrTruncating: c._storage) + var u0 = UInt16(truncatingIfNeeded: c._storage) if _fastPath(u0 < 0x80) { - return EncodedScalar(_containing: UInt8(extendingOrTruncating: u0)) + return EncodedScalar(_containing: UInt8(truncatingIfNeeded: u0)) } var r = UInt32(u0 & 0b0__11_1111) r &<<= 8 @@ -207,7 +207,7 @@ extension UTF8.ReverseParser : Unicode.Parser, _UTFParser { @inline(__always) @_inlineable public func _bufferedScalar(bitCount: UInt8) -> Encoding.EncodedScalar { - let x = UInt32(extendingOrTruncating: _buffer._storage.byteSwapped) + let x = UInt32(truncatingIfNeeded: _buffer._storage.byteSwapped) let shift = 32 &- bitCount return Encoding.EncodedScalar(_biasedBits: (x &+ 0x01010101) &>> shift) } diff --git a/stdlib/public/core/UTFEncoding.swift b/stdlib/public/core/UTFEncoding.swift index 0587d961a9de6..1a257b75d109c 100644 --- a/stdlib/public/core/UTFEncoding.swift +++ b/stdlib/public/core/UTFEncoding.swift @@ -44,11 +44,11 @@ where Encoding.EncodedScalar : RangeReplaceableCollection { // Non-ASCII, proceed to buffering mode. _buffer.append(codeUnit) } else if Encoding._isScalar( - Encoding.CodeUnit(extendingOrTruncating: _buffer._storage) + Encoding.CodeUnit(truncatingIfNeeded: _buffer._storage) ) { // ASCII in _buffer. We don't refill the buffer so we can return // to bufferless mode once we've exhausted it. - let codeUnit = Encoding.CodeUnit(extendingOrTruncating: _buffer._storage) + let codeUnit = Encoding.CodeUnit(truncatingIfNeeded: _buffer._storage) _buffer.remove(at: _buffer.startIndex) return .valid(Encoding.EncodedScalar(CollectionOfOne(codeUnit))) } @@ -74,7 +74,7 @@ where Encoding.EncodedScalar : RangeReplaceableCollection { _buffer._storage = UInt32( // widen to 64 bits so that we can empty the buffer in the 4-byte case - extendingOrTruncating: UInt64(_buffer._storage) &>> scalarBitCount) + truncatingIfNeeded: UInt64(_buffer._storage) &>> scalarBitCount) _buffer._bitCount = _buffer._bitCount &- scalarBitCount diff --git a/stdlib/public/core/Unicode.swift b/stdlib/public/core/Unicode.swift index aad15aea8878a..28326bd50f3e5 100644 --- a/stdlib/public/core/Unicode.swift +++ b/stdlib/public/core/Unicode.swift @@ -231,9 +231,9 @@ extension Unicode.UTF8 : UnicodeCodec { case .valid(let s): return ( result: UTF8.decode(s).value, - length: UInt8(extendingOrTruncating: s.count)) + length: UInt8(truncatingIfNeeded: s.count)) case .error(let l): - return (result: nil, length: UInt8(extendingOrTruncating: l)) + return (result: nil, length: UInt8(truncatingIfNeeded: l)) case .emptyInput: Builtin.unreachable() } } @@ -260,16 +260,16 @@ extension Unicode.UTF8 : UnicodeCodec { into processCodeUnit: (CodeUnit) -> Void ) { var s = encode(input)!._biasedBits - processCodeUnit(UInt8(extendingOrTruncating: s) &- 0x01) + processCodeUnit(UInt8(truncatingIfNeeded: s) &- 0x01) s &>>= 8 if _fastPath(s == 0) { return } - processCodeUnit(UInt8(extendingOrTruncating: s) &- 0x01) + processCodeUnit(UInt8(truncatingIfNeeded: s) &- 0x01) s &>>= 8 if _fastPath(s == 0) { return } - processCodeUnit(UInt8(extendingOrTruncating: s) &- 0x01) + processCodeUnit(UInt8(truncatingIfNeeded: s) &- 0x01) s &>>= 8 if _fastPath(s == 0) { return } - processCodeUnit(UInt8(extendingOrTruncating: s) &- 0x01) + processCodeUnit(UInt8(truncatingIfNeeded: s) &- 0x01) } /// Returns a Boolean value indicating whether the specified code unit is a @@ -411,10 +411,10 @@ extension Unicode.UTF16 : UnicodeCodec { into processCodeUnit: (CodeUnit) -> Void ) { var s = encode(input)!._storage - processCodeUnit(UInt16(extendingOrTruncating: s)) + processCodeUnit(UInt16(truncatingIfNeeded: s)) s &>>= 16 if _fastPath(s == 0) { return } - processCodeUnit(UInt16(extendingOrTruncating: s)) + processCodeUnit(UInt16(truncatingIfNeeded: s)) } } // @available(swift, obsoleted: 4.0, renamed: "Unicode.UTF16") @@ -611,14 +611,14 @@ extension UTF8.CodeUnit : _StringElement { public // @testable static func _toUTF16CodeUnit(_ x: UTF8.CodeUnit) -> UTF16.CodeUnit { _sanityCheck(x <= 0x7f, "should only be doing this with ASCII") - return UTF16.CodeUnit(extendingOrTruncating: x) + return UTF16.CodeUnit(truncatingIfNeeded: x) } public // @testable static func _fromUTF16CodeUnit( _ utf16: UTF16.CodeUnit ) -> UTF8.CodeUnit { _sanityCheck(utf16 <= 0x7f, "should only be doing this with ASCII") - return UTF8.CodeUnit(extendingOrTruncating: utf16) + return UTF8.CodeUnit(truncatingIfNeeded: utf16) } } @@ -669,7 +669,7 @@ extension UTF16 { /// - Returns: The leading surrogate code unit of `x` when encoded in UTF-16. public static func leadSurrogate(_ x: Unicode.Scalar) -> UTF16.CodeUnit { _precondition(width(x) == 2) - return 0xD800 + UTF16.CodeUnit(extendingOrTruncating: + return 0xD800 + UTF16.CodeUnit(truncatingIfNeeded: (x.value - 0x1_0000) &>> (10 as UInt32)) } @@ -692,7 +692,7 @@ extension UTF16 { /// - Returns: The trailing surrogate code unit of `x` when encoded in UTF-16. public static func trailSurrogate(_ x: Unicode.Scalar) -> UTF16.CodeUnit { _precondition(width(x) == 2) - return 0xDC00 + UTF16.CodeUnit(extendingOrTruncating: + return 0xDC00 + UTF16.CodeUnit(truncatingIfNeeded: (x.value - 0x1_0000) & (((1 as UInt32) &<< 10) - 1)) } diff --git a/stdlib/public/core/ValidUTF8Buffer.swift b/stdlib/public/core/ValidUTF8Buffer.swift index 0690e2fa4267d..290d7794aeeb4 100644 --- a/stdlib/public/core/ValidUTF8Buffer.swift +++ b/stdlib/public/core/ValidUTF8Buffer.swift @@ -35,7 +35,7 @@ public struct _ValidUTF8Buffer< internal init(_containing e: Element) { _sanityCheck( e != 192 && e != 193 && !(245...255).contains(e), "invalid UTF8 byte") - _biasedBits = Storage(extendingOrTruncating: e &+ 1) + _biasedBits = Storage(truncatingIfNeeded: e &+ 1) } } @@ -48,7 +48,7 @@ extension _ValidUTF8Buffer : Sequence { public mutating func next() -> Element? { if _biasedBits == 0 { return nil } defer { _biasedBits >>= 8 } - return Element(extendingOrTruncating: _biasedBits) &- 1 + return Element(truncatingIfNeeded: _biasedBits) &- 1 } internal var _biasedBits: Storage } @@ -94,7 +94,7 @@ extension _ValidUTF8Buffer : Collection { } public subscript(i: Index) -> Element { - return Element(extendingOrTruncating: i._biasedBits) &- 1 + return Element(truncatingIfNeeded: i._biasedBits) &- 1 } } @@ -177,7 +177,7 @@ extension _ValidUTF8Buffer : RangeReplaceableCollection { public mutating func append(contentsOf other: _ValidUTF8Buffer) { _debugPrecondition(count + other.count <= capacity) _biasedBits |= Storage( - extendingOrTruncating: other._biasedBits) &<< (count &<< 3) + truncatingIfNeeded: other._biasedBits) &<< (count &<< 3) } } diff --git a/test/Prototypes/BigInt.swift b/test/Prototypes/BigInt.swift index 75432029bb5ff..56374da8992e1 100644 --- a/test/Prototypes/BigInt.swift +++ b/test/Prototypes/BigInt.swift @@ -24,7 +24,7 @@ extension FixedWidthInteger { func addingFullWidth(_ other: Self) -> (high: Self, low: Self) { let sum = self.addingReportingOverflow(other) - return (sum.overflow == .overflow ? 1 : 0, sum.partialValue) + return (sum.overflow ? 1 : 0, sum.partialValue) } /// Returns the high and low parts of two seqeuential potentially overflowing @@ -33,8 +33,8 @@ extension FixedWidthInteger { (high: Self, low: Self) { let xy = x.addingReportingOverflow(y) let xyz = xy.partialValue.addingReportingOverflow(z) - let high: Self = (xy.overflow == .overflow ? 1 : 0) + - (xyz.overflow == .overflow ? 1 : 0) + let high: Self = (xy.overflow ? 1 : 0) + + (xyz.overflow ? 1 : 0) return (high, xyz.partialValue) } @@ -43,7 +43,7 @@ extension FixedWidthInteger { func subtractingWithBorrow(_ rhs: Self) -> (borrow: Self, partialValue: Self) { let difference = subtractingReportingOverflow(rhs) - return (difference.overflow == .overflow ? 1 : 0, difference.partialValue) + return (difference.overflow ? 1 : 0, difference.partialValue) } /// Returns a tuple containing the value that would be borrowed from a higher @@ -53,8 +53,8 @@ extension FixedWidthInteger { let firstDifference = subtractingReportingOverflow(x) let secondDifference = firstDifference.partialValue.subtractingReportingOverflow(y) - let borrow: Self = (firstDifference.overflow == .overflow ? 1 : 0) + - (secondDifference.overflow == .overflow ? 1 : 0) + let borrow: Self = (firstDifference.overflow ? 1 : 0) + + (secondDifference.overflow ? 1 : 0) return (borrow, secondDifference.partialValue) } } @@ -107,7 +107,7 @@ public struct _BigInt : var source = source if source < 0 as T { if source.bitWidth <= UInt64.bitWidth { - let sourceMag = Int(extendingOrTruncating: source).magnitude + let sourceMag = Int(truncatingIfNeeded: source).magnitude self = _BigInt(sourceMag) self.isNegative = true return @@ -123,7 +123,7 @@ public struct _BigInt : _sanityCheck(wordRatio != 0) for var sourceWord in source.words { for _ in 0..>= Word.bitWidth } } @@ -134,7 +134,7 @@ public struct _BigInt : self.init(source) } - public init(extendingOrTruncating source: T) { + public init(truncatingIfNeeded source: T) { self.init(source) } @@ -142,11 +142,11 @@ public struct _BigInt : self.init(source) } - public init(_ source: T) { + public init(_ source: T) { fatalError("Not implemented") } - public init?(exactly source: T) { + public init?(exactly source: T) { fatalError("Not implemented") } @@ -156,7 +156,7 @@ public struct _BigInt : if Word.bitWidth > UInt32.bitWidth { return Word(arc4random()) << 32 | Word(arc4random()) } else { - return Word(extendingOrTruncating: arc4random()) + return Word(truncatingIfNeeded: arc4random()) } } @@ -513,8 +513,7 @@ public struct _BigInt : // 0b11111111 + (0b11111101_____00000010) + 0b11111111 // (0b11111110_____00000001) + 0b11111111 // (0b11111111_____00000000) - _sanityCheck( - product.high.addingReportingOverflow(carry).overflow == .none) + _sanityCheck(!product.high.addingReportingOverflow(carry).overflow) carry = product.high &+ carry } @@ -668,7 +667,7 @@ public struct _BigInt : var word: UInt = 0 var shift = 0 for w in twosComplementData { - word |= UInt(extendingOrTruncating: w) << shift + word |= UInt(truncatingIfNeeded: w) << shift shift += Word.bitWidth if shift == UInt.bitWidth { words.append(word) @@ -1197,7 +1196,7 @@ struct Bit : FixedWidthInteger, UnsignedInteger { self = value } - init?(exactly source: T) { + init?(exactly source: T) { switch source { case T(0): value = 0 case T(1): value = 1 @@ -1206,7 +1205,7 @@ struct Bit : FixedWidthInteger, UnsignedInteger { } } - init(_ source: T) { + init(_ source: T) { self = Bit(exactly: source.rounded(.down))! } @@ -1219,7 +1218,7 @@ struct Bit : FixedWidthInteger, UnsignedInteger { } } - init(extendingOrTruncating source: T) { + init(truncatingIfNeeded source: T) { value = UInt8(source & 1) } @@ -1293,60 +1292,60 @@ struct Bit : FixedWidthInteger, UnsignedInteger { // Arithmetic Operations / Operators - func _checkOverflow(_ v: UInt8) -> ArithmeticOverflow { + func _checkOverflow(_ v: UInt8) -> Bool { let mask: UInt8 = ~0 << 1 - return v & mask == 0 ? .none : .overflow + return v & mask != 0 } func addingReportingOverflow(_ rhs: Bit) -> - (partialValue: Bit, overflow: ArithmeticOverflow) { + (partialValue: Bit, overflow: Bool) { let result = value &+ rhs.value return (Bit(result & 1), _checkOverflow(result)) } func subtractingReportingOverflow(_ rhs: Bit) -> - (partialValue: Bit, overflow: ArithmeticOverflow) { + (partialValue: Bit, overflow: Bool) { let result = value &- rhs.value return (Bit(result & 1), _checkOverflow(result)) } func multipliedReportingOverflow(by rhs: Bit) -> - (partialValue: Bit, overflow: ArithmeticOverflow) { + (partialValue: Bit, overflow: Bool) { let result = value &* rhs.value - return (Bit(result), .none) + return (Bit(result), false) } func dividedReportingOverflow(by rhs: Bit) -> - (partialValue: Bit, overflow: ArithmeticOverflow) { - return rhs == 0 ? (self, .none) : (self, .overflow) + (partialValue: Bit, overflow: Bool) { + return (self, rhs != 0) } func remainderReportingOverflow(dividingBy rhs: Bit) -> - (partialValue: Bit, overflow: ArithmeticOverflow) { + (partialValue: Bit, overflow: Bool) { fatalError() } static func +=(lhs: inout Bit, rhs: Bit) { let result = lhs.addingReportingOverflow(rhs) - assert(result.overflow == .none, "Addition overflow") + assert(!result.overflow, "Addition overflow") lhs = result.partialValue } static func -=(lhs: inout Bit, rhs: Bit) { let result = lhs.subtractingReportingOverflow(rhs) - assert(result.overflow == .none, "Subtraction overflow") + assert(!result.overflow, "Subtraction overflow") lhs = result.partialValue } static func *=(lhs: inout Bit, rhs: Bit) { let result = lhs.multipliedReportingOverflow(by: rhs) - assert(result.overflow == .none, "Multiplication overflow") + assert(!result.overflow, "Multiplication overflow") lhs = result.partialValue } static func /=(lhs: inout Bit, rhs: Bit) { let result = lhs.dividedReportingOverflow(by: rhs) - assert(result.overflow == .none, "Division overflow") + assert(!result.overflow, "Division overflow") lhs = result.partialValue } @@ -1620,7 +1619,7 @@ BigIntTests.test("BinaryInteger interop") { expectTrue(z < zComp + 1) let w = BigInt(UInt.max) - let wComp = UInt(extendingOrTruncating: w) + let wComp = UInt(truncatingIfNeeded: w) expectTrue(w == wComp) expectTrue(wComp == w) expectTrue(wComp - (1 as UInt) < w) @@ -1740,7 +1739,7 @@ BigInt8Tests.test("BinaryInteger interop") { expectTrue(z < zComp + 1) let w = BigInt8(UInt.max) - let wComp = UInt(extendingOrTruncating: w) + let wComp = UInt(truncatingIfNeeded: w) expectTrue(w == wComp) expectTrue(wComp == w) expectTrue(wComp - (1 as UInt) < w) @@ -1803,8 +1802,8 @@ BigInt8Tests.test("Bitwise").forEach(in: [ expectTrue(x & ~0 == x) expectTrue(x ^ 0 == x) expectTrue(x ^ ~0 == ~x) - expectTrue(x == BigInt8(Int(extendingOrTruncating: x))) - expectTrue(~x == BigInt8(~Int(extendingOrTruncating: x))) + expectTrue(x == BigInt8(Int(truncatingIfNeeded: x))) + expectTrue(~x == BigInt8(~Int(truncatingIfNeeded: x))) } } diff --git a/test/Prototypes/UnicodeDecoders.swift b/test/Prototypes/UnicodeDecoders.swift index 96491e3a25e28..ef9e6cba7a7ad 100644 --- a/test/Prototypes/UnicodeDecoders.swift +++ b/test/Prototypes/UnicodeDecoders.swift @@ -195,7 +195,7 @@ func checkStringProtocol( if !utfStr.contains(0) { if Encoding.self == Unicode.UTF8.self { - var ntbs = utfStr.map { CChar(extendingOrTruncating: $0) } + var ntbs = utfStr.map { CChar(truncatingIfNeeded: $0) } ntbs.append(0) expectEqualSequence( expected, utf32(S(cString: ntbs)), "\(S.self) init(cString:)") diff --git a/test/stdlib/Integers.swift.gyb b/test/stdlib/Integers.swift.gyb index 6c99c9b1a7752..fe93e978874c1 100644 --- a/test/stdlib/Integers.swift.gyb +++ b/test/stdlib/Integers.swift.gyb @@ -87,7 +87,7 @@ func expectEqual( } func expectEqual( - _ expected: (T, ArithmeticOverflow), _ actual: (T, ArithmeticOverflow), + _ expected: (T, Bool), _ actual: (T, Bool), _ message: @autoclosure () -> String = "", stackTrace: SourceLocStack = SourceLocStack(), showFrame: Bool = true, @@ -322,46 +322,46 @@ tests.test("ConversionDWordToWordC") { _ = UWord(tooLarge) } -tests.test("extendingOrTruncating") { +tests.test("truncatingIfNeeded") { - expectEqual(-2, Int8(extendingOrTruncating: UInt8.max - 1)) - expectEqual(3, Int8(extendingOrTruncating: 3 as UInt8)) - expectEqual(UInt8.max - 1, UInt8(extendingOrTruncating: -2 as Int8)) - expectEqual(3, UInt8(extendingOrTruncating: 3 as Int8)) + expectEqual(-2, Int8(truncatingIfNeeded: UInt8.max - 1)) + expectEqual(3, Int8(truncatingIfNeeded: 3 as UInt8)) + expectEqual(UInt8.max - 1, UInt8(truncatingIfNeeded: -2 as Int8)) + expectEqual(3, UInt8(truncatingIfNeeded: 3 as Int8)) - expectEqual(-2, DWord(extendingOrTruncating: UDWord.max - 1)) - expectEqual(3, DWord(extendingOrTruncating: 3 as UDWord)) - expectEqual(UDWord.max - 1, UDWord(extendingOrTruncating: -2 as DWord)) - expectEqual(3, UDWord(extendingOrTruncating: 3 as DWord)) + expectEqual(-2, DWord(truncatingIfNeeded: UDWord.max - 1)) + expectEqual(3, DWord(truncatingIfNeeded: 3 as UDWord)) + expectEqual(UDWord.max - 1, UDWord(truncatingIfNeeded: -2 as DWord)) + expectEqual(3, UDWord(truncatingIfNeeded: 3 as DWord)) - expectEqual(-2, Int32(extendingOrTruncating: -2 as Int8)) - expectEqual(3, Int32(extendingOrTruncating: 3 as Int8)) - expectEqual(127, Int32(extendingOrTruncating: 127 as UInt8)) - expectEqual(129, Int32(extendingOrTruncating: 129 as UInt8)) - expectEqual((1 << 31 - 1) << 1, UInt32(extendingOrTruncating: -2 as Int8)) - expectEqual(3, UInt32(extendingOrTruncating: 3 as Int8)) - expectEqual(128, UInt32(extendingOrTruncating: 128 as UInt8)) - expectEqual(129, UInt32(extendingOrTruncating: 129 as UInt8)) + expectEqual(-2, Int32(truncatingIfNeeded: -2 as Int8)) + expectEqual(3, Int32(truncatingIfNeeded: 3 as Int8)) + expectEqual(127, Int32(truncatingIfNeeded: 127 as UInt8)) + expectEqual(129, Int32(truncatingIfNeeded: 129 as UInt8)) + expectEqual((1 << 31 - 1) << 1, UInt32(truncatingIfNeeded: -2 as Int8)) + expectEqual(3, UInt32(truncatingIfNeeded: 3 as Int8)) + expectEqual(128, UInt32(truncatingIfNeeded: 128 as UInt8)) + expectEqual(129, UInt32(truncatingIfNeeded: 129 as UInt8)) - expectEqual(-2, DWord(extendingOrTruncating: -2 as Int8)) - expectEqual(3, DWord(extendingOrTruncating: 3 as Int8)) - expectEqual(127, DWord(extendingOrTruncating: 127 as UInt8)) - expectEqual(129, DWord(extendingOrTruncating: 129 as UInt8)) + expectEqual(-2, DWord(truncatingIfNeeded: -2 as Int8)) + expectEqual(3, DWord(truncatingIfNeeded: 3 as Int8)) + expectEqual(127, DWord(truncatingIfNeeded: 127 as UInt8)) + expectEqual(129, DWord(truncatingIfNeeded: 129 as UInt8)) expectEqual( (1 << ${word_bits*2-1} - 1) << 1, - UDWord(extendingOrTruncating: -2 as Int8)) - expectEqual(3, UDWord(extendingOrTruncating: 3 as Int8)) - expectEqual(128, UDWord(extendingOrTruncating: 128 as UInt8)) - expectEqual(129, UDWord(extendingOrTruncating: 129 as UInt8)) + UDWord(truncatingIfNeeded: -2 as Int8)) + expectEqual(3, UDWord(truncatingIfNeeded: 3 as Int8)) + expectEqual(128, UDWord(truncatingIfNeeded: 128 as UInt8)) + expectEqual(129, UDWord(truncatingIfNeeded: 129 as UInt8)) - expectEqual(-2, Int8(extendingOrTruncating: -2 as DWord)) - expectEqual(-2, Int8(extendingOrTruncating: -1 << 67 - 2 as DWord)) - expectEqual(127, Int8(extendingOrTruncating: 127 as UDWord)) - expectEqual(-127, Int8(extendingOrTruncating: 129 as UDWord)) - expectEqual(0b1111_1100, UInt8(extendingOrTruncating: -4 as DWord)) - expectEqual(0b1111_1100, UInt8(extendingOrTruncating: -1 << 67 - 4 as DWord)) - expectEqual(128, UInt8(extendingOrTruncating: 128 + 1024 as UDWord)) - expectEqual(129, UInt8(extendingOrTruncating: 129 + 1024 as UDWord)) + expectEqual(-2, Int8(truncatingIfNeeded: -2 as DWord)) + expectEqual(-2, Int8(truncatingIfNeeded: -1 << 67 - 2 as DWord)) + expectEqual(127, Int8(truncatingIfNeeded: 127 as UDWord)) + expectEqual(-127, Int8(truncatingIfNeeded: 129 as UDWord)) + expectEqual(0b1111_1100, UInt8(truncatingIfNeeded: -4 as DWord)) + expectEqual(0b1111_1100, UInt8(truncatingIfNeeded: -1 << 67 - 4 as DWord)) + expectEqual(128, UInt8(truncatingIfNeeded: 128 + 1024 as UDWord)) + expectEqual(129, UInt8(truncatingIfNeeded: 129 + 1024 as UDWord)) } tests.test("HeterogeneousEquality") { @@ -706,7 +706,7 @@ dwTests.test("Nested") { let (y, o) = x.addingReportingOverflow(1) expectEqual(y, 0) expectTrue(y == (0 as Int)) - expectTrue(o == .overflow) + expectTrue(o) } do { @@ -716,7 +716,7 @@ dwTests.test("Nested") { expectLT(y, 0) expectTrue(y < (0 as Int)) expectTrue(y < (0 as UInt)) - expectTrue(o == .overflow) + expectTrue(o) } expectFalse(UInt1024.isSigned) @@ -732,7 +732,7 @@ dwTests.test("inits") { expectTrue(DWU16(UInt16.max) == UInt16.max) expectNil(DWU16(exactly: UInt32.max)) - expectEqual(DWU16(extendingOrTruncating: UInt64.max), DWU16.max) + expectEqual(DWU16(truncatingIfNeeded: UInt64.max), DWU16.max) expectCrashLater() _ = DWU16(UInt32.max) @@ -741,7 +741,7 @@ dwTests.test("inits") { dwTests.test("TwoWords") { typealias DW = DoubleWidth - expectEqual(-1 as DW, DW(extendingOrTruncating: -1 as Int8)) + expectEqual(-1 as DW, DW(truncatingIfNeeded: -1 as Int8)) expectNil(Int(exactly: DW(Int.min) - 1)) expectNil(Int(exactly: DW(Int.max) + 1)) diff --git a/validation-test/compiler_crashers_2/0109-sr4737.swift b/validation-test/compiler_crashers_2/0109-sr4737.swift index d97a1357a72e4..1d84f4c06ff08 100644 --- a/validation-test/compiler_crashers_2/0109-sr4737.swift +++ b/validation-test/compiler_crashers_2/0109-sr4737.swift @@ -33,8 +33,8 @@ public struct _UIntBuffer< @inline(__always) public init(containing e: Element) { - _storage = Storage(extendingOrTruncating: e) - _bitCount = UInt8(extendingOrTruncating: Element.bitWidth) + _storage = Storage(truncatingIfNeeded: e) + _bitCount = UInt8(truncatingIfNeeded: Element.bitWidth) } } @@ -51,7 +51,7 @@ extension _UIntBuffer : Sequence { _impl._storage = _impl._storage &>> Element.bitWidth _impl._bitCount = _impl._bitCount &- _impl._elementWidth } - return Element(extendingOrTruncating: _impl._storage) + return Element(truncatingIfNeeded: _impl._storage) } @_versioned var _impl: _UIntBuffer @@ -74,7 +74,7 @@ extension _UIntBuffer : Sequence { var s: Storage = 0 for x in self { s <<= Element.bitWidth - s |= Storage(extendingOrTruncating: x) + s |= Storage(truncatingIfNeeded: x) } return Self(_storage: s, _bitCount: _bitCount) } @@ -116,13 +116,13 @@ extension _UIntBuffer : Collection { @_versioned internal var _elementWidth : UInt8 { - return UInt8(extendingOrTruncating: Element.bitWidth) + return UInt8(truncatingIfNeeded: Element.bitWidth) } public subscript(i: Index) -> Element { @inline(__always) get { - return Element(extendingOrTruncating: _storage &>> i.bitOffset) + return Element(truncatingIfNeeded: _storage &>> i.bitOffset) } } } @@ -141,7 +141,7 @@ extension _UIntBuffer : RandomAccessCollection { @inline(__always) public func index(_ i: Index, offsetBy n: IndexDistance) -> Index { let x = IndexDistance(i.bitOffset) &+ n &* Element.bitWidth - return Index(bitOffset: UInt8(extendingOrTruncating: x)) + return Index(bitOffset: UInt8(truncatingIfNeeded: x)) } @inline(__always) @@ -220,7 +220,7 @@ extension _UIntBuffer : RangeReplaceableCollection { _storage |= replacement1._storage &<< (headCount &* w) _storage |= tailBits &<< ((tailOffset &+ growth) &* w) _bitCount = UInt8( - extendingOrTruncating: IndexDistance(_bitCount) &+ growth &* w) + truncatingIfNeeded: IndexDistance(_bitCount) &+ growth &* w) } } //===----------------------------------------------------------------------===// @@ -520,10 +520,10 @@ extension _UTFEncoding { } // Non-ASCII, proceed to buffering mode. buffer.append(codeUnit) - } else if Self._isScalar(CodeUnit(extendingOrTruncating: buffer._storage)) { + } else if Self._isScalar(CodeUnit(truncatingIfNeeded: buffer._storage)) { // ASCII in buffer. We don't refill the buffer so we can return // to bufferless mode once we've exhausted it. - let codeUnit = CodeUnit(extendingOrTruncating: buffer._storage) + let codeUnit = CodeUnit(truncatingIfNeeded: buffer._storage) buffer.remove(at: buffer.startIndex) return ( EncodedScalar(containing: codeUnit), @@ -826,7 +826,7 @@ extension _UTF16Decoder { public // @testable func _parseMultipleCodeUnits() -> (isValid: Bool, bitCount: UInt8) { _sanityCheck( // this case handled elsewhere - !Self._isScalar(UInt16(extendingOrTruncating: buffer._storage))) + !Self._isScalar(UInt16(truncatingIfNeeded: buffer._storage))) if _fastPath(buffer._storage & 0xFC00_FC00 == Self._surrogatePattern) { return (true, 2*16) diff --git a/validation-test/stdlib/FixedPoint.swift.gyb b/validation-test/stdlib/FixedPoint.swift.gyb index eca0aa313c985..6cecc0c23282a 100644 --- a/validation-test/stdlib/FixedPoint.swift.gyb +++ b/validation-test/stdlib/FixedPoint.swift.gyb @@ -327,11 +327,11 @@ FixedPoint.test("BitwiseOperations/UInt64") { } FixedPoint.test("OverflowCheck") { - expectEqual((partialValue: 9, overflow: .none), + expectEqual((partialValue: 9, overflow: false), (4 as Int8).addingReportingOverflow(5)) - expectEqual((partialValue: -128, overflow: .overflow), + expectEqual((partialValue: -128, overflow: true), (1 as Int8).addingReportingOverflow(127)) - expectEqual((partialValue: 0, overflow: .overflow), + expectEqual((partialValue: 0, overflow: true), (2 as UInt8).multipliedReportingOverflow(by: 128)) } diff --git a/validation-test/stdlib/FixedPointArithmeticTraps.swift.gyb b/validation-test/stdlib/FixedPointArithmeticTraps.swift.gyb index 4ed2f92f2d4c4..a497b8058a8e7 100644 --- a/validation-test/stdlib/FixedPointArithmeticTraps.swift.gyb +++ b/validation-test/stdlib/FixedPointArithmeticTraps.swift.gyb @@ -15,7 +15,7 @@ import StdlibUnittest // not folded. func expectOverflow( - _ res: (T, overflow: Bool), + _ res: (partialValue: T, overflow: Bool), //===--- TRACE boilerplate ----------------------------------------------===// _ message: @autoclosure () -> String = "", showFrame: Bool = true, @@ -28,7 +28,7 @@ func expectOverflow( } func expectNoOverflow( - _ res: (T, overflow: Bool), + _ res: (partialValue: T, overflow: Bool), //===--- TRACE boilerplate ----------------------------------------------===// _ message: @autoclosure () -> String = "", showFrame: Bool = true, @@ -40,32 +40,6 @@ func expectNoOverflow( stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)) } -func expectOverflow( - _ res: (partialValue: T, overflow: ArithmeticOverflow), - //===--- TRACE boilerplate ----------------------------------------------===// - _ message: @autoclosure () -> String = "", - showFrame: Bool = true, - stackTrace: SourceLocStack = SourceLocStack(), - file: String = #file, line: UInt = #line -) { - expectTrue( - res.overflow == .overflow, "expected overflow", - stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)) -} - -func expectNoOverflow( - _ res: (partialValue: T, overflow: ArithmeticOverflow), - //===--- TRACE boilerplate ----------------------------------------------===// - _ message: @autoclosure () -> String = "", - showFrame: Bool = true, - stackTrace: SourceLocStack = SourceLocStack(), - file: String = #file, line: UInt = #line -) { - expectTrue( - res.overflow == .none, "expected no overflow", - stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)) -} - %{ from SwiftIntTypes import all_integer_types diff --git a/validation-test/stdlib/Prototypes/PersistentVector.swift.gyb b/validation-test/stdlib/Prototypes/PersistentVector.swift.gyb index 0e0910d92d142..cedd73614cc8a 100644 --- a/validation-test/stdlib/Prototypes/PersistentVector.swift.gyb +++ b/validation-test/stdlib/Prototypes/PersistentVector.swift.gyb @@ -217,7 +217,7 @@ struct _${name}Bitmap { % if underlyingType == 'UInt': let iAsUnderlyingType = UInt(bitPattern: i) % elif underlyingType == 'UInt32': - let iAsUnderlyingType = UInt32(extendingOrTruncating: i) + let iAsUnderlyingType = UInt32(truncatingIfNeeded: i) % end _bits = (_bits & ~(1 << iAsUnderlyingType)) @@ -230,7 +230,7 @@ struct _${name}Bitmap { % if underlyingType == 'UInt': let iAsUnderlyingType = UInt(bitPattern: i) % elif underlyingType == 'UInt32': - let iAsUnderlyingType = UInt32(extendingOrTruncating: i) + let iAsUnderlyingType = UInt32(truncatingIfNeeded: i) % end let lowBits = _bits & ((1 << iAsUnderlyingType) - 1) return _${name}Bitmap(_bits: lowBits).setBitCount