Skip to content

Commit 4e42dac

Browse files
committed
[Macros] Make sure we actually pass the conformance list to member macros
A bad test let this trivial bug through. Improve the test, fix the bug. Fixes rdar://117227204.
1 parent 5836709 commit 4e42dac

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
12391239
std::string conformanceList;
12401240
{
12411241
llvm::raw_string_ostream OS(conformanceList);
1242-
if (role == MacroRole::Extension) {
1242+
if (role == MacroRole::Extension || role == MacroRole::Member) {
12431243
llvm::interleave(
12441244
conformances,
12451245
[&](const ProtocolDecl *protocol) {

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,12 +2073,18 @@ extension RequiredDefaultInitMacro: MemberMacro {
20732073
conformingTo protocols: [TypeSyntax],
20742074
in context: some MacroExpansionContext
20752075
) throws -> [DeclSyntax] {
2076-
let decl: DeclSyntax
2077-
if declaration.is(ClassDeclSyntax.self) && protocols.isEmpty {
2078-
decl = "required init() { }"
2076+
let initDecl: DeclSyntax
2077+
let funcDecl: DeclSyntax
2078+
if !declaration.is(ClassDeclSyntax.self) {
2079+
initDecl = "init() { }"
2080+
funcDecl = "func f() { }"
2081+
} else if !protocols.isEmpty {
2082+
initDecl = "required init() { }"
2083+
funcDecl = "func f() { }"
20792084
} else {
2080-
decl = "init() { }"
2085+
initDecl = "required init() { }"
2086+
funcDecl = "override func f() { }"
20812087
}
2082-
return [ decl ]
2088+
return [ initDecl, funcDecl ]
20832089
}
20842090
}

test/Macros/macro_expand_member_with_conformances.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ protocol DefaultInit {
99
}
1010

1111
@attached(extension, conformances: DefaultInit)
12-
@attached(member, conformances: DefaultInit, names: named(init()))
12+
@attached(member, conformances: DefaultInit, names: named(init()), named(f()))
1313
macro DefaultInit() = #externalMacro(module: "MacroDefinition", type: "RequiredDefaultInitMacro")
1414

1515
@DefaultInit

0 commit comments

Comments
 (0)