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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
// REQUIRES: swift_swift_parser, executable_test
// For _Concurrency.
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
// RUN: %empty-directory(%t)
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -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) -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-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
// RUN: %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library %s -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-build-swift -swift-version 5 -Xfrontend -disable-availability-checking -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library %s -o %t/main -module-name MacroUser -g
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s -check-prefix=CHECK-EXEC
// Emit module while skipping function bodies
// RUN: %target-swift-frontend -swift-version 5 -emit-module -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library %s -disable-availability-checking -o %t/macro_expand_peers.swiftmodule -experimental-skip-non-inlinable-function-bodies-without-types
#if IMPORT_MACRO_LIBRARY
import macro_library
#else
@attached(peer, names: overloaded)
macro addCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
@attached(peer, names: suffixed(Builder))
macro AddClassReferencingSelf() = #externalMacro(module: "MacroDefinition", type: "AddClassReferencingSelfMacro")
@attached(peer, names: named(value))
macro declareVarValuePeer() = #externalMacro(module: "MacroDefinition", type: "VarValueMacro")
#endif
@attached(peer, names: arbitrary)
macro addCompletionHandlerArbitrarily(_: Int) = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
struct S {
#if IMPORT_MACRO_LIBRARY
@macro_library.addCompletionHandler // test module qualified macro lookup
func f(a: Int, for b: String, _ value: Double) async -> String {
return b
}
#else
@addCompletionHandler
func f(a: Int, for b: String, _ value: Double) async -> String {
return b
}
#endif
// CHECK-DUMP: @__swiftmacro_18macro_expand_peers1SV1f20addCompletionHandlerfMp_.swift
// CHECK-DUMP: func f(a: Int, for b: String, _ value: Double, completionHandler: @escaping (String) -> Void) {
// CHECK-DUMP: Task {
// CHECK-DUMP: completionHandler(await f(a: a, for: b, value))
// CHECK-DUMP: }
// CHECK-DUMP: }
func useOverload(_ body: @escaping (String) -> Void) {
self.f(a: 1, for: "hahaha local", 2.0) {
body($0)
}
}
}
extension S {
@addCompletionHandler
func g(a: Int, for b: String, _ value: Double) async -> String {
return b
}
// CHECK-DUMP: @__swiftmacro_18macro_expand_peers1SV1g20addCompletionHandlerfMp_.swift
// CHECK-DUMP: func g(a: Int, for b: String, _ value: Double, completionHandler: @escaping (String) -> Void) {
// CHECK-DUMP: Task {
// CHECK-DUMP: completionHandler(await g(a: a, for: b, value))
// CHECK-DUMP: }
// CHECK-DUMP: }
}
func useCompletionHandlerG(s: S, _ body: @escaping (String) -> Void) {
s.g(a: 1, for: "hahaha local", 2.0) {
body($0)
}
}
class C {
@addCompletionHandler
func f(a: Int, for b: String, _ value: Double) async -> String {
return b
}
}
@addCompletionHandler
func f(a: Int, for b: String, _ value: Double) async -> String {
return b
}
func useOverload(_ body: @escaping (String) -> Void) {
f(a: 1, for: "hahaha global", 2.0) {
body($0)
}
}
@attached(peer)
macro wrapInType() = #externalMacro(module: "MacroDefinition", type: "WrapInType")
@wrapInType
func global(a: Int, b: String) {
print(a, b)
}
// CHECK-DUMP: @__swiftmacro_18macro_expand_peers6global10wrapInTypefMp_.swift
// CHECK-DUMP: struct $s18macro_expand_peers6global10wrapInTypefMp_6globalfMu0_ {
// CHECK-DUMP: func $s18macro_expand_peers6global10wrapInTypefMp_6globalfMu_(a: Int, b: String) {
// CHECK-DUMP: global(a: a, b: b)
// CHECK-DUMP: }
// CHECK-DUMP: }
@main
struct Main {
static func main() async {
let result1 = await withCheckedContinuation { cont in
S().useOverload {
cont.resume(returning: $0)
}
}
print(result1)
// CHECK-EXEC: hahaha local
let result2 = await withCheckedContinuation { cont in
useOverload {
cont.resume(returning: $0)
}
}
print(result2)
// CHECK-EXEC: hahaha global
// CHECK-EXEC: MyWrapperThingy<Swift.Int>(storage: 5)
print(S3(x: 5))
}
}
@AddClassReferencingSelf
protocol MyProto { }
// Reference cycles amongst arbitrary peer macros and macro arguments.
let x = 10
let y = 10
struct S2 {
@addCompletionHandlerArbitrarily(x)
func f(a: Int, for b: String, _ value: Double) async -> String {
return b
}
@addCompletionHandlerArbitrarily(y)
func g(a: Int, for b: String, _ value: Double) async -> String {
return b
}
#if TEST_DIAGNOSTICS
// expected-error@+1 {{cannot find 'nonexistent' in scope}}
@addCompletionHandlerArbitrarily(nonexistent)
func h(a: Int, for b: String, _ value: Double) async -> String {
return b
}
#endif
}
#if TEST_DIAGNOSTICS
// Peer macros cannot introduce arbitrary names at global scope
//expected-error@+1 {{'peer' macros are not allowed to introduce arbitrary names at global scope}}
@addCompletionHandlerArbitrarily(x)
func h(a: Int, for b: String, _ value: Double) async -> String {
return b
}
#endif
// Stored properties generated by a peer macro
@attached(accessor)
@attached(peer, names: prefixed(_))
macro myPropertyWrapper() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperMacro")
struct MyWrapperThingy<T> {
var storage: T
var wrappedValue: T {
get {
print("Getting value \(storage)")
return storage
}
set {
print("Setting value \(newValue)")
storage = newValue
}
}
}
struct S3 {
@myPropertyWrapper
var x: Int = 0
init(x: Int) {
self._x = MyWrapperThingy(storage: x)
}
}
@declareVarValuePeer
struct Date {
@declareVarValuePeer
func foo() {}
}
func testVarPeer() {
_ = value
_ = Date().value
}
#if TEST_DIAGNOSTICS
// Macros cannot be attached to function parameters
// expected-error@+1{{'peer' macro cannot be attached to parameter ('x')}}
func test(@declareVarValuePeer x: Int) {}
#endif
// Stored properties added via peer macros.
@attached(peer, names: named(_foo))
macro AddPeerStoredProperty() =
#externalMacro(module: "MacroDefinition", type: "AddPeerStoredPropertyMacro")
struct SomeStructWithPeerProperties {
@AddPeerStoredProperty
var foo: String = "hello"
}
func testStructWithPeers() {
let x = SomeStructWithPeerProperties()
print(x)
}
#if TEST_DIAGNOSTICS
@available(*, deprecated, message: "This macro is deprecated.")
@attached(peer, names: overloaded)
macro deprecatedAddCompletionHandler() = #externalMacro(module: "MacroDefinition", type: "AddCompletionHandler")
// expected-warning@+1{{'deprecatedAddCompletionHandler()' is deprecated: This macro is deprecated.}}
@deprecatedAddCompletionHandler
func fDeprecated(a: Int, for b: String, _ value: Double) async -> String {
return b
}
#endif
protocol MyType {
associatedtype Value
associatedtype Entity
}
@attached(peer, names: named(bar))
macro Wrapper<Value>(
get: (Value.Entity) async throws -> Value.Value
) = #externalMacro(module: "MacroDefinition", type: "WrapperMacro")
where Value: MyType
@attached(peer)
macro _AddPeer() = #externalMacro(module: "MacroDefinition", type: "WrapperMacro")
@attached(memberAttribute)
public macro AddMemberPeers() = #externalMacro(module: "MacroDefinition", type: "AddMemberPeersMacro")
struct Thing: MyType {
typealias Entity = [Int]
typealias Value = Int
}
@AddMemberPeers
struct Test {
@Wrapper<Thing>(
get: { p1 in
p1.count // Error: Cannot find 'p1' in scope
}
)
var doesNotWork: Thing
}
|