Skip to content

[ASTScope] Adjust parent location for peer macro to after its attached decl. #64892

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 1 commit into from
Apr 4, 2023
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
12 changes: 11 additions & 1 deletion lib/AST/ASTScopeCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "swift/AST/SourceFile.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Parse/Lexer.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/STLExtras.h"
#include "llvm/Support/Compiler.h"
Expand Down Expand Up @@ -262,10 +263,19 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
case MacroRole::Declaration:
case MacroRole::Accessor:
case MacroRole::MemberAttribute:
case MacroRole::Peer:
case MacroRole::Conformance:
parentLoc = expansion.getStartLoc();
break;
case MacroRole::Peer: {
ASTContext &ctx = SF->getASTContext();
SourceManager &sourceMgr = ctx.SourceMgr;
const auto &generatedSourceInfo =
*sourceMgr.getGeneratedSourceInfo(*SF->getBufferID());

ASTNode node = ASTNode::getFromOpaqueValue(generatedSourceInfo.astNode);
parentLoc = Lexer::getLocForEndOfToken(sourceMgr, node.getEndLoc());
break;
}
case MacroRole::Member: {
// For synthesized member macros, take the end loc of the
// enclosing declaration (before the closing brace), because
Expand Down
3 changes: 3 additions & 0 deletions test/Macros/Inputs/macro_library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ public macro ObservableProperty() = #externalMacro(module: "MacroDefinition", ty

@attached(peer, names: overloaded)
public macro addCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")

@attached(peer, names: suffixed(Builder))
public macro AddClassReferencingSelf() = #externalMacro(module: "MacroDefinition", type: "AddClassReferencingSelfMacro")
23 changes: 23 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1296,3 +1296,26 @@ public struct DefineAnonymousTypesMacro: DeclarationMacro {
]
}
}

public struct AddClassReferencingSelfMacro: PeerMacro {
public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard let protocolDecl = declaration.as(ProtocolDeclSyntax.self) else {
throw CustomError.message("Macro can only be applied to a protocol declarations.")
}

let className = "\(protocolDecl.identifier.text)Builder"
return [
"""
struct \(raw: className) {
init(_ build: (_ builder: Self) -> Self) {
_ = build(self)
}
}
"""
]
}
}
5 changes: 5 additions & 0 deletions test/Macros/macro_expand_peers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import macro_library
#else
@attached(peer, names: overloaded)
macro addCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
@attached(peer, names: suffixed(Builder))
macro AddClassReferencingSelf() = #externalMacro(module: "MacroDefinition", type: "AddClassReferencingSelfMacro")
#endif

struct S {
Expand Down Expand Up @@ -113,3 +115,6 @@ struct Main {
// CHECK-EXEC: hahaha global
}
}

@AddClassReferencingSelf
protocol MyProto { }