Skip to content

Commit d004d24

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

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
@@ -3042,10 +3042,6 @@ class DeclAttributes {
30423042
const_cast<const DeclAttributes *>(this)->getEffectiveSendableAttr());
30433043
}
30443044

3045-
/// Try to find the custom attribute initializer within the list of
3046-
/// attributes.
3047-
CustomAttributeInitializer *findCustomAttributeInitializer() const;
3048-
30493045
public:
30503046
template <typename ATTR, bool AllowInvalid>
30513047
using AttributeKindRange =

lib/AST/Attr.cpp

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

28822882
return false;
28832883
}
2884-
2885-
CustomAttributeInitializer *
2886-
DeclAttributes::findCustomAttributeInitializer() const {
2887-
for (auto custom : getAttributes<CustomAttr>()) {
2888-
if (auto initContext = custom->getInitContext())
2889-
return initContext;
2890-
}
2891-
2892-
return nullptr;
2893-
}

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
@@ -8580,20 +8580,12 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
85808580

85818581
// Parse an initializer if present.
85828582
if (Tok.is(tok::equal)) {
8583-
// If we're not in a local context, we'll need a context to parse initializers
8584-
// into (should we have one). This happens for properties and global
8585-
// variables in libraries.
8586-
85878583
// If we have no local context to parse the initial value into, create one
85888584
// for the PBD we'll eventually create. This allows us to have reasonable
85898585
// DeclContexts for any closures that may live inside of initializers.
85908586
if (!CurDeclContext->isLocalContext() && !topLevelDecl) {
85918587
assert(!initContext && "There cannot be an init context yet");
85928588
initContext = PatternBindingInitializer::create(CurDeclContext);
8593-
8594-
if (auto attributeInit = Attributes.findCustomAttributeInitializer()) {
8595-
attributeInit->setEnclosingInitializer(initContext);
8596-
}
85978589
}
85988590

85998591
// 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)