Skip to content

Commit e827d22

Browse files
moiseevairspeedswift
authored andcommitted
[swift-4.0-branch][stdlib] Removing < overloads (#9343)
* [stdlib] Removing a few harmful overloads of < At some point during the implementation of integer protocols these overloads were necessary to make expressions like `i32 < 0` be faster and unambiguous. Now they are no longer necessary, and also cause problems for expressions like `(u64 - u64) < u64`, where they cause the deprecated `func - (Strideable, Strideable) -> Stride` be used, which is wrong, as it will trap in many cases, where `func - (UInt64, UInt64) -> UInt64` would not. Fixes: <rdar://problem/31909031> * Adding tests for the removed < overloads * [stdlib] Addressing the benchmark regression
1 parent 72da6a6 commit e827d22

File tree

4 files changed

+13
-74
lines changed

4 files changed

+13
-74
lines changed

stdlib/public/core/Integers.swift.gyb

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,80 +2640,6 @@ public struct ${Self}
26402640
return Bool(Builtin.cmp_${u}lt_Int${bits}(lhs._value, rhs._value))
26412641
}
26422642

2643-
// FIXME(integers): it should be possible to turn this into an optimizer pass
2644-
% if Self != 'Int':
2645-
@_transparent
2646-
public static func < (lhs: ${Self}, rhs: Int) -> Bool {
2647-
% if not signed:
2648-
if rhs < 0 { return false }
2649-
% end
2650-
2651-
% if bits < word_bits:
2652-
let lhs_ = Int(_truncatingBits: lhs._lowUWord)
2653-
return lhs_ < rhs
2654-
% elif bits == word_bits:
2655-
return Bool(Builtin.cmp_${u}lt_Int${bits}(lhs._value, rhs._value))
2656-
% else:
2657-
let rhs_ = ${Self}(_truncatingBits: rhs._lowUWord)
2658-
return lhs < rhs_
2659-
% end
2660-
}
2661-
2662-
@_transparent
2663-
public static func < (lhs: Int, rhs: ${Self}) -> Bool {
2664-
% if not signed:
2665-
if lhs < 0 { return true }
2666-
% end
2667-
% if bits < word_bits:
2668-
let rhs_ = Int(_truncatingBits: rhs._lowUWord)
2669-
return lhs < rhs_
2670-
% elif bits == word_bits:
2671-
return Bool(Builtin.cmp_${u}lt_Int${bits}(lhs._value, rhs._value))
2672-
% else:
2673-
let lhs_ = ${Self}(_truncatingBits: lhs._lowUWord)
2674-
return lhs_ < rhs
2675-
% end
2676-
}
2677-
2678-
% for Args in ['lhs: %s, rhs: Int' % Self, 'lhs: Int, rhs: %s' % Self]:
2679-
@_transparent
2680-
public static func > (${Args}) -> Bool {
2681-
return rhs < lhs
2682-
}
2683-
2684-
@_transparent
2685-
public static func <= (${Args}) -> Bool {
2686-
return !(rhs < lhs)
2687-
}
2688-
2689-
@_transparent
2690-
public static func >= (${Args}) -> Bool {
2691-
return !(lhs < rhs)
2692-
}
2693-
2694-
% end
2695-
2696-
% else: # if Self != 'Int'
2697-
2698-
// FIXME(integers): Comparable default implementations should really be
2699-
// chosen by the compiler. <rdar://problem/29340480>
2700-
@_transparent
2701-
public static func > (lhs: Int, rhs: Int) -> Bool {
2702-
return rhs < lhs
2703-
}
2704-
2705-
@_transparent
2706-
public static func <= (lhs: Int, rhs: Int) -> Bool {
2707-
return !(rhs < lhs)
2708-
}
2709-
2710-
@_transparent
2711-
public static func >= (lhs: Int, rhs: Int) -> Bool {
2712-
return !(lhs < rhs)
2713-
}
2714-
2715-
% end # if Self != 'Int'
2716-
27172643
// FIXME(integers): pending compiler improvements.
27182644
// Basically, most simple arithmetic expressions become too complex when `+`
27192645
// gets defined on the protocol rather than on concrete type.

stdlib/public/core/StringComparable.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ extension String {
120120
}
121121

122122
extension String : Equatable {
123+
@inline(__always)
123124
public static func == (lhs: String, rhs: String) -> Bool {
124125
#if _runtime(_ObjC)
125126
// We only want to perform this optimization on objc runtimes. Elsewhere,

test/stdlib/MixedTypeArithmeticsDiagnostics3.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ func mixedTypeArithemtics() {
3333
x += (42 as Int)
3434
}
3535
}
36+
37+
func radar31909031() {
38+
let x = UInt64()
39+
let y = UInt64()
40+
_ = (x - y) < UInt64(42) // should not produce a mixed-type warning
41+
}

test/stdlib/MixedTypeArithmeticsDiagnostics4.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ func mixedTypeArithemtics() {
3333
x += (42 as Int)
3434
}
3535
}
36+
37+
func radar31909031() {
38+
let x = UInt64()
39+
let y = UInt64()
40+
_ = (x - y) < UInt64(42) // should not produce a mixed-type warning
41+
}

0 commit comments

Comments
 (0)