Skip to content

Commit 8119337

Browse files
authored
Merge pull request #1887 from bnbarham/provide-extension-type-fallback
Plugin should keep handling the conformance macro role
2 parents e7db129 + e67a859 commit 8119337

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private extension MacroRole {
168168
case .memberAttribute: self = .memberAttribute
169169
case .member: self = .member
170170
case .peer: self = .peer
171-
case .conformance: self = .conformance
171+
case .conformance: self = .extension
172172
case .codeItem: self = .codeItem
173173
case .extension: self = .extension
174174
}

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,13 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
251251
throw MacroExpansionError.declarationNotDeclGroup
252252
}
253253

254-
guard let extendedType = extendedType else {
254+
let extensionOf: TypeSyntax
255+
if let extendedType {
256+
extensionOf = extendedType
257+
} else if let identified = declarationNode.asProtocol(IdentifiedDeclSyntax.self) {
258+
// Fallback for old compilers with a new plugin, where
259+
extensionOf = TypeSyntax(SimpleTypeIdentifierSyntax(name: identified.identifier))
260+
} else {
255261
throw MacroExpansionError.noExtendedTypeSyntax
256262
}
257263

@@ -260,7 +266,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
260266
let extensions = try attachedMacro.expansion(
261267
of: attributeNode,
262268
attachedTo: declGroup,
263-
providingExtensionsOf: extendedType,
269+
providingExtensionsOf: extensionOf,
264270
conformingTo: protocols,
265271
in: context
266272
)

Sources/SwiftSyntaxMacros/MacroProtocols/ConformanceMacro.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ extension ConformanceMacro {
5151
for (proto, whereClause) in newConformances {
5252
let decl: DeclSyntax =
5353
"""
54-
extension \(type.trimmed): \(proto) {}
54+
extension \(type.trimmed): \(proto.trimmed) {}
5555
"""
5656

5757
var extensionDecl = decl.cast(ExtensionDeclSyntax.self)
5858

5959
if let whereClause {
60-
extensionDecl = extensionDecl.with(\.genericWhereClause, whereClause)
60+
extensionDecl = extensionDecl.with(\.genericWhereClause, whereClause.trimmed)
6161
}
6262

6363
extensions.append(extensionDecl)

0 commit comments

Comments
 (0)