Skip to content

Commit ce53a65

Browse files
authored
Merge pull request #8354 from anayini/bugfix/SR-4172
[stdlib] 0-ary tuples should be equatable
2 parents 846617f + a3e94cc commit ce53a65

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_12 | %FileCheck %s -check-prefix=INFIX_12
3939
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_13 | %FileCheck %s -check-prefix=NO_OPERATORS
4040
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_14 | %FileCheck %s -check-prefix=NO_OPERATORS
41+
4142
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_15 | %FileCheck %s -check-prefix=INFIX_15
4243
// 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
44+
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_17 | %FileCheck %s -check-prefix=VOID_OPERATORS
4445
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_18 | %FileCheck %s -check-prefix=NO_OPERATORS
4546
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_19 | %FileCheck %s -check-prefix=EMPTYCLASS_INFIX
4647
// RUN: %target-swift-ide-test -code-completion -source-filename=%s -code-completion-token=INFIX_20 | %FileCheck %s -check-prefix=NO_OPERATORS
@@ -243,6 +244,7 @@ func testInfix10<T: P where T.T: Fooable>(x: T) {
243244
func testInfix11() {
244245
S2#^INFIX_11^#
245246
}
247+
246248
// INFIX_11: Begin completions, 1 items
247249
// INFIX_11-DAG: Decl[Constructor]/CurrNominal: ()[#S2#]; name=()
248250
// INFIX_11: End completions
@@ -274,13 +276,24 @@ func testInfix15<T: P where T.T == S2>() {
274276
func testInfix16<T: P where T.T == S2>() {
275277
T.foo#^INFIX_16^#
276278
}
279+
277280
// INFIX_16: Begin completions, 1 items
278281
// INFIX_16-NEXT: Pattern/ExprSpecific: ({#(self): T#})[#() -> S2#]; name=(self: T)
279282
// INFIX_16: End completions
280283

281284
func testInfix17(x: Void) {
282285
x#^INFIX_17^#
283286
}
287+
288+
// VOID_OPERATORS: Begin completions
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-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: >= {#()#}[#Bool#]; name=>= ()
293+
// VOID_OPERATORS-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: < {#()#}[#Bool#]; name=< ()
294+
// VOID_OPERATORS-DAG: Decl[InfixOperatorFunction]/OtherModule[Swift]: > {#()#}[#Bool#]; name=> ()
295+
// VOID_OPERATORS: End completions
296+
284297
func testInfix18(x: (S2, S2) {
285298
x#^INFIX_18^#
286299
}

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)