-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathmacro_expand_synthesized_members.swift
177 lines (142 loc) · 4.83 KB
/
macro_expand_synthesized_members.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
// 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
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 -verify-ignore-unknown
// RUN: %target-build-swift -g -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser -swift-version 5
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
@attached(
member,
names: named(init), named(Storage), named(storage), named(getStorage()), named(method), named(init(other:))
)
macro addMembers() = #externalMacro(module: "MacroDefinition", type: "AddMembers")
@attached(
member,
names: named(`init`), named(Storage), named(storage), named(getStorage()), named(method)
)
macro addMembersQuotedInit() = #externalMacro(module: "MacroDefinition", type: "AddMembers")
#if TEST_DIAGNOSTICS
@addMembers
import Swift
// expected-error@-2 {{'member' macro cannot be attached to import}}
#endif
@addMembers
struct S {
func useSynthesized() {
S.method()
print(type(of: getStorage()))
}
}
@attached(
member,
names: named(extInstanceMethod), named(extStaticMethod)
)
macro addExtMembers() = #externalMacro(module: "MacroDefinition", type: "AddExtMembers")
@addExtMembers
extension S { }
let s = S()
// CHECK: synthesized method
// CHECK: Storage
s.useSynthesized()
// Members added via extension.
s.extInstanceMethod()
S.extStaticMethod()
@attached(member, names: arbitrary)
macro addArbitraryMembers() = #externalMacro(module: "MacroDefinition", type: "AddArbitraryMembers")
@addArbitraryMembers
struct MyType {}
// CHECK: MyType1
// CHECK: MyType2
// CHECK: MyType3
print(MyType.MyType1.self)
print(MyType.MyType2.self)
print(MyType.MyType3.self)
@attached(
member,
names: named(RawValue), named(rawValue), named(init)
)
public macro NewType<T>() = #externalMacro(module: "MacroDefinition", type: "NewTypeMacro")
@NewType<String>
public struct MyString {}
// CHECK: String
// CHECK: hello
let myString = MyString("hello")
print(MyString.RawValue.self)
print(myString.rawValue)
struct Base {
static func member() -> Base { .init() }
}
@attached(member) macro empty(
_ : Base
) = #externalMacro(module: "MacroDefinition", type: "EmptyMacro")
@empty(.member())
struct TestMacroTypechecking {}
// Macros adding to an enum
@attached(member, names: named(unknown), arbitrary)
public macro ExtendableEnum() = #externalMacro(module: "MacroDefinition", type: "ExtendableEnum")
@ExtendableEnum
enum ElementType {
case paper
}
print(ElementType.paper.unknown())
#if TEST_DIAGNOSTICS
@addMembersQuotedInit
struct S2 {
// expected-note@-2 {{in expansion of macro 'addMembersQuotedInit' on struct 'S2' here}}
func useSynthesized() {
S.method()
print(type(of: getStorage()))
}
}
#endif
@attached(
member,
names: named(deinit)
)
macro addDeinit() = #externalMacro(module: "MacroDefinition", type: "AddDeinit")
@attached(
member,
names: named(subscript(unchecked:))
)
macro addSubscript() = #externalMacro(module: "MacroDefinition", type: "AddSubscript")
@addDeinit
@addSubscript
class C2 {
init() {
print("Created a C2")
}
}
func testC2() {
// CHECK: Created a C2
let c2 = C2()
// CHECK: 17
print(c2[unchecked: 17])
// CHECK: deinit was called
}
testC2()
@attached(member, names: arbitrary)
macro GenerateStubs() = #externalMacro(module: "MacroDefinition", type: "GenerateStubMemberMacro")
@freestanding(declaration, names: arbitrary)
macro generateMemberStubs() = #externalMacro(module: "MacroDefinition", type: "GenerateStubsFreestandingMacro")
@freestanding(declaration, names: named(member()))
macro generateMember() = #externalMacro(module: "MacroDefinition", type: "SingleMemberStubMacro")
@GenerateStubs
struct NestedMacroExpansion {}
func callNestedExpansionMember() {
NestedMacroExpansion.member()
}
@attached(peer, names: prefixed(`__`)) // introduces `__GenerateStubsForProtocolRequirements
@attached(extension, names: arbitrary) // introduces `extension GenerateStubsForProtocolRequirements`
macro GenerateStubsForProtocolRequirements() = #externalMacro(module: "MacroDefinition", type: "GenerateStubsForProtocolRequirementsMacro")
protocol _TestStub {} // used by 'GenerateStubsForProtocolRequirements'
@GenerateStubsForProtocolRequirements
protocol MacroExpansionRequirements {
func hello(name: String) -> String
}
// struct __MacroExpansionRequirements: _TestStub where ...
// extension MacroExpansionRequirements where Self: _TestStub ...
func testWitnessStub() {
let stub: any MacroExpansionRequirements = __MacroExpansionRequirements()
_ = stub.hello(name: "Caplin")
}