Skip to content

Commit df2c112

Browse files
moiseevairspeedswift
authored andcommitted
[stdlib] 0-ary tuples should be equatable (#11017)
Add an == method for comparing two empty tuples. Resolves: SR-4172 (https://bugs.swift.org/browse/SR-4172) (cherry picked from commit e1a5a81)
1 parent 73afc0f commit df2c112

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

stdlib/public/core/Tuple.swift.gyb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,82 @@ comparableOperators = [
2323

2424
}%
2525

26+
/// Returns a Boolean value indicating whether the corresponding components of
27+
/// two tuples are equal.
28+
///
29+
/// All arity zero tuples are equal.
30+
///
31+
/// - Parameters:
32+
/// - lhs: An empty tuple.
33+
/// - rhs: An empty tuple.
34+
public func ==(lhs: (), rhs: ()) -> Bool {
35+
return true
36+
}
37+
38+
/// Returns a Boolean value indicating whether any corresponding components of
39+
/// the two tuples are not equal.
40+
///
41+
/// All arity zero tuples are equal.
42+
///
43+
/// - Parameters:
44+
/// - lhs: An empty tuple.
45+
/// - rhs: An empty tuple.
46+
public func !=(lhs: (), rhs: ()) -> Bool {
47+
return false
48+
}
49+
50+
/// Returns a Boolean value indicating whether the first tuple is ordered
51+
/// before the second in a lexicographical ordering.
52+
///
53+
/// An arity zero tuple is never strictly before another arity zero tuple in a
54+
/// lexicographical ordering.
55+
///
56+
/// - Parameters:
57+
/// - lhs: An empty tuple.
58+
/// - rhs: An empty tuple.
59+
public func <(lhs: (), rhs: ()) -> Bool {
60+
return false
61+
}
62+
63+
/// Returns a Boolean value indicating whether the first tuple is ordered
64+
/// before or the same as the second in a lexicographical ordering.
65+
///
66+
/// An arity zero tuple is always before or the same as another arity zero tuple
67+
/// in a lexicographical ordering.
68+
///
69+
/// - Parameters:
70+
/// - lhs: An empty tuple.
71+
/// - rhs: An empty tuple.
72+
public func <=(lhs: (), rhs: ()) -> Bool {
73+
return true
74+
}
75+
76+
/// Returns a Boolean value indicating whether the first tuple is ordered
77+
/// after the second in a lexicographical ordering.
78+
///
79+
/// An arity zero tuple is never strictly after another arity zero tuple in a
80+
/// lexicographical ordering.
81+
///
82+
/// - Parameters:
83+
/// - lhs: An empty tuple.
84+
/// - rhs: An empty tuple.
85+
public func >(lhs: (), rhs: ()) -> Bool {
86+
return false
87+
}
88+
89+
/// Returns a Boolean value indicating whether the first tuple is ordered
90+
/// after or the same as the second in a lexicographical ordering.
91+
///
92+
/// An arity zero tuple is always after or the same as another arity zero tuple
93+
/// in a lexicographical ordering.
94+
///
95+
/// - Parameters:
96+
/// - lhs: An empty tuple.
97+
/// - rhs: An empty tuple.
98+
public func >=(lhs: (), rhs: ()) -> Bool {
99+
return true
100+
}
101+
26102
% for arity in range(2,7):
27103
% typeParams = [chr(ord("A") + i) for i in range(arity)]
28104
% tupleT = "({})".format(",".join(typeParams))

test/IDE/complete_operators.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_14 | %FileCheck %s -check-prefix=NO_OPERATORS
4141
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_15 | %FileCheck %s -check-prefix=INFIX_15
4242
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_16 | %FileCheck %s -check-prefix=INFIX_16
43-
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_17 | %FileCheck %s -check-prefix=NO_OPERATORS
43+
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_17 | %FileCheck %s -check-prefix=VOID_OPERATORS
4444
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_18 | %FileCheck %s -check-prefix=NO_OPERATORS
4545
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_19 | %FileCheck %s -check-prefix=EMPTYCLASS_INFIX
4646
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_20 | %FileCheck %s -check-prefix=NO_OPERATORS
@@ -281,6 +281,16 @@ func testInfix16<T: P where T.T == S2>() {
281281
func testInfix17(x: Void) {
282282
x#^INFIX_17^#
283283
}
284+
285+
// VOID_OPERATORS: Begin completions
286+
// VOID_OPERATORS-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: != {#()#}[#Bool#]; name=!= ()
287+
// VOID_OPERATORS-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: == {#()#}[#Bool#]; name=== ()
288+
// VOID_OPERATORS-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: <= {#()#}[#Bool#]; name=<= ()
289+
// VOID_OPERATORS-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: >= {#()#}[#Bool#]; name=>= ()
290+
// VOID_OPERATORS-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: < {#()#}[#Bool#]; name=< ()
291+
// VOID_OPERATORS-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: > {#()#}[#Bool#]; name=> ()
292+
// VOID_OPERATORS: End completions
293+
284294
func testInfix18(x: (S2, S2) {
285295
x#^INFIX_18^#
286296
}

test/stdlib/Tuple.swift.gyb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ TupleTestSuite.test("Tuple/equality") {
5858

5959
TupleTestSuite.test("Tuple/equality/sanity-check") {
6060
// sanity check all arities
61+
62+
expectTrue(() == ())
63+
expectFalse(() != ())
64+
expectFalse(() < ())
65+
expectTrue(() <= ())
66+
expectFalse(() > ())
67+
expectTrue(() >= ())
68+
6169
% for arity in range(2, maxArity + 1):
6270
% a = str(tuple(range(1, arity + 1)))
6371
% b = "({}, 0)".format(", ".join([str(i) for i in range(1, arity)]))

0 commit comments

Comments
 (0)