Skip to content

Commit ac9cd68

Browse files
authored
Merge pull request #70435 from DougGregor/member-attribute-not-on-accessors
[Macros] Don't apply member attribute macros to accessors
2 parents 5e0bcab + 3f518a3 commit ac9cd68

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ ArrayRef<unsigned> ExpandMemberAttributeMacros::evaluate(Evaluator &evaluator,
515515
if (decl->isImplicit())
516516
return { };
517517

518+
// Member attribute macros do not apply to accessors.
519+
if (isa<AccessorDecl>(decl))
520+
return { };
521+
518522
// Member attribute macros do not apply to macro-expanded members.
519523
if (decl->isInMacroExpansionInContext())
520524
return { };

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,3 +2259,18 @@ public struct LoggerMacro: PreambleMacro {
22592259
]
22602260
}
22612261
}
2262+
2263+
public struct AddMemberPeersMacro: MemberAttributeMacro {
2264+
public static func expansion(of node: AttributeSyntax, attachedTo declaration: some DeclGroupSyntax, providingAttributesFor member: some DeclSyntaxProtocol, in context: some MacroExpansionContext) throws -> [AttributeSyntax] {
2265+
["@_AddPeer"]
2266+
}
2267+
}
2268+
2269+
public struct _AddPeerMacro: PeerMacro {
2270+
public static func expansion<Decl: DeclSyntaxProtocol>(of node: AttributeSyntax, providingPeersOf declaration: Decl, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
2271+
guard let name = declaration.as(VariableDeclSyntax.self)?.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier.text else {
2272+
return []
2273+
}
2274+
return ["static let \(raw: name)_special = 41"]
2275+
}
2276+
}

test/Macros/macro_expand_attributes.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,16 @@ typealias A = Int
150150
func noMembers() {}
151151
// expected-error@-2{{'memberAttribute' macro cannot be attached to global function ('noMembers')}}
152152
#endif
153+
154+
@attached(memberAttribute)
155+
public macro AddMemberPeers() = #externalMacro(module: "MacroDefinition", type: "AddMemberPeersMacro")
156+
157+
@attached(peer, names: suffixed(_special))
158+
public macro _AddPeer() = #externalMacro(module: "MacroDefinition", type: "_AddPeerMacro")
159+
160+
@AddMemberPeers
161+
struct User {
162+
var name: String {
163+
"mario"
164+
}
165+
}

0 commit comments

Comments
 (0)