Skip to content

Commit 4f1c3df

Browse files
committed
[NFC] Fix ImplementsAttr::clone()
Correct an issue with `ImplementsAttr` that would come up if you ever tried to clone an attribute that had been deserialized. No tests because there’s nothing in the compiler yet that might actually do so.
1 parent 72867f3 commit 4f1c3df

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

include/swift/AST/Attr.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,14 +1762,16 @@ class StorageRestrictionsAttr final
17621762
/// The @_implements attribute, which treats a decl as the implementation for
17631763
/// some named protocol requirement (but otherwise not-visible by that name).
17641764
class ImplementsAttr : public DeclAttribute {
1765-
TypeRepr *TyR;
1765+
/// If constructed by the \c create() variant with a TypeRepr, the TypeRepr;
1766+
/// if constructed by the \c create() variant with a DeclContext and
1767+
/// ProtocolDecl, the DeclContext.
1768+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC;
17661769
DeclName MemberName;
17671770
DeclNameLoc MemberNameLoc;
17681771

17691772
ImplementsAttr(SourceLoc atLoc, SourceRange Range,
1770-
TypeRepr *TyR,
1771-
DeclName MemberName,
1772-
DeclNameLoc MemberNameLoc);
1773+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC,
1774+
DeclName MemberName, DeclNameLoc MemberNameLoc);
17731775

17741776
public:
17751777
static ImplementsAttr *create(ASTContext &Ctx, SourceLoc atLoc,
@@ -1789,7 +1791,9 @@ class ImplementsAttr : public DeclAttribute {
17891791
/// otherwise `nullopt`. This should only be used for dumping.
17901792
std::optional<ProtocolDecl *> getCachedProtocol(DeclContext *dc) const;
17911793

1792-
TypeRepr *getProtocolTypeRepr() const { return TyR; }
1794+
TypeRepr *getProtocolTypeRepr() const {
1795+
return TyROrDC.dyn_cast<TypeRepr *>();
1796+
}
17931797

17941798
DeclName getMemberName() const { return MemberName; }
17951799
DeclNameLoc getMemberNameLoc() const { return MemberNameLoc; }
@@ -1800,8 +1804,12 @@ class ImplementsAttr : public DeclAttribute {
18001804

18011805
/// Create a copy of this attribute.
18021806
ImplementsAttr *clone(ASTContext &ctx) const {
1803-
return new (ctx) ImplementsAttr(
1804-
AtLoc, Range, TyR, getMemberName(), getMemberNameLoc());
1807+
if (auto tyR = getProtocolTypeRepr()) {
1808+
return create(ctx, AtLoc, Range, tyR, getMemberName(),
1809+
getMemberNameLoc());
1810+
}
1811+
auto dc = TyROrDC.dyn_cast<DeclContext *>();
1812+
return create(dc, getProtocol(dc), getMemberName());
18051813
}
18061814
};
18071815

lib/AST/Attr.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,11 +2674,12 @@ StorageRestrictionsAttr::create(
26742674
/*implicit=*/false);
26752675
}
26762676

2677-
ImplementsAttr::ImplementsAttr(SourceLoc atLoc, SourceRange range,
2678-
TypeRepr *TyR, DeclName MemberName,
2679-
DeclNameLoc MemberNameLoc)
2677+
ImplementsAttr::
2678+
ImplementsAttr(SourceLoc atLoc, SourceRange range,
2679+
llvm::PointerUnion<TypeRepr *, DeclContext *> TyROrDC,
2680+
DeclName MemberName, DeclNameLoc MemberNameLoc)
26802681
: DeclAttribute(DeclAttrKind::Implements, atLoc, range, /*Implicit=*/false),
2681-
TyR(TyR), MemberName(MemberName), MemberNameLoc(MemberNameLoc) {}
2682+
TyROrDC(TyROrDC), MemberName(MemberName), MemberNameLoc(MemberNameLoc) {}
26822683

26832684
ImplementsAttr *ImplementsAttr::create(ASTContext &Ctx, SourceLoc atLoc,
26842685
SourceRange range,
@@ -2695,9 +2696,8 @@ ImplementsAttr *ImplementsAttr::create(DeclContext *DC,
26952696
DeclName MemberName) {
26962697
auto &ctx = DC->getASTContext();
26972698
void *mem = ctx.Allocate(sizeof(ImplementsAttr), alignof(ImplementsAttr));
2698-
auto *attr = new (mem) ImplementsAttr(
2699-
SourceLoc(), SourceRange(), nullptr,
2700-
MemberName, DeclNameLoc());
2699+
auto *attr = new (mem) ImplementsAttr(SourceLoc(), SourceRange(), DC,
2700+
MemberName, DeclNameLoc());
27012701
ctx.evaluator.cacheOutput(ImplementsAttrProtocolRequest{attr, DC},
27022702
std::move(Proto));
27032703
return attr;

0 commit comments

Comments
 (0)