Skip to content

Commit ff89fce

Browse files
Make FloatingPoint require that Self.Magnitude == Self (#17323)
* Make FloatingPoint require that Self.Magnitude == Self We didn't have the where clause to express this constraint at the time that the FloatingPoint protocol was implemented, but we do now. This is not a semantic change to FloatingPoint, which has always bound IEEE-754 arithmetic types, for which this constraint would necessarily hold, but it does effect the type system. For example, currently the following function does not type check: ~~~~ func foo<T>(x: T) -> T where T: FloatingPoint { var r = x.remainder(dividingBy: 1) return r.magnitude } ~~~~ with this change, it compiles correctly. Having done this, we no longer need to have a separate `abs` defined on FloatingPoint; we can use the existing function defined on `SignedNumeric` instead. Additionally mark the global `fabs` defined in the platform C module deprecated in favor of the Swift `abs`; we don't need to carry two names for this function going forward.
1 parent 40b4eda commit ff89fce

File tree

3 files changed

+4
-27
lines changed

3 files changed

+4
-27
lines changed

stdlib/public/Platform/tgmath.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import SwiftShims
1414

1515
// Generic functions implementable directly on FloatingPoint.
1616
@_transparent
17-
public func fabs<T: FloatingPoint>(_ x: T) -> T
18-
where T.Magnitude == T {
17+
@available(swift, deprecated: 4.2, renamed: "abs")
18+
public func fabs<T: FloatingPoint>(_ x: T) -> T {
1919
return x.magnitude
2020
}
2121

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,8 @@ word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
167167
/// print("Average: \(average)°F in \(validTemps.count) " +
168168
/// "out of \(tempsFahrenheit.count) observations.")
169169
/// // Prints "Average: 74.84°F in 5 out of 7 observations."
170-
public protocol FloatingPoint: SignedNumeric, Strideable, Hashable {
171-
172-
// For the minimumMagnitude and maximumMagnitude methods
173-
/// A type that can represent the absolute value of any possible value of the
174-
/// conforming type.
175-
associatedtype Magnitude = Self
170+
public protocol FloatingPoint : SignedNumeric, Strideable, Hashable
171+
where Magnitude == Self {
176172

177173
/// A type that can represent any written exponent.
178174
associatedtype Exponent: SignedInteger
@@ -2505,13 +2501,6 @@ where Self.RawSignificand : FixedWidthInteger,
25052501

25062502
% end
25072503

2508-
/// Returns the absolute value of `x`.
2509-
@inlinable // FIXME(sil-serialize-all)
2510-
@_transparent
2511-
public func abs<T : FloatingPoint>(_ x: T) -> T where T.Magnitude == T {
2512-
return x.magnitude
2513-
}
2514-
25152504
extension FloatingPoint {
25162505
@inlinable // FIXME(sil-serialize-all)
25172506
@available(swift, obsoleted: 4, message: "Please use operators instead.")

test/SourceKit/CodeComplete/complete_moduleimportdepth.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ func test() {
3535
// CHECK: key.modulename: "Swift"
3636
// CHECK-NEXT: },
3737

38-
// CHECK-LABEL: key.name: "abs(:)",
39-
// CHECK-NEXT: key.sourcetext: "abs(<#T##x: FloatingPoint##FloatingPoint#>)",
40-
// CHECK-NEXT: key.description: "abs(x: FloatingPoint)",
41-
// CHECK-NEXT: key.typename: "FloatingPoint",
42-
// CHECK-NEXT: key.doc.brief: "Returns the absolute value of x.",
43-
// CHECK-NEXT: key.context: source.codecompletion.context.othermodule,
44-
// CHECK-NEXT: key.moduleimportdepth: 1,
45-
// CHECK-NEXT: key.num_bytes_to_erase: 0,
46-
// CHECK-NOT: key.modulename
47-
// CHECK: key.modulename: "Swift"
48-
// CHECK-NEXT: },
49-
5038
// FooHelper.FooHelperExplicit == 1
5139
// CHECK-LABEL: key.name: "fooHelperExplicitFrameworkFunc1(:)",
5240
// CHECK-NEXT: key.sourcetext: "fooHelperExplicitFrameworkFunc1(<#T##a: Int32##Int32#>)",

0 commit comments

Comments
 (0)