Skip to content

Commit 1ed846d

Browse files
authored
Merge pull request swiftlang#25696 from kelvin13/comparable-enums
synthesized comparable for enums
2 parents b469389 + 19a8759 commit 1ed846d

14 files changed

+898
-309
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -538,6 +538,9 @@ class ASTContext final {
538538
ConcreteDeclRef getBuiltinInitDecl(NominalTypeDecl *decl,
539539
KnownProtocolKind builtinProtocol,
540540
llvm::function_ref<DeclName (ASTContext &ctx)> initName) const;
541+
542+
/// Retrieve the declaration of Swift.<(Int, Int) -> Bool.
543+
FuncDecl *getLessThanIntDecl() const;
541544

542545
/// Retrieve the declaration of Swift.==(Int, Int) -> Bool.
543546
FuncDecl *getEqualIntDecl() const;

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,6 +2697,8 @@ ERROR(broken_case_iterable_requirement,none,
26972697
"CaseIterable protocol is broken: unexpected requirement", ())
26982698
ERROR(broken_raw_representable_requirement,none,
26992699
"RawRepresentable protocol is broken: unexpected requirement", ())
2700+
ERROR(broken_comparable_requirement,none,
2701+
"Comparable protocol is broken: unexpected requirement", ())
27002702
ERROR(broken_equatable_requirement,none,
27012703
"Equatable protocol is broken: unexpected requirement", ())
27022704
ERROR(broken_hashable_requirement,none,
@@ -2709,6 +2711,8 @@ ERROR(broken_int_hashable_conformance,none,
27092711
"Int type is broken: does not conform to Hashable", ())
27102712
ERROR(broken_int_integer_literal_convertible_conformance,none,
27112713
"Int type is broken: does not conform to ExpressibleByIntegerLiteral", ())
2714+
ERROR(no_less_than_overload_for_int,none,
2715+
"no overload of '<' for Int", ())
27122716
ERROR(no_equal_overload_for_int,none,
27132717
"no overload of '==' for Int", ())
27142718
ERROR(broken_coding_key_requirement,none,

include/swift/AST/KnownIdentifiers.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ IDENTIFIER_WITH_NAME(NativeClassLayout, "_NativeClass")
142142

143143
// Operators
144144
IDENTIFIER_WITH_NAME(MatchOperator, "~=")
145+
IDENTIFIER_WITH_NAME(LessThanOperator, "<")
145146
IDENTIFIER_WITH_NAME(EqualsOperator, "==")
146147
IDENTIFIER_WITH_NAME(NegationOperator, "!")
148+
IDENTIFIER_WITH_NAME(derived_enum_less_than, "__derived_enum_less_than")
147149
IDENTIFIER_WITH_NAME(derived_enum_equals, "__derived_enum_equals")
148150
IDENTIFIER_WITH_NAME(derived_struct_equals, "__derived_struct_equals")
149151

lib/AST/ASTContext.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -224,6 +224,9 @@ struct ASTContext::Implementation {
224224
#define FUNC_DECL(Name, Id) FuncDecl *Get##Name = nullptr;
225225
#include "swift/AST/KnownDecls.def"
226226

227+
/// func <Int, Int) -> Bool
228+
FuncDecl *LessThanIntDecl = nullptr;
229+
227230
/// func ==(Int, Int) -> Bool
228231
FuncDecl *EqualIntDecl = nullptr;
229232

@@ -1083,30 +1086,37 @@ ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,
10831086
return witness;
10841087
}
10851088

1086-
FuncDecl *ASTContext::getEqualIntDecl() const {
1087-
if (getImpl().EqualIntDecl)
1088-
return getImpl().EqualIntDecl;
1089+
static
1090+
FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op, FuncDecl *&cached) {
1091+
if (cached)
1092+
return cached;
10891093

1090-
if (!getIntDecl() || !getBoolDecl())
1094+
if (!C.getIntDecl() || !C.getBoolDecl())
10911095
return nullptr;
10921096

1093-
auto intType = getIntDecl()->getDeclaredType();
1097+
auto intType = C.getIntDecl()->getDeclaredType();
10941098
auto isIntParam = [&](AnyFunctionType::Param param) {
10951099
return (!param.isVariadic() && !param.isInOut() &&
10961100
param.getPlainType()->isEqual(intType));
10971101
};
1098-
auto boolType = getBoolDecl()->getDeclaredType();
1099-
auto decl = lookupOperatorFunc(*this, "==",
1100-
intType, [=](FunctionType *type) {
1102+
auto boolType = C.getBoolDecl()->getDeclaredType();
1103+
auto decl = lookupOperatorFunc(C, op, intType,
1104+
[=](FunctionType *type) {
11011105
// Check for the signature: (Int, Int) -> Bool
11021106
if (type->getParams().size() != 2) return false;
11031107
if (!isIntParam(type->getParams()[0]) ||
11041108
!isIntParam(type->getParams()[1])) return false;
11051109
return type->getResult()->isEqual(boolType);
11061110
});
1107-
getImpl().EqualIntDecl = decl;
1111+
cached = decl;
11081112
return decl;
11091113
}
1114+
FuncDecl *ASTContext::getLessThanIntDecl() const {
1115+
return getBinaryComparisonOperatorIntDecl(*this, "<", getImpl().LessThanIntDecl);
1116+
}
1117+
FuncDecl *ASTContext::getEqualIntDecl() const {
1118+
return getBinaryComparisonOperatorIntDecl(*this, "==", getImpl().EqualIntDecl);
1119+
}
11101120

11111121
FuncDecl *ASTContext::getHashValueForDecl() const {
11121122
if (getImpl().HashValueForDecl)

lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_swift_host_library(swiftSema STATIC
2222
DerivedConformanceCodable.cpp
2323
DerivedConformanceCodingKey.cpp
2424
DerivedConformanceEquatableHashable.cpp
25+
DerivedConformanceComparable.cpp
2526
DerivedConformanceError.cpp
2627
DerivedConformanceRawRepresentable.cpp
2728
DerivedConformances.cpp
@@ -71,4 +72,3 @@ target_link_libraries(swiftSema PRIVATE
7172
swiftAST
7273
swiftParse
7374
swiftSerialization)
74-

0 commit comments

Comments
 (0)