Skip to content

Commit 50bacd8

Browse files
committed
[Macros] Pass the extended type syntax for extension macros to
`expandAttachedMacro`.
1 parent 5d77ef4 commit 50bacd8

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,15 @@ extension CompilerPluginMessageHandler {
122122
expandingSyntax: expandingSyntax
123123
)
124124

125-
case .expandAttachedMacro(let macro, let macroRole, let discriminator, let attributeSyntax, let declSyntax, let parentDeclSyntax):
125+
case .expandAttachedMacro(let macro, let macroRole, let discriminator, let attributeSyntax, let declSyntax, let parentDeclSyntax, let extendedTypeSyntax):
126126
try expandAttachedMacro(
127127
macro: macro,
128128
macroRole: macroRole,
129129
discriminator: discriminator,
130130
attributeSyntax: attributeSyntax,
131131
declSyntax: declSyntax,
132-
parentDeclSyntax: parentDeclSyntax
132+
parentDeclSyntax: parentDeclSyntax,
133+
extendedTypeSyntax: extendedTypeSyntax
133134
)
134135

135136
case .loadPluginLibrary(let libraryPath, let moduleName):

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ extension CompilerPluginMessageHandler {
8686
discriminator: String,
8787
attributeSyntax: PluginMessage.Syntax,
8888
declSyntax: PluginMessage.Syntax,
89-
parentDeclSyntax: PluginMessage.Syntax?
89+
parentDeclSyntax: PluginMessage.Syntax?,
90+
extendedTypeSyntax: PluginMessage.Syntax?
9091
) throws {
9192
let sourceManager = SourceManager()
9293
let context = PluginMacroExpansionContext(
@@ -100,6 +101,9 @@ extension CompilerPluginMessageHandler {
100101
).cast(AttributeSyntax.self)
101102
let declarationNode = sourceManager.add(declSyntax).cast(DeclSyntax.self)
102103
let parentDeclNode = parentDeclSyntax.map { sourceManager.add($0).cast(DeclSyntax.self) }
104+
let extendedType = extendedTypeSyntax.map {
105+
sourceManager.add($0).cast(TypeSyntax.self)
106+
}
103107

104108
// TODO: Make this a 'String?' and remove non-'hasExpandMacroResult' branches.
105109
let expandedSources: [String]?
@@ -115,6 +119,7 @@ extension CompilerPluginMessageHandler {
115119
attributeNode: attributeNode,
116120
declarationNode: declarationNode,
117121
parentDeclNode: parentDeclNode,
122+
extendedType: extendedType,
118123
in: context
119124
)
120125
if let expansions, hostCapability.hasExpandMacroResult {

Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ internal enum HostToPluginMessage: Codable {
3434
discriminator: String,
3535
attributeSyntax: PluginMessage.Syntax,
3636
declSyntax: PluginMessage.Syntax,
37-
parentDeclSyntax: PluginMessage.Syntax?
37+
parentDeclSyntax: PluginMessage.Syntax?,
38+
extendedTypeSyntax: PluginMessage.Syntax?
3839
)
3940

4041
/// Optionally implemented message to load a dynamic link library.

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private enum MacroExpansionError: Error, CustomStringConvertible {
4747
case parentDeclGroupNil
4848
case declarationNotDeclGroup
4949
case declarationNotIdentified
50+
case noExtendedTypeSyntax
5051
case noFreestandingMacroRoles(Macro.Type)
5152

5253
var description: String {
@@ -63,6 +64,9 @@ private enum MacroExpansionError: Error, CustomStringConvertible {
6364
case .declarationNotIdentified:
6465
return "declaration is not a 'Identified' syntax"
6566

67+
case .noExtendedTypeSyntax:
68+
return "no extended type for extension macro"
69+
6670
case .noFreestandingMacroRoles(let type):
6771
return "macro implementation type '\(type)' does not conform to any freestanding macro protocol"
6872

@@ -180,6 +184,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
180184
attributeNode: AttributeSyntax,
181185
declarationNode: DeclSyntax,
182186
parentDeclNode: DeclSyntax?,
187+
extendedType: TypeSyntax?,
183188
in context: Context
184189
) -> [String]? {
185190
do {
@@ -302,13 +307,10 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
302307
// Compiler error: type mismatch.
303308
throw MacroExpansionError.declarationNotDeclGroup
304309
}
305-
guard let identified = declarationNode.asProtocol(IdentifiedDeclSyntax.self)
306-
else {
307-
// Compiler error: type mismatch.
308-
throw MacroExpansionError.declarationNotIdentified
309-
}
310310

311-
let type: TypeSyntax = "\(identified.identifier)"
311+
guard let extendedType = extendedType else {
312+
throw MacroExpansionError.noExtendedTypeSyntax
313+
}
312314

313315
// Local function to expand a extension macro once we've opened up
314316
// the existential.
@@ -318,7 +320,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
318320
return try attachedMacro.expansion(
319321
of: attributeNode,
320322
attachedTo: node,
321-
providingExtensionsOf: type,
323+
providingExtensionsOf: extendedType,
322324
in: context
323325
)
324326
}
@@ -361,6 +363,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
361363
attributeNode: AttributeSyntax,
362364
declarationNode: DeclSyntax,
363365
parentDeclNode: DeclSyntax?,
366+
extendedType: TypeSyntax?,
364367
in context: Context
365368
) -> String? {
366369
let expandedSources = expandAttachedMacroWithoutCollapsing(
@@ -369,6 +372,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
369372
attributeNode: attributeNode,
370373
declarationNode: declarationNode,
371374
parentDeclNode: parentDeclNode,
375+
extendedType: extendedType,
372376
in: context
373377
)
374378
return expandedSources.map {

0 commit comments

Comments
 (0)