Skip to content

Commit e76c309

Browse files
authored
Merge pull request #64893 from DougGregor/peer-macro-scope-tree-fix-5.9
[5.9] [ASTScope] Adjust parent location for peer macro to after its attached decl
2 parents e840d9a + 63ba145 commit e76c309

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

lib/AST/ASTScopeCreation.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "swift/AST/SourceFile.h"
3131
#include "swift/AST/Stmt.h"
3232
#include "swift/AST/TypeRepr.h"
33+
#include "swift/Parse/Lexer.h"
3334
#include "swift/Basic/Debug.h"
3435
#include "swift/Basic/STLExtras.h"
3536
#include "llvm/Support/Compiler.h"
@@ -262,10 +263,19 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
262263
case MacroRole::Declaration:
263264
case MacroRole::Accessor:
264265
case MacroRole::MemberAttribute:
265-
case MacroRole::Peer:
266266
case MacroRole::Conformance:
267267
parentLoc = expansion.getStartLoc();
268268
break;
269+
case MacroRole::Peer: {
270+
ASTContext &ctx = SF->getASTContext();
271+
SourceManager &sourceMgr = ctx.SourceMgr;
272+
auto generatedSourceInfo =
273+
*sourceMgr.getGeneratedSourceInfo(*SF->getBufferID());
274+
275+
ASTNode node = ASTNode::getFromOpaqueValue(generatedSourceInfo.astNode);
276+
parentLoc = Lexer::getLocForEndOfToken(sourceMgr, node.getEndLoc());
277+
break;
278+
}
269279
case MacroRole::Member: {
270280
// For synthesized member macros, take the end loc of the
271281
// enclosing declaration (before the closing brace), because

test/Macros/Inputs/macro_library.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ public macro ObservableProperty() = #externalMacro(module: "MacroDefinition", ty
3535

3636
@attached(peer, names: overloaded)
3737
public macro addCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
38+
39+
@attached(peer, names: suffixed(Builder))
40+
public macro AddClassReferencingSelf() = #externalMacro(module: "MacroDefinition", type: "AddClassReferencingSelfMacro")

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,3 +1290,26 @@ public struct DefineAnonymousTypesMacro: DeclarationMacro {
12901290
]
12911291
}
12921292
}
1293+
1294+
public struct AddClassReferencingSelfMacro: PeerMacro {
1295+
public static func expansion(
1296+
of node: AttributeSyntax,
1297+
providingPeersOf declaration: some DeclSyntaxProtocol,
1298+
in context: some MacroExpansionContext
1299+
) throws -> [DeclSyntax] {
1300+
guard let protocolDecl = declaration.as(ProtocolDeclSyntax.self) else {
1301+
throw CustomError.message("Macro can only be applied to a protocol declarations.")
1302+
}
1303+
1304+
let className = "\(protocolDecl.identifier.text)Builder"
1305+
return [
1306+
"""
1307+
struct \(raw: className) {
1308+
init(_ build: (_ builder: Self) -> Self) {
1309+
_ = build(self)
1310+
}
1311+
}
1312+
"""
1313+
]
1314+
}
1315+
}

test/Macros/macro_expand_peers.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import macro_library
2424
#else
2525
@attached(peer, names: overloaded)
2626
macro addCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
27+
@attached(peer, names: suffixed(Builder))
28+
macro AddClassReferencingSelf() = #externalMacro(module: "MacroDefinition", type: "AddClassReferencingSelfMacro")
2729
#endif
2830

2931
struct S {
@@ -120,3 +122,6 @@ struct Main {
120122
// CHECK-EXEC: hahaha global
121123
}
122124
}
125+
126+
@AddClassReferencingSelf
127+
protocol MyProto { }

0 commit comments

Comments
 (0)