Skip to content

Commit 5ac2d24

Browse files
Allow Comparable: ~Escapable (#85891)
Builds on #85854
1 parent fc25044 commit 5ac2d24

File tree

5 files changed

+58
-28
lines changed

5 files changed

+58
-28
lines changed

stdlib/public/core/Comparable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
/// (`FloatingPoint.nan`) compares as neither less than, greater than, nor
136136
/// equal to any normal floating-point value. Exceptional values need not
137137
/// take part in the strict total order.
138-
public protocol Comparable: Equatable, ~Copyable {
138+
public protocol Comparable: Equatable, ~Copyable, ~Escapable {
139139
/// Returns a Boolean value indicating whether the value of the first
140140
/// argument is less than that of the second argument.
141141
///
@@ -173,7 +173,7 @@ public protocol Comparable: Equatable, ~Copyable {
173173
static func > (lhs: borrowing Self, rhs: borrowing Self) -> Bool
174174
}
175175

176-
extension Comparable where Self: ~Copyable {
176+
extension Comparable where Self: ~Copyable & ~Escapable {
177177
/// Returns a Boolean value indicating whether the value of the first argument
178178
/// is greater than that of the second argument.
179179
///

test/SILOptimizer/mandatory_performance_optimizations.sil

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bb0:
8484
sil [no_allocation] [ossa] @deserialize_and_inline_after_devirtualize : $@convention(thin) (@in Int) -> () {
8585
bb0(%0 : $*Int):
8686
%1 = metatype $@thick Int.Type
87-
%2 = witness_method $Int, #Comparable."<" : <Self where Self : Comparable, Self : ~Copyable> (Self.Type) -> (borrowing Self, borrowing Self) -> Bool : $@convention(witness_method: Comparable) <τ_0_0 where τ_0_0 : Comparable> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> Bool
87+
%2 = witness_method $Int, #Comparable."<" : <Self where Self : Comparable, Self : ~Copyable, Self : ~Escapable> (Self.Type) -> (borrowing Self, borrowing Self) -> Bool : $@convention(witness_method: Comparable) <τ_0_0 where τ_0_0 : Comparable> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> Bool
8888
%3 = apply %2<Int>(%0, %0, %1) : $@convention(witness_method: Comparable) <τ_0_0 where τ_0_0 : Comparable> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> Bool
8989
%4 = tuple()
9090
return %4 : $()
@@ -124,7 +124,7 @@ bb0(%0 : $*Int, %1 : $*Int, %2 : $@thick Int.Type):
124124

125125
sil_witness_table public_external [serialized] Int: Comparable module Swift {
126126
base_protocol Equatable: Int: Equatable module Swift
127-
method #Comparable."<": <Self where Self : Comparable, Self : ~Copyable> (Self.Type) -> (borrowing Self, borrowing Self) -> Bool : @$sSiSLsSL1loiySbx_xtFZTW
127+
method #Comparable."<": <Self where Self : Comparable, Self : ~Copyable, Self : ~Escapable> (Self.Type) -> (borrowing Self, borrowing Self) -> Bool : @$sSiSLsSL1loiySbx_xtFZTW
128128
}
129129

130130
sil [ossa] @get_int_value : $@convention(thin) () -> Int32 {

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ Protocol CodingKey has added inherited protocol Copyable
7171
Protocol CodingKey has added inherited protocol Escapable
7272
Protocol Collection has added inherited protocol Copyable
7373
Protocol Collection has added inherited protocol Escapable
74-
Protocol Comparable has added inherited protocol Escapable
7574
Protocol CustomDebugStringConvertible has added inherited protocol Copyable
7675
Protocol CustomDebugStringConvertible has added inherited protocol Escapable
7776
Protocol CustomLeafReflectable has added inherited protocol Copyable
@@ -384,18 +383,18 @@ Func Equatable.==(_:_:) has generic signature change from <Self where Self : Swi
384383
Func Equatable.==(_:_:) has parameter 0 changing from Default to Shared
385384
Func Equatable.==(_:_:) has parameter 1 changing from Default to Shared
386385

387-
// Comparable: ~Copyable
388-
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable>
389-
Func Comparable.<(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
386+
// Comparable: ~Copyable & ~Escapable
387+
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable, Self : ~Escapable>
388+
Func Comparable.<(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
390389
Func Comparable.<(_:_:) has parameter 0 changing from Default to Shared
391390
Func Comparable.<(_:_:) has parameter 1 changing from Default to Shared
392-
Func Comparable.<=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
391+
Func Comparable.<=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
393392
Func Comparable.<=(_:_:) has parameter 0 changing from Default to Shared
394393
Func Comparable.<=(_:_:) has parameter 1 changing from Default to Shared
395-
Func Comparable.>(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
394+
Func Comparable.>(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
396395
Func Comparable.>(_:_:) has parameter 0 changing from Default to Shared
397396
Func Comparable.>(_:_:) has parameter 1 changing from Default to Shared
398-
Func Comparable.>=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
397+
Func Comparable.>=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
399398
Func Comparable.>=(_:_:) has parameter 0 changing from Default to Shared
400399
Func Comparable.>=(_:_:) has parameter 1 changing from Default to Shared
401400

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ Protocol CodingKey has added inherited protocol Copyable
189189
Protocol CodingKey has added inherited protocol Escapable
190190
Protocol Collection has added inherited protocol Copyable
191191
Protocol Collection has added inherited protocol Escapable
192-
Protocol Comparable has added inherited protocol Escapable
193192
Protocol CustomDebugStringConvertible has added inherited protocol Copyable
194193
Protocol CustomDebugStringConvertible has added inherited protocol Escapable
195194
Protocol CustomLeafReflectable has added inherited protocol Copyable
@@ -889,23 +888,23 @@ Func Equatable.==(_:_:) has generic signature change from <Self where Self : Swi
889888
Func Equatable.==(_:_:) has parameter 0 changing from Default to Shared
890889
Func Equatable.==(_:_:) has parameter 1 changing from Default to Shared
891890

892-
// Comparable: ~Copyable
893-
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable>
894-
Func Comparable.<(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
891+
// Comparable: ~Copyable & ~Escapable
892+
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable, Self : ~Escapable>
893+
Func Comparable.<(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
895894
Func Comparable.<(_:_:) has parameter 0 changing from Default to Shared
896895
Func Comparable.<(_:_:) has parameter 1 changing from Default to Shared
897-
Func Comparable.<=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
898-
Func Comparable.<=(_:_:) has mangled name changing from 'static (extension in Swift):Swift.Comparable.<= infix(A, A) -> Swift.Bool' to 'static (extension in Swift):Swift.Comparable< where A: ~Swift.Copyable>.<= infix(A, A) -> Swift.Bool'
896+
Func Comparable.<=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
897+
Func Comparable.<=(_:_:) has mangled name changing from 'static (extension in Swift):Swift.Comparable.<= infix(A, A) -> Swift.Bool' to 'static (extension in Swift):Swift.Comparable< where A: ~Swift.Copyable, A: ~Swift.Escapable>.<= infix(A, A) -> Swift.Bool'
899898
Func Comparable.<=(_:_:) has parameter 0 changing from Default to Shared
900899
Func Comparable.<=(_:_:) has parameter 1 changing from Default to Shared
901900
Func Comparable.<=(_:_:) is now with @_preInverseGenerics
902-
Func Comparable.>(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
903-
Func Comparable.>(_:_:) has mangled name changing from 'static (extension in Swift):Swift.Comparable.> infix(A, A) -> Swift.Bool' to 'static (extension in Swift):Swift.Comparable< where A: ~Swift.Copyable>.> infix(A, A) -> Swift.Bool'
901+
Func Comparable.>(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
902+
Func Comparable.>(_:_:) has mangled name changing from 'static (extension in Swift):Swift.Comparable.> infix(A, A) -> Swift.Bool' to 'static (extension in Swift):Swift.Comparable< where A: ~Swift.Copyable, A: ~Swift.Escapable>.> infix(A, A) -> Swift.Bool'
904903
Func Comparable.>(_:_:) has parameter 0 changing from Default to Shared
905904
Func Comparable.>(_:_:) has parameter 1 changing from Default to Shared
906905
Func Comparable.>(_:_:) is now with @_preInverseGenerics
907-
Func Comparable.>=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
908-
Func Comparable.>=(_:_:) has mangled name changing from 'static (extension in Swift):Swift.Comparable.>= infix(A, A) -> Swift.Bool' to 'static (extension in Swift):Swift.Comparable< where A: ~Swift.Copyable>.>= infix(A, A) -> Swift.Bool'
906+
Func Comparable.>=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
907+
Func Comparable.>=(_:_:) has mangled name changing from 'static (extension in Swift):Swift.Comparable.>= infix(A, A) -> Swift.Bool' to 'static (extension in Swift):Swift.Comparable< where A: ~Swift.Copyable, A: ~Swift.Escapable>.>= infix(A, A) -> Swift.Bool'
909908
Func Comparable.>=(_:_:) has parameter 0 changing from Default to Shared
910909
Func Comparable.>=(_:_:) has parameter 1 changing from Default to Shared
911910
Func Comparable.>=(_:_:) is now with @_preInverseGenerics

test/stdlib/NoncopyableComparable.swift

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,44 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
// RUN: %target-run-simple-swift
12+
// RUN: %target-run-simple-swift(-enable-experimental-feature Lifetimes)
1313
// REQUIRES: executable_test
14+
// REQUIRES: swift_feature_Lifetimes
1415

1516
import StdlibUnittest
1617

1718
let NoncopyableComparableTests = TestSuite("NoncopyableComparable")
1819

19-
struct Noncopyable<Wrapped: ~Copyable>: ~Copyable {
20+
struct Noncopyable<Wrapped: ~Copyable & ~Escapable>: ~Copyable, ~Escapable {
2021
var wrapped: Wrapped
2122
}
2223

23-
extension Noncopyable: Equatable where Wrapped: Equatable & ~Copyable { }
24+
extension Noncopyable: Equatable where Wrapped: Equatable & ~Copyable & ~Escapable { }
2425

25-
extension Noncopyable: Comparable where Wrapped: Comparable & ~Copyable {
26+
extension Noncopyable: Comparable where Wrapped: Comparable & ~Copyable & ~Escapable {
2627
static func < (lhs: borrowing Self, rhs: borrowing Self) -> Bool { lhs.wrapped < rhs.wrapped }
2728
}
2829

29-
extension Comparable where Self: ~Copyable {
30+
extension Noncopyable: Escapable where Wrapped: Escapable & ~Copyable { }
31+
32+
struct Nonescapable: ~Escapable {
33+
let wrapped: Int
34+
}
35+
36+
extension Nonescapable: Equatable { }
37+
38+
extension Nonescapable: Comparable {
39+
static func <(lhs: Self, rhs: Self) -> Bool { lhs.wrapped < rhs.wrapped }
40+
41+
}
42+
43+
extension Comparable where Self: ~Copyable & ~Escapable {
3044
func isLessThan(_ other: borrowing Self) -> Bool {
3145
self < other
3246
}
3347
}
3448

35-
func isLessOrEqual<T: Comparable & ~Copyable>(_ lhs: borrowing T, _ rhs: borrowing T) -> Bool {
49+
func isLessOrEqual<T: Comparable & ~Copyable & ~Escapable>(_ lhs: borrowing T, _ rhs: borrowing T) -> Bool {
3650
lhs <= rhs
3751
}
3852

@@ -80,6 +94,24 @@ NoncopyableComparableTests.test("comparing noncopyables") {
8094
expectTrue(array.inReverseOrder())
8195
array.swapAt(1, 2)
8296
expectFalse(array.inReverseOrder())
83-
}
97+
}
98+
99+
NoncopyableComparableTests.test("comparing nonescapables") {
100+
let a = Noncopyable<Nonescapable>(wrapped: .init(wrapped: 0))
101+
let b = Noncopyable<Nonescapable>(wrapped: .init(wrapped: 1))
102+
let c = Noncopyable<Nonescapable>(wrapped: .init(wrapped: 2))
103+
104+
expectTrue(a < b)
105+
expectTrue(c > a)
106+
expectFalse(a < a)
107+
expectFalse(a > c)
108+
expectFalse(c <= a)
109+
expectTrue(a <= a)
110+
expectTrue(a < b && b < c)
111+
112+
expectTrue(a.isLessThan(b))
113+
expectTrue(isLessOrEqual(a,b))
114+
expectFalse(b.isLessThan(a))
115+
}
84116

85117
runAllTests()

0 commit comments

Comments
 (0)