Skip to content

Commit 66e651d

Browse files
hborlaDougGregor
authored andcommitted
[Macros] Separate each protocol name with commas when forming the conformance
list buffer for extension macro expansion. (cherry picked from commit 0be80c8)
1 parent 0664218 commit 66e651d

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
@@ -1238,9 +1238,13 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
12381238
{
12391239
llvm::raw_string_ostream OS(conformanceList);
12401240
if (role == MacroRole::Extension) {
1241-
for (auto *protocol : conformances) {
1242-
protocol->getDeclaredType()->print(OS);
1243-
}
1241+
llvm::interleave(
1242+
conformances,
1243+
[&](const ProtocolDecl *protocol) {
1244+
protocol->getDeclaredType()->print(OS);
1245+
},
1246+
[&] { OS << ", "; }
1247+
);
12441248
} else {
12451249
OS << "";
12461250
}

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,24 @@ public struct ConditionallyAvailableConformance: ExtensionMacro {
14811481
}
14821482
}
14831483

1484+
public struct AddAllConformancesMacro: ExtensionMacro {
1485+
public static func expansion(
1486+
of node: AttributeSyntax,
1487+
attachedTo decl: some DeclGroupSyntax,
1488+
providingExtensionsOf type: some TypeSyntaxProtocol,
1489+
conformingTo protocols: [TypeSyntax],
1490+
in context: some MacroExpansionContext
1491+
) throws -> [ExtensionDeclSyntax] {
1492+
protocols.map { proto in
1493+
let decl: DeclSyntax =
1494+
"""
1495+
extension \(type): \(proto) {}
1496+
"""
1497+
return decl.cast(ExtensionDeclSyntax.self)
1498+
}
1499+
}
1500+
}
1501+
14841502
public struct AlwaysAddCodable: ExtensionMacro {
14851503
public static func expansion(
14861504
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)