Skip to content

Commit 4b2571a

Browse files
committed
[Macros] Add tests for conditional conformance expansion.
1 parent 02a53d0 commit 4b2571a

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

test/Demangle/Inputs/manglings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,4 @@ $s9MacroUser016testFreestandingA9ExpansionyyF4Foo3L_V23bitwidthNumberedStructsfM
456456
@__swiftmacro_1a13testStringifyAA1bySi_SitF9stringifyfMf_ ---> freestanding macro expansion #1 of stringify in a.testStringify(a: Swift.Int, b: Swift.Int) -> ()
457457
@__swiftmacro_15accessor_macros8MyStructV4nameSSvp17myPropertyWrapperfMa_ ---> accessor macro expansion #1 of myPropertyWrapper in accessor_macros.MyStruct.name : Swift.String
458458
@__swiftmacro_18macro_expand_peers1SV1f1a3for_SSSi_SSSdtYaF20addCompletionHandlerfMp_ ---> peer macro expansion #1 of addCompletionHandler in macro_expand_peers.S.f(a: Swift.Int, for: Swift.String, _: Swift.Double) async -> Swift.String
459+
@__swiftmacro_25macro_expand_conformances7GenericV20DelegatedConformancefMc_ ---> conformance macro expansion #1 of DelegatedConformance in macro_expand_conformances.Generic

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,38 @@ public struct EquatableMacro: ConformanceMacro {
10231023
return [(protocolName, nil)]
10241024
}
10251025
}
1026+
1027+
public struct DelegatedConformanceMacro: ConformanceMacro, MemberMacro {
1028+
public static func expansion(
1029+
of node: AttributeSyntax,
1030+
providingConformancesOf decl: some DeclGroupSyntax,
1031+
in context: some MacroExpansionContext
1032+
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1033+
let protocolName: TypeSyntax = "P"
1034+
let conformance: DeclSyntax =
1035+
"""
1036+
extension Placeholder where Element: P {}
1037+
"""
1038+
1039+
guard let extensionDecl = conformance.as(ExtensionDeclSyntax.self) else {
1040+
return []
1041+
}
1042+
1043+
return [(protocolName, extensionDecl.genericWhereClause)]
1044+
}
1045+
1046+
public static func expansion(
1047+
of node: AttributeSyntax,
1048+
providingMembersOf decl: some DeclGroupSyntax,
1049+
in context: some MacroExpansionContext
1050+
) throws -> [DeclSyntax] {
1051+
let requirement: DeclSyntax =
1052+
"""
1053+
static func requirement() where Element : P {
1054+
Element.requirement()
1055+
}
1056+
"""
1057+
1058+
return [requirement]
1059+
}
1060+
}

test/Macros/macro_expand_conformances.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
3-
// RUNx: %target-swift-frontend -dump-macro-expansions -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser 2>&1 | %FileCheck --check-prefix CHECK-DUMP %s
3+
// RUN: %target-swift-frontend -swift-version 5 -typecheck -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -disable-availability-checking -dump-macro-expansions > %t/expansions-dump.txt 2>&1
4+
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
45
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5
56
// RUN: %target-build-swift -swift-version 5 -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser -swift-version 5
67
// RUN: %target-run %t/main | %FileCheck %s
@@ -19,7 +20,35 @@ func requireEquatable(_ value: some Equatable) {
1920
@Equatable
2021
struct S {}
2122

23+
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances1SV9EquatablefMc_.swift
2224
// CHECK-DUMP: extension S : Equatable {}
2325

2426
// CHECK: true
2527
requireEquatable(S())
28+
29+
@attached(conformance)
30+
@attached(member)
31+
macro DelegatedConformance() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceMacro")
32+
33+
protocol P {
34+
static func requirement()
35+
}
36+
37+
struct Wrapped: P {
38+
static func requirement() {
39+
print("Wrapped.requirement")
40+
}
41+
}
42+
43+
@DelegatedConformance
44+
struct Generic<Element> {}
45+
46+
// CHECK-DUMP: @__swiftmacro_25macro_expand_conformances7GenericV20DelegatedConformancefMc_.swift
47+
// CHECK-DUMP: extension Generic : P where Element: P {}
48+
49+
func requiresP(_ value: (some P).Type) {
50+
value.requirement()
51+
}
52+
53+
// CHECK: Wrapped.requirement
54+
requiresP(Generic<Wrapped>.self)

0 commit comments

Comments
 (0)