Skip to content

synthesized comparable for enums #25696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 7, 2020
Merged
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
5 changes: 4 additions & 1 deletion include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -538,6 +538,9 @@ class ASTContext final {
ConcreteDeclRef getBuiltinInitDecl(NominalTypeDecl *decl,
KnownProtocolKind builtinProtocol,
llvm::function_ref<DeclName (ASTContext &ctx)> initName) const;

/// Retrieve the declaration of Swift.<(Int, Int) -> Bool.
FuncDecl *getLessThanIntDecl() const;

/// Retrieve the declaration of Swift.==(Int, Int) -> Bool.
FuncDecl *getEqualIntDecl() const;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2693,6 +2693,8 @@ ERROR(broken_case_iterable_requirement,none,
"CaseIterable protocol is broken: unexpected requirement", ())
ERROR(broken_raw_representable_requirement,none,
"RawRepresentable protocol is broken: unexpected requirement", ())
ERROR(broken_comparable_requirement,none,
"Comparable protocol is broken: unexpected requirement", ())
ERROR(broken_equatable_requirement,none,
"Equatable protocol is broken: unexpected requirement", ())
ERROR(broken_hashable_requirement,none,
Expand All @@ -2705,6 +2707,8 @@ ERROR(broken_int_hashable_conformance,none,
"Int type is broken: does not conform to Hashable", ())
ERROR(broken_int_integer_literal_convertible_conformance,none,
"Int type is broken: does not conform to ExpressibleByIntegerLiteral", ())
ERROR(no_less_than_overload_for_int,none,
"no overload of '<' for Int", ())
ERROR(no_equal_overload_for_int,none,
"no overload of '==' for Int", ())
ERROR(broken_coding_key_requirement,none,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ IDENTIFIER_WITH_NAME(NativeClassLayout, "_NativeClass")

// Operators
IDENTIFIER_WITH_NAME(MatchOperator, "~=")
IDENTIFIER_WITH_NAME(LessThanOperator, "<")
IDENTIFIER_WITH_NAME(EqualsOperator, "==")
IDENTIFIER_WITH_NAME(NegationOperator, "!")
IDENTIFIER_WITH_NAME(derived_enum_less_than, "__derived_enum_less_than")
IDENTIFIER_WITH_NAME(derived_enum_equals, "__derived_enum_equals")
IDENTIFIER_WITH_NAME(derived_struct_equals, "__derived_struct_equals")

Expand Down
30 changes: 20 additions & 10 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -224,6 +224,9 @@ struct ASTContext::Implementation {
#define FUNC_DECL(Name, Id) FuncDecl *Get##Name = nullptr;
#include "swift/AST/KnownDecls.def"

/// func <Int, Int) -> Bool
FuncDecl *LessThanIntDecl = nullptr;

/// func ==(Int, Int) -> Bool
FuncDecl *EqualIntDecl = nullptr;

Expand Down Expand Up @@ -1083,30 +1086,37 @@ ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,
return witness;
}

FuncDecl *ASTContext::getEqualIntDecl() const {
if (getImpl().EqualIntDecl)
return getImpl().EqualIntDecl;
static
FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op, FuncDecl *&cached) {
if (cached)
return cached;

if (!getIntDecl() || !getBoolDecl())
if (!C.getIntDecl() || !C.getBoolDecl())
return nullptr;

auto intType = getIntDecl()->getDeclaredType();
auto intType = C.getIntDecl()->getDeclaredType();
auto isIntParam = [&](AnyFunctionType::Param param) {
return (!param.isVariadic() && !param.isInOut() &&
param.getPlainType()->isEqual(intType));
};
auto boolType = getBoolDecl()->getDeclaredType();
auto decl = lookupOperatorFunc(*this, "==",
intType, [=](FunctionType *type) {
auto boolType = C.getBoolDecl()->getDeclaredType();
auto decl = lookupOperatorFunc(C, op, intType,
[=](FunctionType *type) {
// Check for the signature: (Int, Int) -> Bool
if (type->getParams().size() != 2) return false;
if (!isIntParam(type->getParams()[0]) ||
!isIntParam(type->getParams()[1])) return false;
return type->getResult()->isEqual(boolType);
});
getImpl().EqualIntDecl = decl;
cached = decl;
return decl;
}
FuncDecl *ASTContext::getLessThanIntDecl() const {
return getBinaryComparisonOperatorIntDecl(*this, "<", getImpl().LessThanIntDecl);
}
FuncDecl *ASTContext::getEqualIntDecl() const {
return getBinaryComparisonOperatorIntDecl(*this, "==", getImpl().EqualIntDecl);
}

FuncDecl *ASTContext::getHashValueForDecl() const {
if (getImpl().HashValueForDecl)
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_swift_host_library(swiftSema STATIC
DerivedConformanceCodable.cpp
DerivedConformanceCodingKey.cpp
DerivedConformanceEquatableHashable.cpp
DerivedConformanceComparable.cpp
DerivedConformanceError.cpp
DerivedConformanceRawRepresentable.cpp
DerivedConformances.cpp
Expand Down Expand Up @@ -71,4 +72,3 @@ target_link_libraries(swiftSema PRIVATE
swiftAST
swiftParse
swiftSerialization)

Loading