Skip to content

[5.9][Macros] A few bug fixes for extension macros. #67189

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 3 commits into from
Jul 9, 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
2 changes: 1 addition & 1 deletion lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ func expandAttachedMacroIPC(
}

let conformanceListSyntax: PluginMessage.Syntax?
if (qualifiedType.isEmpty) {
if (conformanceList.isEmpty) {
conformanceListSyntax = nil
} else {
let placeholderDecl: DeclSyntax =
Expand Down
3 changes: 3 additions & 0 deletions lib/SIL/IR/SILSymbolVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
void addConformances(const IterableDeclContext *IDC) {
for (auto conformance :
IDC->getLocalConformances(ConformanceLookupKind::NonInherited)) {
if (conformance->getSourceKind() == ConformanceEntryKind::PreMacroExpansion)
continue;

auto protocol = conformance->getProtocol();
if (Ctx.getOpts().PublicSymbolsOnly &&
getDeclLinkage(protocol) != FormalLinkage::PublicUnique)
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,10 @@ ResolveExtensionMacroConformances::evaluate(Evaluator &evaluator,

typeExpr->setType(MetatypeType::get(resolved));
protocols.push_back(resolved);
} else {
// If there's no type repr, we already have a resolved instance
// type, e.g. because the type expr was deserialized.
protocols.push_back(typeExpr->getInstanceType());
}
}

Expand Down
1 change: 1 addition & 0 deletions test/Macros/Inputs/macro_library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public struct ObservationRegistrar<Subject: Observable> {
names: named(_registrar), named(addObserver), named(removeObserver), named(withTransaction), named(Storage), named(_storage)
)
@attached(memberAttribute)
@attached(extension, conformances: Observable)
public macro Observable() = #externalMacro(module: "MacroDefinition", type: "ObservableMacro")

@attached(accessor)
Expand Down
24 changes: 24 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,30 @@ public struct ObservableMacro: MemberMacro, MemberAttributeMacro {

}

extension ObservableMacro: ExtensionMacro {
public static func expansion(
of node: AttributeSyntax,
attachedTo decl: some DeclGroupSyntax,
providingExtensionsOf type: some TypeSyntaxProtocol,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax] {
if (protocols.isEmpty) {
return []
}

let decl: DeclSyntax =
"""
extension \(raw: type.trimmedDescription): Observable {
}
"""

return [
decl.cast(ExtensionDeclSyntax.self)
]
}
}

public struct ObservablePropertyMacro: AccessorMacro {
public static func expansion(
of node: AttributeSyntax,
Expand Down
2 changes: 1 addition & 1 deletion test/Macros/macro_expand_peers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library -disable-availability-checking -DTEST_DIAGNOSTICS

// Check with the imported macro library vs. the local declaration of the macro.
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/macro_library.swiftmodule %S/Inputs/macro_library.swift -module-name macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)
// RUN: %target-swift-frontend -enable-experimental-feature ExtensionMacros -swift-version 5 -emit-module -o %t/macro_library.swiftmodule %S/Inputs/macro_library.swift -module-name macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)

// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library -disable-availability-checking -DIMPORT_MACRO_LIBRARY -I %t -DTEST_DIAGNOSTICS

Expand Down
10 changes: 9 additions & 1 deletion test/Macros/macro_expand_primary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t-scratch)
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/macro_library.swiftmodule %S/Inputs/macro_library.swift -module-name macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)
// RUN: %target-swift-frontend -enable-experimental-feature ExtensionMacros -swift-version 5 -emit-module -o %t/macro_library.swiftmodule %S/Inputs/macro_library.swift -module-name macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)
// RUN: %target-swift-frontend -swift-version 5 -typecheck -I%t -verify -primary-file %s %S/Inputs/macro_expand_other.swift -verify-ignore-unknown -load-plugin-library %t/%target-library-name(MacroDefinition) -dump-macro-expansions > %t/expansions-dump.txt 2>&1
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt

Expand Down Expand Up @@ -86,6 +86,14 @@ func test() {
observeDog()
}

@Observable
class Person {
init() {}
}

// CHECK-DUMP: extension Person: Observable {
// CHECK-DUMP: }


@freestanding(declaration, names: named(Foo)) macro useIdentifier(_ value: String) = #externalMacro(module: "MacroDefinition", type: "UseIdentifierMacro")

Expand Down