Files
swift-mirror/test/Macros/accessor_macros.swift
Doug Gregor a23d39bdfb [Macros] Mangle attached macro expansions based only on syntactic information
The mangling of attached macro expansions based on the declaration to
which they are attached requires semantic information (specifically,
the interface type of that declaration) that caused cyclic
dependencies during type checking. Replace the mangling with a
less-complete mangling that only requires syntactic information from
the declaration, i.e., the name of the declaration to which the macro
was attached.

This eliminates reference cycles that occur with attached macros that
produce arbitrary names.
2023-04-11 23:40:28 -04:00

75 lines
2.3 KiB
Swift

// 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
// First check for no errors.
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition)
// Check that the expansion buffer are as expected.
// RUN: %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) %s -dump-macro-expansions > %t/expansions-dump.txt 2>&1
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
// Execution testing
// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
@attached(accessor)
macro myPropertyWrapper() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperMacro")
struct Date { }
struct MyWrapperThingy<T> {
var storage: T
var wrappedValue: T {
get {
print("Getting value \(storage)")
return storage
}
set {
print("Setting value \(newValue)")
storage = newValue
}
}
}
struct MyStruct {
var _name: MyWrapperThingy<String> = .init(storage: "Hello")
var _birthDate: MyWrapperThingy<Date?> = .init(storage: nil)
@myPropertyWrapper
var name: String
// CHECK-DUMP: @__swiftmacro_15accessor_macros8MyStructV4name17myPropertyWrapperfMa_.swift
// CHECK-DUMP: get {
// CHECK-DUMP: _name.wrappedValue
// CHECK-DUMP: }
// CHECK-DUMP: set {
// CHECK-DUMP: _name.wrappedValue = newValue
// CHECK-DUMP: }
@myPropertyWrapper
var birthDate: Date?
// CHECK-DUMP: @__swiftmacro_15accessor_macros8MyStructV9birthDate17myPropertyWrapperfMa_.swift
// CHECK-DUMP: get {
// CHECK-DUMP: _birthDate.wrappedValue
// CHECK-DUMP: }
// CHECK-DUMP: set {
// CHECK-DUMP: _birthDate.wrappedValue = newValue
// CHECK-DUMP: }
}
// Test that the fake-property-wrapper-introduced accessors execute properly at
// runtime.
var ms = MyStruct()
// CHECK: Getting value Hello
_ = ms.name
// CHECK-NEXT: Setting value World
ms.name = "World"