Skip to content

[5.9] [Macros] Fix lookup of macro-produced variables #66010

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
May 19, 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
5 changes: 4 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10328,9 +10328,12 @@ void MissingDecl::forEachMacroExpandedDecl(MacroExpandedDeclCallback callback) {
auto *baseDecl = unexpandedMacro.baseDecl;
if (!macroRef || !baseDecl)
return;
auto *module = getModuleContext();

baseDecl->visitAuxiliaryDecls([&](Decl *auxiliaryDecl) {
auto *sf = auxiliaryDecl->getInnermostDeclContext()->getParentSourceFile();
SourceFile *sf = auxiliaryDecl->getLoc()
? module->getSourceFileContainingLocation(auxiliaryDecl->getLoc())
: auxiliaryDecl->getInnermostDeclContext()->getParentSourceFile();
// We only visit auxiliary decls that are macro expansions associated with
// this macro reference.
if (auto *med = macroRef.dyn_cast<MacroExpansionDecl *>()) {
Expand Down
5 changes: 4 additions & 1 deletion lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,10 @@ void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,
return;

// Synthesized member macros can add new members in a macro expansion buffer.
auto *memberSourceFile = member->getInnermostDeclContext()->getParentSourceFile();
SourceFile *memberSourceFile = member->getLoc()
? member->getModuleContext()
->getSourceFileContainingLocation(member->getLoc())
: member->getInnermostDeclContext()->getParentSourceFile();
if (memberSourceFile->getFulfilledMacroRole() == MacroRole::Member ||
memberSourceFile->getFulfilledMacroRole() == MacroRole::Peer)
return;
Expand Down
3 changes: 3 additions & 0 deletions test/Macros/Inputs/freestanding_macro_library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ public macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module

@freestanding(expression)
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")

@freestanding(declaration, names: named(value))
public macro varValue() = #externalMacro(module: "MacroDefinition", type: "VarValueMacro")
3 changes: 3 additions & 0 deletions test/Macros/Inputs/macro_library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ public macro addCompletionHandler() = #externalMacro(module: "MacroDefinition",

@attached(peer, names: suffixed(Builder))
public macro AddClassReferencingSelf() = #externalMacro(module: "MacroDefinition", type: "AddClassReferencingSelfMacro")

@attached(peer, names: named(value))
public macro declareVarValuePeer() = #externalMacro(module: "MacroDefinition", type: "VarValueMacro")
21 changes: 21 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1415,3 +1415,24 @@ public struct MultiStatementClosure: ExpressionMacro {
"""
}
}

public struct VarValueMacro: DeclarationMacro, PeerMacro {
public static func expansion(
of macro: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> [DeclSyntax] {
return [
"var value: Int { 1 }"
]
}

public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return [
"var value: Int { 1 }"
]
}
}
5 changes: 2 additions & 3 deletions test/Macros/macro_attribute_expansiondecl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ public struct FuncFromClosureMacro: DeclarationMacro {
@available(SwiftStdlib 9999, *)
#globalDecls

func testGlobal() { // expected-note {{add @available attribute to enclosing global function}}
func testGlobal() { // expected-note 2 {{add @available attribute to enclosing global function}}
globalFunc() // expected-error {{'globalFunc()' is only available in macOS 9999 or newer}} expected-note {{add 'if #available' version check}}
// FIXME(109376568): Global variable introduced by macro expansion not found
_ = globalVar // expected-error {{cannot find 'globalVar' in scope}}
_ = globalVar // expected-error {{'globalVar' is only available in macOS 9999 or newer}} expected-note {{add 'if #available' version check}}
}

struct S {
Expand Down
15 changes: 13 additions & 2 deletions test/Macros/macro_expand_peers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import macro_library
macro addCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
@attached(peer, names: suffixed(Builder))
macro AddClassReferencingSelf() = #externalMacro(module: "MacroDefinition", type: "AddClassReferencingSelfMacro")
@attached(peer, names: named(value))
macro declareVarValuePeer() = #externalMacro(module: "MacroDefinition", type: "VarValueMacro")
#endif

@attached(peer, names: arbitrary)
Expand Down Expand Up @@ -162,8 +164,6 @@ struct S2 {
macro myPropertyWrapper() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperMacro")

struct Date { }

struct MyWrapperThingy<T> {
var storage: T

Expand All @@ -188,3 +188,14 @@ struct S3 {
self._x = MyWrapperThingy(storage: x)
}
}

@declareVarValuePeer
struct Date {
@declareVarValuePeer
func foo() {}
}

func testVarPeer() {
_ = value
_ = Date().value
}
7 changes: 7 additions & 0 deletions test/Macros/top_level_freestanding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ macro anonymousTypes(public: Bool = false, _: () -> String) = #externalMacro(mod
macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
@freestanding(declaration, names: named(value)) macro varValue() = #externalMacro(module: "MacroDefinition", type: "VarValueMacro")
#endif

// Test unqualified lookup from within a macro expansion
Expand Down Expand Up @@ -79,3 +80,9 @@ func testArbitraryAtGlobal() {

// DIAG_BUFFERS: @__swiftmacro_9MacroUser33_{{.*}}9stringifyfMf1_{{.*}}warning: 'deprecated()' is deprecated
// DIAG_BUFFERS: @__swiftmacro_9MacroUser33_{{.*}}9stringifyfMf2_{{.*}}warning: 'deprecated()' is deprecated

#varValue

func testGlobalVariable() {
_ = value
}