Skip to content

Commit 043a13a

Browse files
authored
Merge pull request #70751 from DougGregor/member-attribute-insertion-after-written-attrs
Insert member-attribute macros right before modifiers
2 parents 2c2f05e + bcf2371 commit 043a13a

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ static CharSourceRange getExpansionInsertionRange(MacroRole role,
895895
case MacroRole::MemberAttribute: {
896896
SourceLoc startLoc;
897897
if (auto valueDecl = dyn_cast<ValueDecl>(target.get<Decl *>()))
898-
startLoc = valueDecl->getAttributeInsertionLoc(/*forModifier=*/false);
898+
startLoc = valueDecl->getAttributeInsertionLoc(/*forModifier=*/true);
899899
else
900900
startLoc = target.getStartLoc();
901901

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,3 +2274,13 @@ public struct _AddPeerMacro: PeerMacro {
22742274
return ["static let \(raw: name)_special = 41"]
22752275
}
22762276
}
2277+
2278+
public struct WrapperMacro: PeerMacro {
2279+
public static func expansion(
2280+
of node: SwiftSyntax.AttributeSyntax,
2281+
providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol,
2282+
in context: some SwiftSyntaxMacros.MacroExpansionContext
2283+
) throws -> [SwiftSyntax.DeclSyntax] {
2284+
[]
2285+
}
2286+
}

test/Macros/macro_expand_peers.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,35 @@ func fDeprecated(a: Int, for b: String, _ value: Double) async -> String {
257257
}
258258

259259
#endif
260+
261+
protocol MyType {
262+
associatedtype Value
263+
associatedtype Entity
264+
}
265+
266+
@attached(peer, names: named(bar))
267+
macro Wrapper<Value>(
268+
get: (Value.Entity) async throws -> Value.Value
269+
) = #externalMacro(module: "MacroDefinition", type: "WrapperMacro")
270+
where Value: MyType
271+
272+
@attached(peer)
273+
macro _AddPeer() = #externalMacro(module: "MacroDefinition", type: "WrapperMacro")
274+
275+
@attached(memberAttribute)
276+
public macro AddMemberPeers() = #externalMacro(module: "MacroDefinition", type: "AddMemberPeersMacro")
277+
278+
struct Thing: MyType {
279+
typealias Entity = [Int]
280+
typealias Value = Int
281+
}
282+
283+
@AddMemberPeers
284+
struct Test {
285+
@Wrapper<Thing>(
286+
get: { p1 in
287+
p1.count // Error: Cannot find 'p1' in scope
288+
}
289+
)
290+
var doesNotWork: Thing
291+
}

0 commit comments

Comments
 (0)