Skip to content

Commit 485dda2

Browse files
committed
[AST] Prefer parent PatternBindingDecl as owner for CustomAttr
If we have a VarDecl, use its parent PatternBindingDecl as the owner to ensure we can correctly represent pattern bindings that bind multiple vars.
1 parent 157bba1 commit 485dda2

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

include/swift/AST/Attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2322,7 +2322,7 @@ class CustomAttr final : public DeclAttribute {
23222322

23232323
/// Retrieve the Decl or DeclContext owner for the attribute.
23242324
CustomAttrOwner getOwner() const { return owner; }
2325-
void setOwner(CustomAttrOwner newOwner) { owner = newOwner; }
2325+
void setOwner(CustomAttrOwner newOwner);
23262326

23272327
ASTContext &getASTContext() const;
23282328

lib/AST/Attr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,6 +3084,16 @@ CustomAttr *CustomAttr::create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
30843084
CustomAttr(atLoc, range, type, owner, initContext, argList, implicit);
30853085
}
30863086

3087+
void CustomAttr::setOwner(CustomAttrOwner newOwner) {
3088+
// Prefer to set the parent PatternBindingDecl as the owner for a VarDecl,
3089+
// this ensures we can handle PBDs with multiple vars.
3090+
if (auto *VD = dyn_cast_or_null<VarDecl>(newOwner.getAsDecl())) {
3091+
if (auto *PBD = VD->getParentPatternBinding())
3092+
newOwner = PBD;
3093+
}
3094+
owner = newOwner;
3095+
}
3096+
30873097
std::pair<UnqualifiedIdentTypeRepr *, DeclRefTypeRepr *>
30883098
CustomAttr::destructureMacroRef() {
30893099
TypeRepr *typeRepr = getTypeRepr();

lib/AST/NameLookup.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,9 +3976,11 @@ static bool shouldPreferPropertyWrapperOverMacro(CustomAttrOwner owner) {
39763976
// if one exists. This is necessary since we don't properly support peer
39773977
// declarations in local contexts, so want to use a property wrapper if one
39783978
// exists.
3979-
if (auto *D = dyn_cast_or_null<VarDecl>(owner.getAsDecl())) {
3980-
if (D->getDeclContext()->isLocalContext())
3979+
if (auto *D = owner.getAsDecl()) {
3980+
if ((isa<VarDecl>(D) || isa<PatternBindingDecl>(D)) &&
3981+
D->getDeclContext()->isLocalContext()) {
39813982
return true;
3983+
}
39823984
}
39833985
return false;
39843986
}

0 commit comments

Comments
 (0)