-
Notifications
You must be signed in to change notification settings - Fork 455
Description
Test code: the AutoEnvironmentKey branch of my fork of swift-macro-examples
Toolchain: the latest nightly, swift-DEVELOPMENT-SNAPSHOT-2023-02-15-a in Xcode 14.2 (14C18) on macOS 13.2.1 (22D68).
SE-0389, Attached Macros, says
The expansion of an accessor macro that does not specify one of
willSet
ordidSet
in its list of names must result in a computed property. A side effect of the expansion is to remove any initializer from the stored property itself; it is up to the implementation of the accessor macro to either diagnose the presence of the initializer (if it cannot be used) or incorporate it in the result.
The AutoEnvironmentKey
macro in the linked repo is an accessor macro that does not specify either willSet
or didSet
, and indeed it returns get
and set
accessors in its expansion. But swiftc does not remove the initializer. The testAutoEnvironmentKey
test case demonstrates this. It uses this input syntax:
extension EnvironmentValues {
@AutoEnvironmentKey
public var myValue: String = "don't abuse macros"
}
and expects this output syntax:
extension EnvironmentValues {
public var myValue: String {
get { self[EnvironmentKey_for_myValue.self] }
set { self[EnvironmentKey_for_myValue.self] = newValue }
}
private enum EnvironmentKey_for_myValue: EnvironmentKey {
static var defaultValue: String { "don't abuse macros" }
}
}
but the actual expanded syntax is this:
extension EnvironmentValues {
public var myValue: String = "don't abuse macros" {
get { self[EnvironmentKey_for_myValue.self] }
set { self[EnvironmentKey_for_myValue.self] = newValue }
}
private enum EnvironmentKey_for_myValue: EnvironmentKey {
static var defaultValue: String { "don't abuse macros" }
}
}
Note that the initializer has not been removed from the actual expanded syntax.