Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stdlib/public/core/Comparable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
/// (`FloatingPoint.nan`) compares as neither less than, greater than, nor
/// equal to any normal floating-point value. Exceptional values need not
/// take part in the strict total order.
public protocol Comparable: Equatable, ~Copyable {
public protocol Comparable: Equatable, ~Copyable, ~Escapable {
/// Returns a Boolean value indicating whether the value of the first
/// argument is less than that of the second argument.
///
Expand Down Expand Up @@ -173,7 +173,7 @@ public protocol Comparable: Equatable, ~Copyable {
static func > (lhs: borrowing Self, rhs: borrowing Self) -> Bool
}

extension Comparable where Self: ~Copyable {
extension Comparable where Self: ~Copyable & ~Escapable {
/// Returns a Boolean value indicating whether the value of the first argument
/// is greater than that of the second argument.
///
Expand Down
4 changes: 2 additions & 2 deletions test/SILOptimizer/mandatory_performance_optimizations.sil
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bb0:
sil [no_allocation] [ossa] @deserialize_and_inline_after_devirtualize : $@convention(thin) (@in Int) -> () {
bb0(%0 : $*Int):
%1 = metatype $@thick Int.Type
%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
%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
%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
%4 = tuple()
return %4 : $()
Expand Down Expand Up @@ -124,7 +124,7 @@ bb0(%0 : $*Int, %1 : $*Int, %2 : $@thick Int.Type):

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

sil [ossa] @get_int_value : $@convention(thin) () -> Int32 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Protocol CodingKey has added inherited protocol Copyable
Protocol CodingKey has added inherited protocol Escapable
Protocol Collection has added inherited protocol Copyable
Protocol Collection has added inherited protocol Escapable
Protocol Comparable has added inherited protocol Escapable
Protocol CustomDebugStringConvertible has added inherited protocol Copyable
Protocol CustomDebugStringConvertible has added inherited protocol Escapable
Protocol CustomLeafReflectable has added inherited protocol Copyable
Expand Down Expand Up @@ -384,18 +383,18 @@ Func Equatable.==(_:_:) has generic signature change from <Self where Self : Swi
Func Equatable.==(_:_:) has parameter 0 changing from Default to Shared
Func Equatable.==(_:_:) has parameter 1 changing from Default to Shared

// Comparable: ~Copyable
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable>
Func Comparable.<(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
// Comparable: ~Copyable & ~Escapable
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable, Self : ~Escapable>
Func Comparable.<(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
Func Comparable.<(_:_:) has parameter 0 changing from Default to Shared
Func Comparable.<(_:_:) has parameter 1 changing from Default to Shared
Func Comparable.<=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
Func Comparable.<=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
Func Comparable.<=(_:_:) has parameter 0 changing from Default to Shared
Func Comparable.<=(_:_:) has parameter 1 changing from Default to Shared
Func Comparable.>(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
Func Comparable.>(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
Func Comparable.>(_:_:) has parameter 0 changing from Default to Shared
Func Comparable.>(_:_:) has parameter 1 changing from Default to Shared
Func Comparable.>=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
Func Comparable.>=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
Func Comparable.>=(_:_:) has parameter 0 changing from Default to Shared
Func Comparable.>=(_:_:) has parameter 1 changing from Default to Shared

Expand Down
19 changes: 9 additions & 10 deletions test/api-digester/stability-stdlib-abi-without-asserts.test
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ Protocol CodingKey has added inherited protocol Copyable
Protocol CodingKey has added inherited protocol Escapable
Protocol Collection has added inherited protocol Copyable
Protocol Collection has added inherited protocol Escapable
Protocol Comparable has added inherited protocol Escapable
Protocol CustomDebugStringConvertible has added inherited protocol Copyable
Protocol CustomDebugStringConvertible has added inherited protocol Escapable
Protocol CustomLeafReflectable has added inherited protocol Copyable
Expand Down Expand Up @@ -889,23 +888,23 @@ Func Equatable.==(_:_:) has generic signature change from <Self where Self : Swi
Func Equatable.==(_:_:) has parameter 0 changing from Default to Shared
Func Equatable.==(_:_:) has parameter 1 changing from Default to Shared

// Comparable: ~Copyable
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable>
Func Comparable.<(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
// Comparable: ~Copyable & ~Escapable
Protocol Comparable has generic signature change from <Self : Swift.Equatable> to <Self : Swift.Equatable, Self : ~Copyable, Self : ~Escapable>
Func Comparable.<(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
Func Comparable.<(_:_:) has parameter 0 changing from Default to Shared
Func Comparable.<(_:_:) has parameter 1 changing from Default to Shared
Func Comparable.<=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
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'
Func Comparable.<=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
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'
Func Comparable.<=(_:_:) has parameter 0 changing from Default to Shared
Func Comparable.<=(_:_:) has parameter 1 changing from Default to Shared
Func Comparable.<=(_:_:) is now with @_preInverseGenerics
Func Comparable.>(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
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'
Func Comparable.>(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
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'
Func Comparable.>(_:_:) has parameter 0 changing from Default to Shared
Func Comparable.>(_:_:) has parameter 1 changing from Default to Shared
Func Comparable.>(_:_:) is now with @_preInverseGenerics
Func Comparable.>=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable>
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'
Func Comparable.>=(_:_:) has generic signature change from <Self where Self : Swift.Comparable> to <Self where Self : Swift.Comparable, Self : ~Copyable, Self : ~Escapable>
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'
Func Comparable.>=(_:_:) has parameter 0 changing from Default to Shared
Func Comparable.>=(_:_:) has parameter 1 changing from Default to Shared
Func Comparable.>=(_:_:) is now with @_preInverseGenerics
Expand Down
46 changes: 39 additions & 7 deletions test/stdlib/NoncopyableComparable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,44 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-simple-swift
// RUN: %target-run-simple-swift(-enable-experimental-feature Lifetimes)
// REQUIRES: executable_test
// REQUIRES: swift_feature_Lifetimes

import StdlibUnittest

let NoncopyableComparableTests = TestSuite("NoncopyableComparable")

struct Noncopyable<Wrapped: ~Copyable>: ~Copyable {
struct Noncopyable<Wrapped: ~Copyable & ~Escapable>: ~Copyable, ~Escapable {
var wrapped: Wrapped
}

extension Noncopyable: Equatable where Wrapped: Equatable & ~Copyable { }
extension Noncopyable: Equatable where Wrapped: Equatable & ~Copyable & ~Escapable { }

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

extension Comparable where Self: ~Copyable {
extension Noncopyable: Escapable where Wrapped: Escapable & ~Copyable { }

struct Nonescapable: ~Escapable {
let wrapped: Int
}

extension Nonescapable: Equatable { }

extension Nonescapable: Comparable {
static func <(lhs: Self, rhs: Self) -> Bool { lhs.wrapped < rhs.wrapped }

}

extension Comparable where Self: ~Copyable & ~Escapable {
func isLessThan(_ other: borrowing Self) -> Bool {
self < other
}
}

func isLessOrEqual<T: Comparable & ~Copyable>(_ lhs: borrowing T, _ rhs: borrowing T) -> Bool {
func isLessOrEqual<T: Comparable & ~Copyable & ~Escapable>(_ lhs: borrowing T, _ rhs: borrowing T) -> Bool {
lhs <= rhs
}

Expand Down Expand Up @@ -80,6 +94,24 @@ NoncopyableComparableTests.test("comparing noncopyables") {
expectTrue(array.inReverseOrder())
array.swapAt(1, 2)
expectFalse(array.inReverseOrder())
}
}

NoncopyableComparableTests.test("comparing nonescapables") {
let a = Noncopyable<Nonescapable>(wrapped: .init(wrapped: 0))
let b = Noncopyable<Nonescapable>(wrapped: .init(wrapped: 1))
let c = Noncopyable<Nonescapable>(wrapped: .init(wrapped: 2))

expectTrue(a < b)
expectTrue(c > a)
expectFalse(a < a)
expectFalse(a > c)
expectFalse(c <= a)
expectTrue(a <= a)
expectTrue(a < b && b < c)

expectTrue(a.isLessThan(b))
expectTrue(isLessOrEqual(a,b))
expectFalse(b.isLessThan(a))
}

runAllTests()