std.typecons: make Nullable payload void initialized#8219
std.typecons: make Nullable payload void initialized#8219RazvanN7 merged 1 commit intodlang:masterfrom
Conversation
|
Thanks for your pull request and interest in making D better, @ljmf00! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#8219" |
|
Why? |
Because this payload only gets valid when assigned and the assignment only occurs when it is not null. Therefore, it doesn't necessarily need to be initialized. It still needs to be initialized in order to be This can be beneficial and have a real-world impact on beefy structs or static arrays. |
This can be fully void initialized if the assignment of void initialized memory doesn't override it, although I'm not totally sure about that, and doubt it. |
|
I can come up with an idea to fully void initialize this safely and discuss |
|
This will not achieve the desired effect. Because there is at least one non-void member, the entire struct will get an initializer value, and the entire struct will be initialized to set the value of the non-void member Not only that, but the |
Can you prove me with a specification point? I think that behaviour is implementation-defined, although didn't find anything in there. If there is no point talking about that, I assume the default behaviour of the
What do you mean here? The destructor is never called as it is wrapped in a // the lifetime of the value in copy shall be managed by
// this Nullable, so we must avoid calling its destructor.
auto copy = DontCallDestructorT(value);If this is not what you mean, can you elaborate on that with an example, please? |
|
The spec does not say how types with partial void initializers are initialized. True, one could implement a compiler that only initializes the non void parts. But that's not what happens now. But at least Andrei agrees that the current implementation is substandard (while also acknowledging the "spec" of TypeInfo requires the current implementation): https://issues.dlang.org/show_bug.cgi?id=21277
I actually misread this note: if (_isNull)
{
// trusted since payload is known to be T.init here.
() @trusted { moveEmplace(copy.payload, _value.payload); }();
}I didn't realize moveEmplace would not call the destructor. So you are right on that point. Perhaps change the note. The code inside the assign operator is confusing for sure (why make a copy to avoid destructing the value, when the value's destructor will already run?). I have the feeling that this can be done in a simpler way. But this PR is not about that. |
a7a76c4 to
ee6faf5
Compare
Arguing that this shouldn't be a thing because currently it is implemented in a way it is not described in the specification is not correct.
I changed.
This is to efficiently move memory to _value.payload reference as it is known to be uninitialized, and therefore no need to call the destructor although I'm not aware of how this can be done in a better way. |
This is a workaround introduced in PR #6619 to deal with types whose destructor cannot be called on |
Signed-off-by: Luís Ferreira <contact@lsferreira.net>
ee6faf5 to
d19c437
Compare
|
Commit message changed to comply with the contributions guidelines. |
Thanks for clarifying this! Anyway, this enters a bit out of the scope of this PR. |
Signed-off-by: Luís Ferreira contact@lsferreira.net