Skip to content

Commit 17bebad

Browse files
committed
[Macros] Separate each protocol name with commas when forming the conformance
list buffer for extension macro expansion.
1 parent 309e340 commit 17bebad

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,9 +1235,13 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
12351235
{
12361236
llvm::raw_string_ostream OS(conformanceList);
12371237
if (role == MacroRole::Extension) {
1238-
for (auto *protocol : conformances) {
1239-
protocol->getDeclaredType()->print(OS);
1240-
}
1238+
llvm::interleave(
1239+
conformances,
1240+
[&](const ProtocolDecl *protocol) {
1241+
protocol->getDeclaredType()->print(OS);
1242+
},
1243+
[&] { OS << ", "; }
1244+
);
12411245
} else {
12421246
OS << "";
12431247
}

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,24 @@ public struct ConditionallyAvailableConformance: ExtensionMacro {
14431443
}
14441444
}
14451445

1446+
public struct AddAllConformancesMacro: ExtensionMacro {
1447+
public static func expansion(
1448+
of node: AttributeSyntax,
1449+
attachedTo decl: some DeclGroupSyntax,
1450+
providingExtensionsOf type: some TypeSyntaxProtocol,
1451+
conformingTo protocols: [TypeSyntax],
1452+
in context: some MacroExpansionContext
1453+
) throws -> [ExtensionDeclSyntax] {
1454+
protocols.map { proto in
1455+
let decl: DeclSyntax =
1456+
"""
1457+
extension \(raw: type): \(raw: proto) {}
1458+
"""
1459+
return decl.cast(ExtensionDeclSyntax.self)
1460+
}
1461+
}
1462+
}
1463+
14461464
public struct AlwaysAddCodable: ExtensionMacro {
14471465
public static func expansion(
14481466
of node: AttributeSyntax,

test/Macros/macro_expand_extensions.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,17 @@ macro AvailableEquatable() = #externalMacro(module: "MacroDefinition", type: "Co
165165
struct TestAvailability {
166166
static let x : any Equatable.Type = TestAvailability.self
167167
}
168+
169+
protocol P1 {}
170+
protocol P2 {}
171+
172+
@attached(extension, conformances: P1, P2)
173+
macro AddAllConformances() = #externalMacro(module: "MacroDefinition", type: "AddAllConformancesMacro")
174+
175+
@AddAllConformances
176+
struct MultipleConformances {}
177+
178+
// CHECK-DUMP: extension MultipleConformances: P1 {
179+
// CHECK-DUMP: }
180+
// CHECK-DUMP: extension MultipleConformances: P2 {
181+
// CHECK-DUMP: }

0 commit comments

Comments
 (0)