-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathmacro_expand_extensions.swift
268 lines (198 loc) · 9.24 KB
/
macro_expand_extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// REQUIRES: swift_swift_parser, executable_test
// RUN: %empty-directory(%t)
// 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
// Check for errors first
// RUN: %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) %s -I %t -disable-availability-checking
// RUN: %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) %s -I %t -disable-availability-checking -dump-macro-expansions > %t/expansions-dump.txt 2>&1
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 -I %t
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s -emit-macro-expansion-files no-diagnostics
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK-DIAGS %s
// Ensure that we can serialize this file as a module.
// RUN: %target-swift-frontend -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -I %t -disable-availability-checking -emit-module -o %t/MyModule.swiftmodule -enable-testing
// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser -swift-version 5 -emit-tbd -emit-tbd-path %t/MacroUser.tbd -I %t
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
@attached(extension, conformances: P, names: named(requirement))
macro DelegatedConformance() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceViaExtensionMacro")
protocol P {
static func requirement()
}
struct Wrapped: P {
static func requirement() {
print("Wrapped.requirement")
}
}
@DelegatedConformance
struct Generic<Element> {}
// CHECK-DUMP: @__swiftmacro_23macro_expand_extensions7Generic20DelegatedConformancefMe_.swift
// CHECK-DUMP: extension Generic: P where Element: P {
// CHECK-DUMP: static func requirement() {
// CHECK-DUMP: Element.requirement()
// CHECK-DUMP: }
// CHECK-DUMP: }
func requiresP(_ value: (some P).Type) {
value.requirement()
}
// CHECK: Wrapped.requirement
requiresP(Generic<Wrapped>.self)
struct Outer {
@DelegatedConformance
struct Nested<Element> {}
}
// CHECK-DUMP: @__swiftmacro_23macro_expand_extensions5OuterV6Nested20DelegatedConformancefMe_.swift
// CHECK-DUMP: extension Outer.Nested: P where Element: P {
// CHECK-DUMP: static func requirement() {
// CHECK-DUMP: Element.requirement()
// CHECK-DUMP: }
// CHECK-DUMP: }
// CHECK: Wrapped.requirement
requiresP(Outer.Nested<Wrapped>.self)
@DelegatedConformance
struct AlreadyConforms: P {
static func requirement() {}
}
// CHECK-DUMP-LABEL: @__swiftmacro_23macro_expand_extensions15AlreadyConforms20DelegatedConformancefMe_.swift
// CHECK-DUMP-NOT: extension AlreadyConforms
@DelegatedConformance
struct AlreadyConformsInExtension {}
extension AlreadyConformsInExtension: P {
static func requirement() {}
}
// CHECK-DUMP-LABEL: @__swiftmacro_23macro_expand_extensions26AlreadyConformsInExtension20DelegatedConformancefMe_.swift
// CHECK-DUMP-NOT: extension AlreadyConformsInExtension
class ConformsExplicitly<Element>: P {
static func requirement() {}
}
@DelegatedConformance
class InheritsConformance<Element>: ConformsExplicitly<Element> {}
// CHECK-DUMP-LABEL: @__swiftmacro_23macro_expand_extensions19InheritsConformance09DelegatedE0fMe_.swift
// CHECK-DUMP-NOT: extension InheritsConformance
@DelegatedConformance
class ConformsViaMacro<Element> {
}
// CHECK-DUMP-LABEL: @__swiftmacro_23macro_expand_extensions16ConformsViaMacro20DelegatedConformancefMe_.swift
// CHECK-DUMP: extension ConformsViaMacro: P where Element: P {
// CHECK-DUMP: static func requirement() {
// CHECK-DUMP: Element.requirement()
// CHECK-DUMP: }
// CHECK-DUMP: }
@DelegatedConformance
class InheritsConformanceViaMacro<Element: P>: ConformsViaMacro<Element> {}
// CHECK-DUMP-LABEL: @__swiftmacro_23macro_expand_extensions27InheritsConformanceViaMacro09DelegatedE0fMe_.swift
// CHECK-DUMP-NOT: extension InheritsConformanceViaMacro
#if TEST_DIAGNOSTICS
func testLocal() {
@DelegatedConformance
struct Local<Element> {}
// expected-error@-1{{local type cannot have attached extension macro}}
}
@DelegatedConformance
typealias A = Int
// expected-error@-2 {{'extension' macro cannot be attached to type alias ('A')}}
@DelegatedConformance
extension Int {}
// expected-error@-2 {{'extension' macro cannot be attached to extension (extension of 'Int')}}
@attached(extension, conformances: P)
macro UndocumentedNamesInExtension() = #externalMacro(module: "MacroDefinition", type: "DelegatedConformanceViaExtensionMacro")
@UndocumentedNamesInExtension
struct S<Element> {}
// expected-note@-2 {{in expansion of macro 'UndocumentedNamesInExtension' on generic struct 'S' here}}
// CHECK-DIAGS: error: declaration name 'requirement()' is not covered by macro 'UndocumentedNamesInExtension'
@attached(extension, names: named(requirement))
macro UndocumentedConformanceInExtension() = #externalMacro(module: "MacroDefinition", type: "AlwaysAddConformance")
@UndocumentedConformanceInExtension
struct InvalidConformance<Element> {}
// expected-note@-2 {{in expansion of macro 'UndocumentedConformanceInExtension' on generic struct 'InvalidConformance' here}}
// CHECK-DIAGS: error: conformance to 'P' is not covered by macro 'UndocumentedConformanceInExtension'
@attached(extension)
macro UndocumentedCodable() = #externalMacro(module: "MacroDefinition", type: "AlwaysAddCodable")
@UndocumentedCodable
struct TestUndocumentedCodable {}
// expected-note@-2 {{in expansion of macro 'UndocumentedCodable' on struct 'TestUndocumentedCodable' here}}
// CHECK-DIAGS: error: conformance to 'Codable' (aka 'Decodable & Encodable') is not covered by macro 'UndocumentedCodable'
@attached(extension, conformances: Decodable)
macro UndocumentedEncodable() = #externalMacro(module: "MacroDefinition", type: "AlwaysAddCodable")
@UndocumentedEncodable
struct TestUndocumentedEncodable {}
// expected-note@-2 {{in expansion of macro 'UndocumentedEncodable' on struct 'TestUndocumentedEncodable' here}}
// CHECK-DIAGS: error: conformance to 'Codable' (aka 'Decodable & Encodable') is not covered by macro 'UndocumentedEncodable'
#endif
@attached(extension, conformances: Equatable)
macro AvailableEquatable() = #externalMacro(module: "MacroDefinition", type: "ConditionallyAvailableConformance")
@available(macOS 99, *)
@AvailableEquatable
struct TestAvailability {
static let x : any Equatable.Type = TestAvailability.self
}
protocol P1 {}
protocol P2 {}
@attached(extension, conformances: P1, P2)
macro AddAllConformances() = #externalMacro(module: "MacroDefinition", type: "AddAllConformancesMacro")
@AddAllConformances
struct MultipleConformances {}
// CHECK-DUMP: extension MultipleConformances: P1 {
// CHECK-DUMP: }
// CHECK-DUMP: extension MultipleConformances: P2 {
// CHECK-DUMP: }
@attached(extension, conformances: Equatable, names: named(==))
macro Equatable() = #externalMacro(module: "MacroDefinition", type: "EquatableViaMembersMacro")
@propertyWrapper
struct NotEquatable<T> {
var wrappedValue: T
}
@Equatable
struct HasPropertyWrappers {
@NotEquatable
var value: Int = 0
}
func requiresEquatable<T: Equatable>(_: T) { }
func testHasPropertyWrappers(hpw: HasPropertyWrappers) {
requiresEquatable(hpw)
}
@Equatable
struct HasPrivateMembers {
@NotEquatable
private var value: Int = 0
}
// Check that conformances implied by a macro-defined conformance are serialized
// without issue.
public protocol ImpliesHashable: Hashable { }
@attached(extension, conformances: ImpliesHashable)
macro ImpliesHashable() = #externalMacro(module: "MacroDefinition", type: "ImpliesHashableMacro")
func requiresHashable<T: Hashable>(_: T) { }
func testMakeMeHashable(mmh: MakeMeHashable, dict: [MakeMeHashable: Int]) {
requiresHashable(mmh)
}
@ImpliesHashable
public struct MakeMeHashable { }
// https://github.com/apple/swift/issues/67989 - member added via extension
// to a protocol is not visible
@attached(extension, names: named(foo), named(printFoo))
public macro FooExtension() = #externalMacro(module: "MacroDefinition", type: "FooExtensionMacro")
@FooExtension
protocol Foo {
var foo: String { get }
}
extension String: Foo { }
func testStringFoo(s: String) {
"Test".printFoo()
s.printFoo()
}
@attached(extension, conformances: Sendable)
macro AddSendable() = #externalMacro(module: "MacroDefinition", type: "SendableMacro")
@AddSendable
final class SendableClass {
}
// expected-warning@+2 {{non-final class 'InvalidSendableClass' cannot conform to 'Sendable'; use '@unchecked Sendable'}}
@AddSendable
class InvalidSendableClass {
}
@AddSendable
struct HasNestedType {
struct Inner {}
}
// Make sure no circularity error is produced when resolving
// extensions of nested types when the outer type has an
// attached macro that can add other nested types.
extension HasNestedType.Inner {}