Skip to content

Commit 4895334

Browse files
authored
Merge pull request #67189 from hborla/5.9-extension-macro-fixes
[5.9][Macros] A few bug fixes for extension macros.
2 parents 5efec1c + 9f5a7a0 commit 4895334

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ func expandAttachedMacroIPC(
837837
}
838838

839839
let conformanceListSyntax: PluginMessage.Syntax?
840-
if (qualifiedType.isEmpty) {
840+
if (conformanceList.isEmpty) {
841841
conformanceListSyntax = nil
842842
} else {
843843
let placeholderDecl: DeclSyntax =

lib/SIL/IR/SILSymbolVisitor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
238238
void addConformances(const IterableDeclContext *IDC) {
239239
for (auto conformance :
240240
IDC->getLocalConformances(ConformanceLookupKind::NonInherited)) {
241+
if (conformance->getSourceKind() == ConformanceEntryKind::PreMacroExpansion)
242+
continue;
243+
241244
auto protocol = conformance->getProtocol();
242245
if (Ctx.getOpts().PublicSymbolsOnly &&
243246
getDeclLinkage(protocol) != FormalLinkage::PublicUnique)

lib/Sema/TypeCheckMacros.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,10 @@ ResolveExtensionMacroConformances::evaluate(Evaluator &evaluator,
17151715

17161716
typeExpr->setType(MetatypeType::get(resolved));
17171717
protocols.push_back(resolved);
1718+
} else {
1719+
// If there's no type repr, we already have a resolved instance
1720+
// type, e.g. because the type expr was deserialized.
1721+
protocols.push_back(typeExpr->getInstanceType());
17181722
}
17191723
}
17201724

test/Macros/Inputs/macro_library.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public struct ObservationRegistrar<Subject: Observable> {
2828
names: named(_registrar), named(addObserver), named(removeObserver), named(withTransaction), named(Storage), named(_storage)
2929
)
3030
@attached(memberAttribute)
31+
@attached(extension, conformances: Observable)
3132
public macro Observable() = #externalMacro(module: "MacroDefinition", type: "ObservableMacro")
3233

3334
@attached(accessor)

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,30 @@ public struct ObservableMacro: MemberMacro, MemberAttributeMacro {
11951195

11961196
}
11971197

1198+
extension ObservableMacro: ExtensionMacro {
1199+
public static func expansion(
1200+
of node: AttributeSyntax,
1201+
attachedTo decl: some DeclGroupSyntax,
1202+
providingExtensionsOf type: some TypeSyntaxProtocol,
1203+
conformingTo protocols: [TypeSyntax],
1204+
in context: some MacroExpansionContext
1205+
) throws -> [ExtensionDeclSyntax] {
1206+
if (protocols.isEmpty) {
1207+
return []
1208+
}
1209+
1210+
let decl: DeclSyntax =
1211+
"""
1212+
extension \(raw: type.trimmedDescription): Observable {
1213+
}
1214+
"""
1215+
1216+
return [
1217+
decl.cast(ExtensionDeclSyntax.self)
1218+
]
1219+
}
1220+
}
1221+
11981222
public struct ObservablePropertyMacro: AccessorMacro {
11991223
public static func expansion(
12001224
of node: AttributeSyntax,

test/Macros/macro_expand_peers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library -disable-availability-checking -DTEST_DIAGNOSTICS
66

77
// Check with the imported macro library vs. the local declaration of the macro.
8-
// 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)
8+
// 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)
99

1010
// 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
1111

test/Macros/macro_expand_primary.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: %empty-directory(%t)
44
// RUN: %empty-directory(%t-scratch)
55
// 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
6-
// 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)
6+
// 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)
77
// 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
88
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
99

@@ -86,6 +86,14 @@ func test() {
8686
observeDog()
8787
}
8888

89+
@Observable
90+
class Person {
91+
init() {}
92+
}
93+
94+
// CHECK-DUMP: extension Person: Observable {
95+
// CHECK-DUMP: }
96+
8997

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

0 commit comments

Comments
 (0)