Skip to content

Commit 511eb07

Browse files
committed
Generize MemberAttributeMacro
1 parent 1b834a0 commit 511eb07

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

Sources/_SwiftSyntaxMacros/MacroProtocols/MemberAttributeMacro.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ public protocol MemberAttributeMacro: AttachedMacro {
2121
/// - Parameters:
2222
/// - node: The custom attribute describing the attached macro.
2323
/// - declaration: The declaration the macro attribute is attached to.
24-
/// - member: The member delcaration to attach the resulting attributes to.
24+
/// - member: The member declaration to attach the resulting attributes to.
2525
/// - context: The context in which to perform the macro expansion.
2626
///
2727
/// - Returns: the set of attributes to apply to the given member.
28-
static func expansion(
28+
static func expansion<
29+
Declaration: DeclGroupSyntax,
30+
Context: MacroExpansionContext
31+
>(
2932
of node: AttributeSyntax,
30-
attachedTo declaration: DeclSyntax,
33+
attachedTo declaration: Declaration,
3134
annotating member: DeclSyntax,
32-
in context: any MacroExpansionContext
35+
in context: Context
3336
) throws -> [AttributeSyntax]
3437
}

Sources/_SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,27 @@ extension MacroApplication {
425425
)
426426
}
427427

428+
private func expandMemberAttribute(
429+
attribute: AttributeSyntax,
430+
macro: MemberAttributeMacro.Type,
431+
decl: DeclGroupSyntax,
432+
member: DeclSyntax,
433+
in context: MacroExpansionContext
434+
) throws -> [AttributeSyntax] {
435+
#if false
436+
_openExistential(decl) { d in
437+
return try! macro.expansion(
438+
of: attribute,
439+
attachedTo: d,
440+
annotating: member,
441+
in: context
442+
)
443+
}
444+
#else
445+
return []
446+
#endif
447+
}
448+
428449
private func expandAttributes(
429450
for macroAttributes: [(AttributeSyntax, MemberAttributeMacro.Type)],
430451
attachedTo decl: DeclSyntax,
@@ -437,13 +458,19 @@ extension MacroApplication {
437458
var attributes: [AttributeSyntax] = []
438459
for (attribute, attributeMacro) in macroAttributes {
439460
do {
440-
try attributes.append(
441-
contentsOf: attributeMacro.expansion(
461+
let typedDecl = decl.asProtocol(DeclGroupSyntax.self)!
462+
463+
func expand<Decl: DeclGroupSyntax>(_ decl: Decl) throws -> [AttributeSyntax] {
464+
return try attributeMacro.expansion(
442465
of: attribute,
443-
attachedTo: DeclSyntax(decl),
466+
attachedTo: decl,
444467
annotating: member.decl,
445468
in: context
446469
)
470+
}
471+
472+
attributes.append(
473+
contentsOf: try _openExistential(typedDecl, do: expand)
447474
)
448475
} catch {
449476
// Record the error

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,14 @@ public struct AddBackingStorage: MemberMacro {
455455
}
456456

457457
public struct WrapAllProperties: MemberAttributeMacro {
458-
public static func expansion(
458+
public static func expansion<
459+
Declaration: DeclGroupSyntax,
460+
Context: MacroExpansionContext
461+
>(
459462
of node: AttributeSyntax,
460-
attachedTo decl: DeclSyntax,
463+
attachedTo decl: Declaration,
461464
annotating member: DeclSyntax,
462-
in context: any MacroExpansionContext
465+
in context: Context
463466
) throws -> [AttributeSyntax] {
464467
guard member.is(VariableDeclSyntax.self) else {
465468
return []
@@ -477,11 +480,14 @@ public struct WrapAllProperties: MemberAttributeMacro {
477480
}
478481

479482
public struct WrapStoredProperties: MemberAttributeMacro {
480-
public static func expansion(
483+
public static func expansion<
484+
Declaration: DeclGroupSyntax,
485+
Context: MacroExpansionContext
486+
>(
481487
of node: AttributeSyntax,
482-
attachedTo decl: DeclSyntax,
488+
attachedTo decl: Declaration,
483489
annotating member: DeclSyntax,
484-
in context: any MacroExpansionContext
490+
in context: Context
485491
) throws -> [AttributeSyntax] {
486492
guard let property = member.as(VariableDeclSyntax.self),
487493
property.bindings.count == 1
@@ -536,11 +542,14 @@ extension CustomTypeWrapperMacro: MemberMacro {
536542
}
537543

538544
extension CustomTypeWrapperMacro: MemberAttributeMacro {
539-
static func expansion(
545+
static func expansion<
546+
Declaration: DeclGroupSyntax,
547+
Context: MacroExpansionContext
548+
>(
540549
of node: AttributeSyntax,
541-
attachedTo declaration: DeclSyntax,
550+
attachedTo declaration: Declaration,
542551
annotating member: DeclSyntax,
543-
in context: any MacroExpansionContext
552+
in context: Context
544553
) throws -> [AttributeSyntax] {
545554
return [
546555
AttributeSyntax(

0 commit comments

Comments
 (0)