Skip to content

Commit a67ba32

Browse files
authored
Merge pull request swiftlang#13944 from davezarzycki/nfc_find_optionals_elems_via_sema
2 parents 081046f + 6d670c8 commit a67ba32

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

include/swift/AST/Decl.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,6 +3079,11 @@ class EnumDecl final : public NominalTypeDecl {
30793079
return result;
30803080
}
30813081

3082+
/// If this enum has a unique element, return it. A unique element can
3083+
/// either hold a value or not, and the existence of one unique element does
3084+
/// not imply the existence or non-existence of the opposite unique element.
3085+
EnumElementDecl *getUniqueElement(bool hasValue) const;
3086+
30823087
/// Return a range that iterates over all the cases of an enum.
30833088
CaseRange getAllCases() const {
30843089
return CaseRange(getMembers());
@@ -6522,6 +6527,20 @@ inline DeclContext *DeclContext::castDeclToDeclContext(const Decl *D) {
65226527
}
65236528
}
65246529

6530+
inline EnumElementDecl *EnumDecl::getUniqueElement(bool hasValue) const {
6531+
EnumElementDecl *result = nullptr;
6532+
bool found = false;
6533+
for (auto elt : getAllElements()) {
6534+
if (elt->hasAssociatedValues() == hasValue) {
6535+
if (found)
6536+
return nullptr;
6537+
found = true;
6538+
result = elt;
6539+
}
6540+
}
6541+
return result;
6542+
}
6543+
65256544
} // end namespace swift
65266545

65276546
#endif

include/swift/AST/KnownIdentifiers.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ IDENTIFIER(makeIterator)
7272
IDENTIFIER(Iterator)
7373
IDENTIFIER(load)
7474
IDENTIFIER(next)
75-
IDENTIFIER(none)
7675
IDENTIFIER_(nsErrorDomain)
7776
IDENTIFIER(objectAtIndexedSubscript)
7877
IDENTIFIER(objectForKeyedSubscript)
@@ -89,7 +88,6 @@ IDENTIFIER(self)
8988
IDENTIFIER(Self)
9089
IDENTIFIER(setObject)
9190
IDENTIFIER(simd)
92-
IDENTIFIER(some)
9391
IDENTIFIER(storage)
9492
IDENTIFIER(stringValue)
9593
IDENTIFIER(super)

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,6 @@ EnumDecl *ASTContext::getOptionalDecl(OptionalTypeKind kind) const {
651651
llvm_unreachable("Unhandled OptionalTypeKind in switch.");
652652
}
653653

654-
static EnumElementDecl *findEnumElement(EnumDecl *e, Identifier name) {
655-
for (auto elt : e->getAllElements()) {
656-
if (elt->getName() == name)
657-
return elt;
658-
}
659-
return nullptr;
660-
}
661-
662654
EnumElementDecl *ASTContext::getOptionalSomeDecl(OptionalTypeKind kind) const {
663655
switch (kind) {
664656
case OTK_Optional:
@@ -685,27 +677,27 @@ EnumElementDecl *ASTContext::getOptionalNoneDecl(OptionalTypeKind kind) const {
685677

686678
EnumElementDecl *ASTContext::getOptionalSomeDecl() const {
687679
if (!Impl.OptionalSomeDecl)
688-
Impl.OptionalSomeDecl = findEnumElement(getOptionalDecl(), Id_some);
680+
Impl.OptionalSomeDecl = getOptionalDecl()->getUniqueElement(/*hasVal*/true);
689681
return Impl.OptionalSomeDecl;
690682
}
691683

692684
EnumElementDecl *ASTContext::getOptionalNoneDecl() const {
693685
if (!Impl.OptionalNoneDecl)
694-
Impl.OptionalNoneDecl = findEnumElement(getOptionalDecl(), Id_none);
686+
Impl.OptionalNoneDecl =getOptionalDecl()->getUniqueElement(/*hasVal*/false);
695687
return Impl.OptionalNoneDecl;
696688
}
697689

698690
EnumElementDecl *ASTContext::getImplicitlyUnwrappedOptionalSomeDecl() const {
699691
if (!Impl.ImplicitlyUnwrappedOptionalSomeDecl)
700692
Impl.ImplicitlyUnwrappedOptionalSomeDecl =
701-
findEnumElement(getImplicitlyUnwrappedOptionalDecl(), Id_some);
693+
getImplicitlyUnwrappedOptionalDecl()->getUniqueElement(/*hasVal*/true);
702694
return Impl.ImplicitlyUnwrappedOptionalSomeDecl;
703695
}
704696

705697
EnumElementDecl *ASTContext::getImplicitlyUnwrappedOptionalNoneDecl() const {
706698
if (!Impl.ImplicitlyUnwrappedOptionalNoneDecl)
707699
Impl.ImplicitlyUnwrappedOptionalNoneDecl =
708-
findEnumElement(getImplicitlyUnwrappedOptionalDecl(), Id_none);
700+
getImplicitlyUnwrappedOptionalDecl()->getUniqueElement(/*hasVal*/false);
709701
return Impl.ImplicitlyUnwrappedOptionalNoneDecl;
710702
}
711703

lib/Sema/TypeCheckPattern.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,10 +1231,11 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
12311231
if (numExtraOptionals > 0) {
12321232
Pattern *sub = IP;
12331233
for (int i = 0; i < numExtraOptionals; ++i) {
1234+
auto some = Context.getOptionalDecl()->getUniqueElement(/*hasVal*/true);
12341235
sub = new (Context) EnumElementPattern(TypeLoc(),
12351236
IP->getStartLoc(),
12361237
IP->getEndLoc(),
1237-
Context.Id_some,
1238+
some->getName(),
12381239
nullptr, sub,
12391240
/*Implicit=*/true);
12401241
}

0 commit comments

Comments
 (0)