New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-12202] Property wrappers initialization is somehow artificially restricted for State
#54627
Comments
Weird, looking at the generated SILGen'd output it seems like the backing property's initializer is not being called, before the setter is called. |
Okay, after digging a bit, it seems like this isn't a bug. It's because the It's also not tied to @propertyWrapper
struct Foo<T> {
private var _value: T
init(wrappedValue value: T) {
_value = value
}
var wrappedValue: T {
get { return _value }
nonmutating set { fatalError("unimplemented") }
}
}
struct Bar {
@Foo var value: Int
init(value: Int) {
self.value = value
}
} although I suppose the diagnostics could be a bit better. |
Isn‘t this the other way around then? How is the setter even related to this as we still need to initialize the property wrapper first and therefore substitute the whole line. The initialization isn‘t really going through the computed property. |
I mean, this rule says nothing about the setter kind. https://forums.swift.org/t/why-does-this-property-wrapper-code-compile/31931/6 |
Okay it also seems to happen if `wrappedValue` is get-only, but this rule isn‘t about that but rather about initialization similar to like out of line initialization but from within the init context. |
@swift-ci create |
This is due to an artificial limitation in the compiler's implementation of property wrappers. The workaround is to initialize the backing storage directly via init(value: Int) {
self._value = State(wrappedValue: value)
} |
This should be fixed by #35218 |
This is fixed. Could you please verify using the latest main development snapshot from https://swift.org/download/#snapshots ? Thank you! |
Environment
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
Additional Detail from JIRA
md5: 8aaf5e4f262a6c145f3c7cad19fdf8c6
Issue Description:
This general property wrapper works as expected:
This one from SwiftUI doesn't for some reason:
Expected behavior is that the compiler substitute the above line like with every other property wrapper, which would be `self._value = State(wrappedValue: value)`.
The text was updated successfully, but these errors were encountered: