Skip to content

[Macros] Don't apply member attribute macros to accessors #70435

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
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
4 changes: 4 additions & 0 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ ArrayRef<unsigned> ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
if (decl->isImplicit())
return { };

// Member attribute macros do not apply to accessors.
if (isa<AccessorDecl>(decl))
return { };

// Member attribute macros do not apply to macro-expanded members.
if (decl->isInMacroExpansionInContext())
return { };
Expand Down
15 changes: 15 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2259,3 +2259,18 @@ public struct LoggerMacro: PreambleMacro {
]
}
}

public struct AddMemberPeersMacro: MemberAttributeMacro {
public static func expansion(of node: AttributeSyntax, attachedTo declaration: some DeclGroupSyntax, providingAttributesFor member: some DeclSyntaxProtocol, in context: some MacroExpansionContext) throws -> [AttributeSyntax] {
["@_AddPeer"]
}
}

public struct _AddPeerMacro: PeerMacro {
public static func expansion<Decl: DeclSyntaxProtocol>(of node: AttributeSyntax, providingPeersOf declaration: Decl, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
guard let name = declaration.as(VariableDeclSyntax.self)?.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier.text else {
return []
}
return ["static let \(raw: name)_special = 41"]
}
}
13 changes: 13 additions & 0 deletions test/Macros/macro_expand_attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,16 @@ typealias A = Int
func noMembers() {}
// expected-error@-2{{'memberAttribute' macro cannot be attached to global function ('noMembers')}}
#endif

@attached(memberAttribute)
public macro AddMemberPeers() = #externalMacro(module: "MacroDefinition", type: "AddMemberPeersMacro")

@attached(peer, names: suffixed(_special))
public macro _AddPeer() = #externalMacro(module: "MacroDefinition", type: "_AddPeerMacro")

@AddMemberPeers
struct User {
var name: String {
"mario"
}
}