Skip to content

Commit a6b5824

Browse files
committed
AST: Correctly set parent DeclContext of ParamDecls in EnumElementDecls
When an EnumElementDecl is parsed, we create the parameter list before creating the EnumElementDecl itself, so we have to re-parent those ParamDecls just like we do for functions and subscripts.
1 parent f692420 commit a6b5824

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6355,13 +6355,7 @@ class EnumElementDecl : public DeclContext, public ValueDecl {
63556355
ParameterList *Params,
63566356
SourceLoc EqualsLoc,
63576357
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-
{}
6358+
DeclContext *DC);
63656359

63666360
Identifier getName() const { return getFullName().getBaseIdentifier(); }
63676361

@@ -6377,6 +6371,7 @@ class EnumElementDecl : public DeclContext, public ValueDecl {
63776371

63786372
Type getArgumentInterfaceType() const;
63796373

6374+
void setParameterList(ParameterList *params);
63806375
ParameterList *getParameterList() const { return Params; }
63816376

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

lib/AST/Decl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7070,6 +7070,18 @@ SourceRange FuncDecl::getSourceRange() const {
70707070
return StartLoc;
70717071
}
70727072

7073+
EnumElementDecl::EnumElementDecl(SourceLoc IdentifierLoc, DeclName Name,
7074+
ParameterList *Params,
7075+
SourceLoc EqualsLoc,
7076+
LiteralExpr *RawValueExpr,
7077+
DeclContext *DC)
7078+
: DeclContext(DeclContextKind::EnumElementDecl, DC),
7079+
ValueDecl(DeclKind::EnumElement, DC, Name, IdentifierLoc),
7080+
EqualsLoc(EqualsLoc),
7081+
RawValueExpr(RawValueExpr) {
7082+
setParameterList(Params);
7083+
}
7084+
70737085
SourceRange EnumElementDecl::getSourceRange() const {
70747086
if (RawValueExpr && !RawValueExpr->isImplicit())
70757087
return {getStartLoc(), RawValueExpr->getEndLoc()};
@@ -7127,6 +7139,13 @@ Type EnumElementDecl::getArgumentInterfaceType() const {
71277139
return TupleType::get(elements, ctx);
71287140
}
71297141

7142+
void EnumElementDecl::setParameterList(ParameterList *params) {
7143+
Params = params;
7144+
7145+
if (params)
7146+
params->setDeclContextOfParamDecls(this);
7147+
}
7148+
71307149
EnumCaseDecl *EnumElementDecl::getParentCase() const {
71317150
for (EnumCaseDecl *EC : getParentEnum()->getAllCases()) {
71327151
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)