Skip to content

[5.9] Make sure we visit auxiliary declarations for symbol visitation. #64944

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
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
2 changes: 1 addition & 1 deletion lib/SIL/IR/SILSymbolVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
void visit(FileUnit *file) {
auto visitFile = [this](FileUnit *file) {
SmallVector<Decl *, 16> decls;
file->getTopLevelDecls(decls);
file->getTopLevelDeclsWithAuxiliaryDecls(decls);

addMainIfNecessary(file);

Expand Down
27 changes: 25 additions & 2 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,20 @@ public struct DefineStructWithUnqualifiedLookupMacro: DeclarationMacro {
}
}

extension TupleExprElementListSyntax {
/// Retrieve the first element with the given label.
func first(labeled name: String) -> Element? {
return first { element in
if let label = element.label, label.text == name {
return true
}

return false
}
}
}


public struct DefineAnonymousTypesMacro: DeclarationMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
Expand All @@ -1267,13 +1281,22 @@ public struct DefineAnonymousTypesMacro: DeclarationMacro {
guard let body = node.trailingClosure else {
throw CustomError.message("#anonymousTypes macro requires a trailing closure")
}

let accessSpecifier: String
if let _ = node.argumentList.first(labeled: "public") {
accessSpecifier = "public "
} else {
accessSpecifier = ""
}
return [
"""

class \(context.makeUniqueName("name")) {
func hello() -> String {
\(raw:accessSpecifier)class \(context.makeUniqueName("name")) {
\(raw:accessSpecifier)func hello() -> String {
\(body.statements)
}

\(raw:accessSpecifier)func getSelf() -> Any.Type { return Self.self }
}
""",
"""
Expand Down
6 changes: 3 additions & 3 deletions test/Macros/top_level_freestanding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func lookupGlobalFreestandingExpansion() {
}

@freestanding(declaration)
macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
macro anonymousTypes(public: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")

#anonymousTypes { "hello" }
#anonymousTypes(public: true) { "hello" }

// CHECK-SIL: sil hidden @$s9MacroUser03$s9A71User33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_4namefMu_C5helloSSyF
// CHECK-SIL: sil @$s9MacroUser03$s9A71User33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_4namefMu_C5helloSSyF

@main
struct Main {
Expand Down
1 change: 1 addition & 0 deletions test/SourceKit/Macros/macro_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition"
// EXPAND_MACRO_DECL-NEXT: func hello() -> String {
// EXPAND_MACRO_DECL-NEXT: "hello"
// EXPAND_MACRO_DECL-NEXT: }
// EXPAND_MACRO_DECL: func getSelf() -> Any.Type { return Self.self }
// EXPAND_MACRO_DECL-NEXT: }
// EXPAND_MACRO_DECL-NEXT: enum $s9MacroUser33_70D4178875715FB9B8B50C58F66F8D53Ll14anonymousTypesfMf0_4namefMu0_ {
// EXPAND_MACRO_DECL-NEXT: case apple
Expand Down