Skip to content

Commit b4465ee

Browse files
committed
Ensure that we wire up *all* custom attribute initializer contexts
Thank you again, Rintaro
1 parent a0f043d commit b4465ee

File tree

5 files changed

+16
-28
lines changed

5 files changed

+16
-28
lines changed

include/swift/AST/Attr.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,10 +3065,6 @@ class DeclAttributes {
30653065
const_cast<const DeclAttributes *>(this)->getEffectiveSendableAttr());
30663066
}
30673067

3068-
/// Try to find the custom attribute initializer within the list of
3069-
/// attributes.
3070-
CustomAttributeInitializer *findCustomAttributeInitializer() const;
3071-
30723068
public:
30733069
template <typename ATTR, bool AllowInvalid>
30743070
using AttributeKindRange =

lib/AST/Attr.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,13 +3148,3 @@ bool swift::hasAttribute(const LangOptions &langOpts,
31483148

31493149
return false;
31503150
}
3151-
3152-
CustomAttributeInitializer *
3153-
DeclAttributes::findCustomAttributeInitializer() const {
3154-
for (auto custom : getAttributes<CustomAttr>()) {
3155-
if (auto initContext = custom->getInitContext())
3156-
return initContext;
3157-
}
3158-
3159-
return nullptr;
3160-
}

lib/AST/Decl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,15 +2063,15 @@ PatternBindingDecl::create(ASTContext &Ctx, SourceLoc StaticLoc,
20632063
// only affect implicit code though.
20642064
if (!initContext && !Parent->isLocalContext()) {
20652065
initContext = PatternBindingInitializer::create(Parent);
2066-
20672066
}
20682067

2069-
if (auto firstVar = PBD->getAnchoringVarDecl(idx)) {
2070-
if (auto attributeInit =
2071-
firstVar->getAttrs().findCustomAttributeInitializer()) {
2072-
attributeInit->setEnclosingInitializer(initContext);
2068+
// Set up the custom attribute contexts for each variable in this pattern.
2069+
PBD->getPattern(idx)->forEachVariable([&](VarDecl *var) {
2070+
for (auto custom : var->getAttrs().getAttributes<CustomAttr>()) {
2071+
if (auto attributeInit = custom->getInitContext())
2072+
attributeInit->setEnclosingInitializer(initContext);
20732073
}
2074-
}
2074+
});
20752075

20762076
// We need to call setPattern to ensure the VarDecls in the pattern have
20772077
// the PatternBindingDecl set as their parent. We also need to call

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8598,20 +8598,12 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
85988598

85998599
// Parse an initializer if present.
86008600
if (Tok.is(tok::equal)) {
8601-
// If we're not in a local context, we'll need a context to parse initializers
8602-
// into (should we have one). This happens for properties and global
8603-
// variables in libraries.
8604-
86058601
// If we have no local context to parse the initial value into, create one
86068602
// for the PBD we'll eventually create. This allows us to have reasonable
86078603
// DeclContexts for any closures that may live inside of initializers.
86088604
if (!CurDeclContext->isLocalContext() && !topLevelDecl) {
86098605
assert(!initContext && "There cannot be an init context yet");
86108606
initContext = PatternBindingInitializer::create(CurDeclContext);
8611-
8612-
if (auto attributeInit = Attributes.findCustomAttributeInitializer()) {
8613-
attributeInit->setEnclosingInitializer(initContext);
8614-
}
86158607
}
86168608

86178609
// If we're using a local context (either a TopLevelCodeDecl or a

test/Macros/macro_expand_peers.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,13 @@ struct Trait {
299299

300300
@trait(Trait {})
301301
func closureInPeerMacroCrash() {}
302+
303+
@trait(Trait {})
304+
@trait(Trait {})
305+
@trait(Trait {})
306+
func closuresInPeerMacroCrash() {}
307+
308+
@trait(Trait {})
309+
@trait(Trait {})
310+
@trait(Trait {})
311+
var closuresInPeerMacroOnVariableCrash: Int = 0

0 commit comments

Comments
 (0)