Skip to content

Commit e306c11

Browse files
committed
[Macros] Ensure that we visit *all* members when emitting memberwise initializer
Fixes rdar://111122261.
1 parent 814e520 commit e306c11

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5662,7 +5662,7 @@ static bool shouldEmitCategory(IRGenModule &IGM, ExtensionDecl *ext) {
56625662
return true;
56635663
}
56645664

5665-
for (auto member : ext->getMembers()) {
5665+
for (auto member : ext->getAllMembers()) {
56665666
if (auto func = dyn_cast<FuncDecl>(member)) {
56675667
if (requiresObjCMethodDescriptor(func))
56685668
return true;

lib/SILGen/SILGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ static bool requiresIVarInitialization(SILGenModule &SGM, ClassDecl *cd) {
15091509
if (!cd->requiresStoredPropertyInits())
15101510
return false;
15111511

1512-
for (Decl *member : cd->getImplementationContext()->getMembers()) {
1512+
for (Decl *member : cd->getImplementationContext()->getAllMembers()) {
15131513
auto pbd = dyn_cast<PatternBindingDecl>(member);
15141514
if (!pbd) continue;
15151515

@@ -1522,7 +1522,7 @@ static bool requiresIVarInitialization(SILGenModule &SGM, ClassDecl *cd) {
15221522
}
15231523

15241524
bool SILGenModule::hasNonTrivialIVars(ClassDecl *cd) {
1525-
for (Decl *member : cd->getImplementationContext()->getMembers()) {
1525+
for (Decl *member : cd->getImplementationContext()->getAllMembers()) {
15261526
auto *vd = dyn_cast<VarDecl>(member);
15271527
if (!vd || !vd->hasStorage()) continue;
15281528

lib/SILGen/SILGenConstructor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ void SILGenFunction::emitMemberInitializers(DeclContext *dc,
14691469
NominalTypeDecl *nominal) {
14701470
auto subs = getSubstitutionsForPropertyInitializer(dc, nominal);
14711471

1472-
for (auto member : nominal->getImplementationContext()->getMembers()) {
1472+
for (auto member : nominal->getImplementationContext()->getAllMembers()) {
14731473
// Find instance pattern binding declarations that have initializers.
14741474
if (auto pbd = dyn_cast<PatternBindingDecl>(member)) {
14751475
if (pbd->isStatic()) continue;

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,21 @@ extension SelfAlwaysEqualOperator: MemberMacro {
16041604
}
16051605
}
16061606

1607+
public struct AddPeerStoredPropertyMacro: PeerMacro, Sendable {
1608+
public static func expansion(
1609+
of attribute: AttributeSyntax,
1610+
providingPeersOf declaration: some DeclSyntaxProtocol,
1611+
in context: some MacroExpansionContext
1612+
) throws -> [DeclSyntax] {
1613+
return [
1614+
"""
1615+
1616+
private var _foo: Int = 100
1617+
"""
1618+
]
1619+
}
1620+
}
1621+
16071622
public struct InitializableMacro: ConformanceMacro, MemberMacro {
16081623
public static func expansion(
16091624
of node: AttributeSyntax,

test/Macros/macro_expand_peers.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,19 @@ func testVarPeer() {
213213
_ = value
214214
_ = Date().value
215215
}
216+
217+
// Stored properties added via peer macros.
218+
@attached(peer, names: named(_foo))
219+
macro AddPeerStoredProperty() =
220+
#externalMacro(module: "MacroDefinition", type: "AddPeerStoredPropertyMacro")
221+
222+
struct SomeStructWithPeerProperties {
223+
@AddPeerStoredProperty
224+
225+
func foo() {}
226+
}
227+
228+
func testStructWithPeers() {
229+
let x = SomeStructWithPeerProperties()
230+
print(x)
231+
}

0 commit comments

Comments
 (0)