Skip to content

Commit 5e4118a

Browse files
committed
Allow expansion of member macros on extensions and make those names visible.
Ensure that name lookup triggers the expansion of member macros on extensions, so we can find those names. And fix a bug that caused expansion of member macros to crash.
1 parent 467f8f5 commit 5e4118a

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

lib/AST/NameLookup.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,11 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
21452145
(void)evaluateOrDefault(ctx.evaluator,
21462146
ExpandSynthesizedMemberMacroRequest{current},
21472147
false);
2148+
for (auto ext : current->getExtensions()) {
2149+
(void)evaluateOrDefault(ctx.evaluator,
2150+
ExpandSynthesizedMemberMacroRequest{ext},
2151+
false);
2152+
}
21482153

21492154
// Look for results within the current nominal type and its extensions.
21502155
bool currentIsProtocol = isa<ProtocolDecl>(current);

lib/Sema/TypeCheckMacros.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
12581258
if (auto nominal = dyn_cast<NominalTypeDecl>(attachedTo)) {
12591259
rightBraceLoc = nominal->getBraces().End;
12601260
} else {
1261-
auto ext = cast<ExtensionDecl>(parentDecl);
1261+
auto ext = cast<ExtensionDecl>(attachedTo);
12621262
rightBraceLoc = ext->getBraces().End;
12631263
}
12641264

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,37 @@ public struct AddMembers: MemberMacro {
554554
}
555555
}
556556

557+
public struct AddExtMembers: MemberMacro {
558+
public static func expansion(
559+
of node: AttributeSyntax,
560+
providingMembersOf decl: some DeclGroupSyntax,
561+
in context: some MacroExpansionContext
562+
) throws -> [DeclSyntax] {
563+
let uniqueClassName = context.createUniqueName("uniqueClass")
564+
565+
let instanceMethod: DeclSyntax =
566+
"""
567+
func extInstanceMethod() {}
568+
"""
569+
570+
let staticMethod: DeclSyntax =
571+
"""
572+
static func extStaticMethod() {}
573+
"""
574+
575+
let classDecl: DeclSyntax =
576+
"""
577+
class \(uniqueClassName) { }
578+
"""
579+
580+
return [
581+
instanceMethod,
582+
staticMethod,
583+
classDecl,
584+
]
585+
}
586+
}
587+
557588
public struct AddArbitraryMembers: MemberMacro {
558589
public static func expansion(
559590
of node: AttributeSyntax,

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,25 @@ struct S {
2323
}
2424
}
2525

26+
@attached(
27+
member,
28+
names: named(extInstanceMethod), named(extStaticMethod)
29+
)
30+
macro addExtMembers() = #externalMacro(module: "MacroDefinition", type: "AddExtMembers")
31+
32+
@addExtMembers
33+
extension S { }
34+
2635
let s = S()
2736

2837
// CHECK: synthesized method
2938
// CHECK: Storage
3039
s.useSynthesized()
3140

41+
// Members added via extension.
42+
s.extInstanceMethod()
43+
S.extStaticMethod()
44+
3245
@attached(member, names: arbitrary)
3346
macro addArbitraryMembers() = #externalMacro(module: "MacroDefinition", type: "AddArbitraryMembers")
3447

0 commit comments

Comments
 (0)