Skip to content

Commit f5dd971

Browse files
authored
Merge pull request #27583 from slavapestov/enum-declcontext
Correctly set the parent DeclContext of ParamDecls in EnumElementDecls
2 parents 1d7d58a + 03ae7a2 commit f5dd971

File tree

6 files changed

+36
-29
lines changed

6 files changed

+36
-29
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,8 +3541,6 @@ class EnumDecl final : public NominalTypeDecl {
35413541
return SourceRange(EnumLoc, getBraces().End);
35423542
}
35433543

3544-
EnumElementDecl *getElement(Identifier Name) const;
3545-
35463544
public:
35473545
/// A range for iterating the elements of an enum.
35483546
using ElementRange = DowncastFilterRange<EnumElementDecl, DeclRange>;
@@ -6355,13 +6353,7 @@ class EnumElementDecl : public DeclContext, public ValueDecl {
63556353
ParameterList *Params,
63566354
SourceLoc EqualsLoc,
63576355
LiteralExpr *RawValueExpr,
6358-
DeclContext *DC)
6359-
: DeclContext(DeclContextKind::EnumElementDecl, DC),
6360-
ValueDecl(DeclKind::EnumElement, DC, Name, IdentifierLoc),
6361-
Params(Params),
6362-
EqualsLoc(EqualsLoc),
6363-
RawValueExpr(RawValueExpr)
6364-
{}
6356+
DeclContext *DC);
63656357

63666358
Identifier getName() const { return getFullName().getBaseIdentifier(); }
63676359

@@ -6377,6 +6369,7 @@ class EnumElementDecl : public DeclContext, public ValueDecl {
63776369

63786370
Type getArgumentInterfaceType() const;
63796371

6372+
void setParameterList(ParameterList *params);
63806373
ParameterList *getParameterList() const { return Params; }
63816374

63826375
/// Retrieves a fully typechecked raw value expression associated

lib/AST/Decl.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4182,14 +4182,6 @@ EnumCaseDecl *EnumCaseDecl::create(SourceLoc CaseLoc,
41824182
return ::new (buf) EnumCaseDecl(CaseLoc, Elements, DC);
41834183
}
41844184

4185-
EnumElementDecl *EnumDecl::getElement(Identifier Name) const {
4186-
// FIXME: Linear search is not great for large enum decls.
4187-
for (EnumElementDecl *Elt : getAllElements())
4188-
if (Elt->getName() == Name)
4189-
return Elt;
4190-
return nullptr;
4191-
}
4192-
41934185
bool EnumDecl::hasPotentiallyUnavailableCaseValue() const {
41944186
switch (static_cast<AssociatedValueCheck>(Bits.EnumDecl.HasAssociatedValues)) {
41954187
case AssociatedValueCheck::Unchecked:
@@ -7070,6 +7062,18 @@ SourceRange FuncDecl::getSourceRange() const {
70707062
return StartLoc;
70717063
}
70727064

7065+
EnumElementDecl::EnumElementDecl(SourceLoc IdentifierLoc, DeclName Name,
7066+
ParameterList *Params,
7067+
SourceLoc EqualsLoc,
7068+
LiteralExpr *RawValueExpr,
7069+
DeclContext *DC)
7070+
: DeclContext(DeclContextKind::EnumElementDecl, DC),
7071+
ValueDecl(DeclKind::EnumElement, DC, Name, IdentifierLoc),
7072+
EqualsLoc(EqualsLoc),
7073+
RawValueExpr(RawValueExpr) {
7074+
setParameterList(Params);
7075+
}
7076+
70737077
SourceRange EnumElementDecl::getSourceRange() const {
70747078
if (RawValueExpr && !RawValueExpr->isImplicit())
70757079
return {getStartLoc(), RawValueExpr->getEndLoc()};
@@ -7127,6 +7131,13 @@ Type EnumElementDecl::getArgumentInterfaceType() const {
71277131
return TupleType::get(elements, ctx);
71287132
}
71297133

7134+
void EnumElementDecl::setParameterList(ParameterList *params) {
7135+
Params = params;
7136+
7137+
if (params)
7138+
params->setDeclContextOfParamDecls(this);
7139+
}
7140+
71307141
EnumCaseDecl *EnumElementDecl::getParentCase() const {
71317142
for (EnumCaseDecl *EC : getParentEnum()->getAllCases()) {
71327143
ArrayRef<EnumElementDecl *> CaseElements = EC->getElements();

lib/AST/UnqualifiedLookup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,8 @@ void UnqualifiedLookupFactory::lookupNamesIntroducedByMiscContext(
778778
assert(isa<TopLevelCodeDecl>(dc) ||
779779
isa<Initializer>(dc) ||
780780
isa<TypeAliasDecl>(dc) ||
781-
isa<SubscriptDecl>(dc));
781+
isa<SubscriptDecl>(dc) ||
782+
isa<EnumElementDecl>(dc));
782783
finishLookingInContext(
783784
AddGenericParameters::Yes,
784785
dc,

lib/Serialization/Deserialization.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,8 @@ DeclContext *ModuleFile::getDeclContext(DeclContextID DCID) {
18741874
return AFD;
18751875
if (auto SD = dyn_cast<SubscriptDecl>(D))
18761876
return SD;
1877+
if (auto EED = dyn_cast<EnumElementDecl>(D))
1878+
return EED;
18771879

18781880
llvm_unreachable("Unknown Decl : DeclContext kind");
18791881
}
@@ -3554,24 +3556,24 @@ class swift::DeclDeserializer {
35543556
}
35553557
}
35563558

3557-
// Read payload parameter list, if it exists.
3558-
ParameterList *paramList = nullptr;
3559-
if (hasPayload) {
3560-
paramList = MF.readParameterList();
3561-
}
3562-
35633559
DeclContext *DC = MF.getDeclContext(contextID);
35643560
if (declOrOffset.isComplete())
35653561
return declOrOffset;
35663562

35673563
auto elem = MF.createDecl<EnumElementDecl>(SourceLoc(),
35683564
name,
3569-
paramList,
3565+
nullptr,
35703566
SourceLoc(),
35713567
nullptr,
35723568
DC);
35733569
declOrOffset = elem;
35743570

3571+
// Read payload parameter list, if it exists.
3572+
if (hasPayload) {
3573+
auto *paramList = MF.readParameterList();
3574+
elem->setParameterList(paramList);
3575+
}
3576+
35753577
// Deserialize the literal raw value, if any.
35763578
switch ((EnumElementRawValueKind)rawValueKindID) {
35773579
case EnumElementRawValueKind::None:

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ static ASTContext &getContext(ModuleOrSourceFile DC) {
439439

440440
static bool shouldSerializeAsLocalContext(const DeclContext *DC) {
441441
return DC->isLocalContext() && !isa<AbstractFunctionDecl>(DC) &&
442-
!isa<SubscriptDecl>(DC);
442+
!isa<SubscriptDecl>(DC) && !isa<EnumElementDecl>(DC);
443443
}
444444

445445
namespace {

test/IDE/comment_attach.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ func unterminatedBlockDocComment() {}
293293
// CHECK-NEXT: comment_attach.swift:166:6: Enum/decl_enum_1 RawComment=[/// decl_enum_1 Aaa.\n]
294294
// CHECK-NEXT: comment_attach.swift:168:8: EnumElement/decl_enum_1.Case1 RawComment=[/// Case1 Aaa.\n]
295295
// CHECK-NEXT: comment_attach.swift:171:8: EnumElement/decl_enum_1.Case2 RawComment=[/// Case2 Aaa.\n]
296-
// CHECK-NEXT: Param/decl_enum_1.<anonymous> RawComment=none BriefComment=none DocCommentAsXML=none
296+
// CHECK-NEXT: Param/<anonymous> RawComment=none BriefComment=none DocCommentAsXML=none
297297
// CHECK-NEXT: comment_attach.swift:174:8: EnumElement/decl_enum_1.Case3 RawComment=[/// Case3 Aaa.\n]
298-
// CHECK-NEXT: Param/decl_enum_1.<anonymous> RawComment=none BriefComment=none DocCommentAsXML=none
299-
// CHECK-NEXT: Param/decl_enum_1.<anonymous> RawComment=none BriefComment=none DocCommentAsXML=none
298+
// CHECK-NEXT: Param/<anonymous> RawComment=none BriefComment=none DocCommentAsXML=none
299+
// CHECK-NEXT: Param/<anonymous> RawComment=none BriefComment=none DocCommentAsXML=none
300300
// CHECK-NEXT: comment_attach.swift:177:8: EnumElement/decl_enum_1.Case4 RawComment=[/// Case4 Case5 Aaa.\n]
301301
// CHECK-NEXT: comment_attach.swift:177:15: EnumElement/decl_enum_1.Case5 RawComment=[/// Case4 Case5 Aaa.\n]
302302
// CHECK-NEXT: comment_attach.swift:181:7: Class/decl_class_1 RawComment=[/// decl_class_1 Aaa.\n]

0 commit comments

Comments
 (0)