Skip to content

Commit 1715ebc

Browse files
committed
address more reviewed issues
1 parent 2701012 commit 1715ebc

File tree

6 files changed

+33
-49
lines changed

6 files changed

+33
-49
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 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

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 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 - 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

lib/Sema/DerivedConformanceComparable.cpp

Lines changed: 25 additions & 41 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 - 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
@@ -31,7 +31,6 @@
3131

3232
using namespace swift;
3333

34-
// how does this code ever even get invoked? you can’t compare uninhabited enums...
3534
static std::pair<BraceStmt *, bool>
3635
deriveBodyComparable_enum_uninhabited_lt(AbstractFunctionDecl *ltDecl, void *) {
3736
auto parentDC = ltDecl->getDeclContext();
@@ -42,26 +41,9 @@ deriveBodyComparable_enum_uninhabited_lt(AbstractFunctionDecl *ltDecl, void *) {
4241
auto bParam = args->get(1);
4342

4443
assert(!cast<EnumDecl>(aParam->getType()->getAnyNominal())->hasCases());
44+
assert(!cast<EnumDecl>(bParam->getType()->getAnyNominal())->hasCases());
4545

46-
SmallVector<ASTNode, 1> statements;
47-
SmallVector<ASTNode, 0> cases;
48-
49-
// switch (a, b) { }
50-
auto aRef = new (C) DeclRefExpr(aParam, DeclNameLoc(), /*implicit*/ true,
51-
AccessSemantics::Ordinary,
52-
aParam->getType());
53-
auto bRef = new (C) DeclRefExpr(bParam, DeclNameLoc(), /*implicit*/ true,
54-
AccessSemantics::Ordinary,
55-
bParam->getType());
56-
TupleTypeElt abTupleElts[2] = { aParam->getType(), bParam->getType() };
57-
auto abExpr = TupleExpr::create(C, SourceLoc(), {aRef, bRef}, {}, {},
58-
SourceLoc(), /*HasTrailingClosure*/ false,
59-
/*implicit*/ true,
60-
TupleType::get(abTupleElts, C));
61-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), abExpr,
62-
SourceLoc(), cases, SourceLoc(), C);
63-
statements.push_back(switchStmt);
64-
46+
SmallVector<ASTNode, 0> statements;
6547
auto body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc());
6648
return { body, /*isTypeChecked=*/true };
6749
}
@@ -143,7 +125,7 @@ deriveBodyComparable_enum_hasAssociatedValues_lt(AbstractFunctionDecl *ltDecl, v
143125

144126
SmallVector<ASTNode, 6> statements;
145127
SmallVector<ASTNode, 4> cases;
146-
unsigned elementCount = 0;
128+
unsigned elementCount = 0; // need this as `getAllElements` returns a generator
147129

148130
// For each enum element, generate a case statement matching a pair containing
149131
// the same case, binding variables for the left- and right-hand associated
@@ -333,9 +315,10 @@ deriveComparable_lt(
333315
return comparableDecl;
334316
}
335317

318+
// for now, only enums can synthesize `Comparable`, so this function can take
319+
// an `EnumDecl` instead of a `NominalTypeDecl`
336320
bool
337-
DerivedConformance::canDeriveComparable(DeclContext *context, NominalTypeDecl *declaration) {
338-
auto enumeration = dyn_cast<EnumDecl>(declaration);
321+
DerivedConformance::canDeriveComparable(DeclContext *context, EnumDecl *enumeration) {
339322
// The type must be an enum.
340323
if (!enumeration) {
341324
return false;
@@ -349,26 +332,27 @@ DerivedConformance::canDeriveComparable(DeclContext *context, NominalTypeDecl *d
349332
}
350333

351334
ValueDecl *DerivedConformance::deriveComparable(ValueDecl *requirement) {
352-
if (checkAndDiagnoseDisallowedContext(requirement))
335+
if (checkAndDiagnoseDisallowedContext(requirement)) {
353336
return nullptr;
337+
}
338+
if (requirement->getBaseName() != "<") {
339+
requirement->diagnose(diag::broken_comparable_requirement);
340+
return nullptr;
341+
}
342+
354343
// Build the necessary decl.
355-
if (requirement->getBaseName() == "<") {
356-
if (EnumDecl const *const enumeration = dyn_cast<EnumDecl>(this->Nominal)) {
357-
std::pair<BraceStmt *, bool> (*synthesizer)(AbstractFunctionDecl *, void *);
358-
if (enumeration->hasCases()) {
359-
if (enumeration->hasOnlyCasesWithoutAssociatedValues()) {
360-
synthesizer = &deriveBodyComparable_enum_noAssociatedValues_lt;
361-
} else {
362-
synthesizer = &deriveBodyComparable_enum_hasAssociatedValues_lt;
363-
}
364-
} else {
365-
synthesizer = &deriveBodyComparable_enum_uninhabited_lt;
366-
}
367-
return deriveComparable_lt(*this, synthesizer);
344+
auto enumeration = dyn_cast<EnumDecl>(this->Nominal);
345+
assert(enumeration);
346+
347+
std::pair<BraceStmt *, bool> (*synthesizer)(AbstractFunctionDecl *, void *);
348+
if (enumeration->hasCases()) {
349+
if (enumeration->hasOnlyCasesWithoutAssociatedValues()) {
350+
synthesizer = &deriveBodyComparable_enum_noAssociatedValues_lt;
368351
} else {
369-
llvm_unreachable("todo");
352+
synthesizer = &deriveBodyComparable_enum_hasAssociatedValues_lt;
370353
}
354+
} else {
355+
synthesizer = &deriveBodyComparable_enum_uninhabited_lt;
371356
}
372-
requirement->diagnose(diag::broken_comparable_requirement);
373-
return nullptr;
357+
return deriveComparable_lt(*this, synthesizer);
374358
}

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 1 addition & 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

lib/Sema/DerivedConformances.cpp

Lines changed: 3 additions & 3 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 - 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
@@ -80,7 +80,7 @@ bool DerivedConformance::derivesProtocolConformance(DeclContext *DC,
8080

8181
case KnownProtocolKind::Comparable:
8282
return !enumDecl->hasPotentiallyUnavailableCaseValue()
83-
&& canDeriveComparable(DC, Nominal); // why do y’all pass wide `NominalTypeDecl*` value when u could pass downcast `EnumDecl*` value?
83+
&& canDeriveComparable(DC, enumDecl);
8484

8585
// "Simple" enums without availability attributes can explicitly derive
8686
// a CaseIterable conformance.
@@ -405,7 +405,7 @@ GuardStmt *DerivedConformance::returnIfNotEqualGuard(ASTContext &C,
405405
SmallVector<ASTNode, 1> statements;
406406

407407
auto returnStmt = new (C) ReturnStmt(SourceLoc(), guardReturnValue);
408-
statements.emplace_back(ASTNode(returnStmt));
408+
statements.push_back(returnStmt);
409409

410410
// Next, generate the condition being checked.
411411
// lhs == rhs

lib/Sema/DerivedConformances.h

Lines changed: 2 additions & 2 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 - 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
@@ -132,7 +132,7 @@ class DerivedConformance {
132132
/// This is implemented for enums without associated or raw values.
133133
///
134134
/// \returns True if the requirement can be derived.
135-
static bool canDeriveComparable(DeclContext *DC, NominalTypeDecl *type);
135+
static bool canDeriveComparable(DeclContext *DC, EnumDecl *enumeration);
136136

137137
/// Derive an Equatable requirement for a type.
138138
///

0 commit comments

Comments
 (0)