Skip to content

[Macros] Fix the formatting of the conformance list buffer for extension macro expansion. #67889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,9 +1235,13 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
{
llvm::raw_string_ostream OS(conformanceList);
if (role == MacroRole::Extension) {
for (auto *protocol : conformances) {
protocol->getDeclaredType()->print(OS);
}
llvm::interleave(
conformances,
[&](const ProtocolDecl *protocol) {
protocol->getDeclaredType()->print(OS);
},
[&] { OS << ", "; }
);
} else {
OS << "";
}
Expand Down
18 changes: 18 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,24 @@ public struct ConditionallyAvailableConformance: ExtensionMacro {
}
}

public struct AddAllConformancesMacro: ExtensionMacro {
public static func expansion(
of node: AttributeSyntax,
attachedTo decl: some DeclGroupSyntax,
providingExtensionsOf type: some TypeSyntaxProtocol,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax] {
protocols.map { proto in
let decl: DeclSyntax =
"""
extension \(type): \(proto) {}
"""
return decl.cast(ExtensionDeclSyntax.self)
}
}
}

public struct AlwaysAddCodable: ExtensionMacro {
public static func expansion(
of node: AttributeSyntax,
Expand Down
14 changes: 14 additions & 0 deletions test/Macros/macro_expand_extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,17 @@ macro AvailableEquatable() = #externalMacro(module: "MacroDefinition", type: "Co
struct TestAvailability {
static let x : any Equatable.Type = TestAvailability.self
}

protocol P1 {}
protocol P2 {}

@attached(extension, conformances: P1, P2)
macro AddAllConformances() = #externalMacro(module: "MacroDefinition", type: "AddAllConformancesMacro")

@AddAllConformances
struct MultipleConformances {}

// CHECK-DUMP: extension MultipleConformances: P1 {
// CHECK-DUMP: }
// CHECK-DUMP: extension MultipleConformances: P2 {
// CHECK-DUMP: }