Files
swift-mirror/test/Macros/accessor_macros.swift
Doug Gregor 65a9bff5b0 Rework mangling of macro expansions in local contexts to not trigger type checking.
The mangling of macro expansions relies on having a type-checked AST
for its enclosing context. When that enclosing context is within a
local context (say, a local type), mangling would trigger type
checking of that local type, which could then involve assigning local
discriminators. However, if this happens before type checking of the
enclosing function body, we would end up failing to assign closure
discriminators to (e.g.) autoclosures within the body.

The fundamental problem here is the interaction between discriminator
assignment (which can only happen after type checking) and mangling of
macro expansion buffers (which can happen during that type checking).
Break this cycle by providing a different approach to mangling macro
expansions within local contexts as the innermost non-local context +
a name-based discriminator within that local context. These manglings
are not ABI and are not stable, so we can adjust them later if we come
up with a scheme we like better. However, by breaking this cycle, we
eliminate assertions and miscompiles that come from missing
discriminators in this case.

Fixes rdar://139734958.
2024-12-05 16:09:02 -08:00

210 lines
6.4 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
// Check for expected errors.
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -DTEST_DIAGNOSTICS -verify-ignore-unknown
// RUN: not %target-swift-frontend -typecheck -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -DTEST_DIAGNOSTICS %s > %t/diags.txt 2>&1
// RUN: %FileCheck -check-prefix=CHECK-DIAGS %s < %t/diags.txt
// 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")
@attached(accessor)
macro myPropertyWrapperSkipsComputed() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperSkipsComputedMacro")
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)
var _favoriteColor: MyWrapperThingy<String> = .init(storage: "Blue")
@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: }
@myPropertyWrapperSkipsComputed
var age: Int? {
get { nil }
}
@myPropertyWrapper
var favoriteColor: String {
didSet { fatalError("Boom") }
}
}
// 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"
// CHECK-NEXT: Setting value Yellow
ms.favoriteColor = "Yellow"
#if TEST_DIAGNOSTICS
struct MyBrokenStruct {
var _birthDate: MyWrapperThingy<Date?> = .init(storage: nil)
// expected-note@+1 2{{in expansion of macro 'myPropertyWrapper' on property 'birthDate' here}}
@myPropertyWrapper
var birthDate: Date? {
// CHECK-DIAGS: variable already has a getter
// CHECK-DIAGS: in expansion of macro
// CHECK-DIAGS: previous definition of getter here
get { fatalError("Boom") }
// expected-note @-1{{previous definition of getter here}}
// CHECK-DIAGS: variable already has a setter
// CHECK-DIAGS: in expansion of macro
// CHECK-DIAGS: previous definition of setter here
set { fatalError("Boom") }
// expected-note @-1{{previous definition of setter here}}
}
}
// expected-error@+1{{'accessor' macro cannot be attached to struct ('CannotHaveAccessors')}}
@myPropertyWrapper
struct CannotHaveAccessors {}
// CHECK-DIAGS: 'accessor' macro cannot be attached to struct ('CannotHaveAccessors')
#endif
@attached(accessor, names: named(willSet))
macro SkipsComputed() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperSkipsComputedMacro")
struct HasComputed {
@SkipsComputed
var value: Int { 17 }
}
@attached(accessor, names: named(willSet))
macro AddWillSet() =
#externalMacro(module: "MacroDefinition", type: "WillSetMacro")
@attached(accessor)
macro AddWillSetSneakily() =
#externalMacro(module: "MacroDefinition", type: "WillSetMacro")
@attached(accessor, names: named(willSet))
macro MakeComputedSneakily() =
#externalMacro(module: "MacroDefinition", type: "PropertyWrapperMacro")
struct HasStoredTests {
@AddWillSet var x: Int = 0
#if TEST_DIAGNOSTICS
@AddWillSetSneakily var y: Int = 0
// expected-error@-1{{expansion of macro 'AddWillSetSneakily()' did not produce a non-observing accessor (such as 'get') as expected}}
@MakeComputedSneakily var z: Int = 0
// expected-error@-1{{expansion of macro 'MakeComputedSneakily()' produced an unexpected getter}}
// expected-note@-2 2{{in expansion of macro}}
// expected-note@-3 2{{'z' declared here}}
#endif
}
#if TEST_DIAGNOSTICS
struct MultipleVars {
@AddWillSet var (x, y): (Int, Int) = (0, 0)
// expected-error@-1 2{{accessor macro 'AddWillSet()' can only apply to a single variable}}
}
#endif
@attached(accessor)
macro addGetterMacro() =
#externalMacro(module: "MacroDefinition", type: "AddGetterMacro")
#if TEST_DIAGNOSTICS
struct S {
@addGetterMacro let x: Int
// expected-warning@-1 {{cannot expand accessor macro on variable declared with 'let'; this is an error in the Swift 6 language mode}}
}
#endif
func acceptAutoclosure(_ success: @autoclosure () -> Bool, message: @autoclosure () -> String) {
}
@attached(accessor)
macro BigEndianAccessorMacro() = #externalMacro(module: "MacroDefinition", type: "BigEndianAccessorMacro")
func testLocalWithAutoclosure(x: Int, y: Int) {
struct Local {
var __value: Int = 0
// CHECK-DUMP: @__swiftmacro_15accessor_macros9value_$l022BigEndianAccessorMacrofMa_.swift
@BigEndianAccessorMacro
var value: Int
}
acceptAutoclosure(x == y, message: "they better be the same")
let local = Local(__value: 5)
acceptAutoclosure(x + 1 == local.__value, message: "they better be the same")
if x == y {
struct Nested {
struct Local {
var __value: Int = 0
// CHECK-DUMP: @__swiftmacro_15accessor_macros9value_$l122BigEndianAccessorMacrofMa_.swift
@BigEndianAccessorMacro
var value: Int
}
}
}
}