-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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-6667] Use of WritableKeyPath on self crashes inside init method unless self is already initialized #49216
Comments
Since the key path doesn't know that the value is uninitialized, that probably means most of this can't work. At the very least, though, it shouldn't crash. @jckarter, thoughts? Agreement, disagreement? |
Sounds like DI analysis is failing to catch this. This should be a compile-time error, since you can't use `self` freely until it's fully initialized. |
@swift-ci create |
Well, even once it is fully initialized, you won't be able to execute this properly, right? |
Why not? Once `self` is fully statically initialized, it can be used like any other value. |
Yes, but |
Ah, I missed that detail. We should also not allow |
Fix for master: #15808 |
Fix for 4.2: #15826 |
Merged. This should be fixed in future snapshots. |
Additional Detail from JIRA
md5: 8b972e84b863fb3a33708f56175eb7bb
relates to:
Issue Description:
The use of WritableKeyPath on self crashes inside an init method unless self is already initialized. Put this very simplified example in the playground:
struct Foo {
let bar: Bool
init() {
let path: WritableKeyPath<Foo, Bool> = \Foo.bar // this works
self[keyPath: path] = true // crashes
}
}
let foo = Foo()
Of course our real use-case scenario is nothing similar to this basic example; we are passing arrays of key paths into the init method and iterating through them. It's very awesome but it seems non-ideal to have to specify a default value for every property (or provide a prototype to clone from).
Using var instead of let here does not help.
If ANY of the properties don't have a default value, then ALL of the WritableKeyPaths will crash – even ones that point to properties that do have default values. Also, since there is no way using reflection to determine at runtime which properties have default values and which don't, it doesn't seem like there is a workaround other than giving every property a default value.
This bug prohibits using "let" for values that you want to init with a keypath. Alone this makes it a really bad bug forcing bad compromises when using keypaths in functional programming patterns.
I hope we can find a solution to use WritableKeyPath during init. I'm not sure why it would care that the value is not initialized since we're inside an init method and therefore we should not expect it to be initialized yet.
The text was updated successfully, but these errors were encountered: