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
Fix Issue 15604 - std.array.array of structs with template opAssign and default initialised 'new'ed class member #3952
Conversation
Thanks for your pull request, @John-Colvin! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Some tips to help speed things up:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla references
|
6f223f9
to
94159d3
Compare
@@ -3967,7 +3967,7 @@ private void emplaceInitializer(T)(ref T chunk) @trusted pure nothrow | |||
else | |||
{ | |||
import core.stdc.string : memcpy; | |||
static immutable T init = T.init; | |||
static immutable T init;// = T.init; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails for types T
with @disable this();
, which seems to be the cause of the auto-tester failure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use TypeInfo.init
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeInfo.init
is deprecated and should be replaced byTypeInfo.initializer
in all code that try to use it.TypeInfo.
... is not a good idea for such a core functuonality such as emplace, because it is not usable fromversion (FreeStanding)
code.T init = T.init
is better thanstatic immutable
, because:static immutable
keeps objects alive for ever.- stack allocated data is more cache friendly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the other hand, stack allocated structs are not allowed if they have @disable ~this();
, so maybe we need to use static immutable
at least for such types, probably under static if
...
Why would this even be supposed to work in the first place? Isn't it clearly an accepts-invalid compiler issue that you can have a mutable member initialized to point to a class instance? Each struct instance would need to be initialized to point to a different object! If it were a static member, we could discuss whether it would make sense to put the class instance into TLS to support that use case, but not for per-object data. Am I missing something here? What does the original use case look like? |
@klickverbot, member fields have compile-time initializers, and where but global/TLS would such data go? Putting them in global memory and TLS is consistent with every other compile-time initialized variable. When the programmer wants runtime initialization of fields, they can use a constructor. When support for class types was initially added to compile-time initialized variables, it allowed all class types regardless of type qualifiers despite always allocating them in global memory. Since then, it has been made a compilation error to use compile-time initializers for mutable and unshared variables of class type at module-level and function scope. I assume the patch missed member fields. |
I'd be happy to see this fixed at a language level, preferably in some way that disallows the madness of the test-case I'm using in this pull request. |
Shall we close this and issue 15604 as invalid, then? |
happy to close this, but I think 15604 should stay open, some sort of fix is necessary, whether it's documentation or better error messages or something more fundamental. |
What do you think of refiling it as accepts-invalid (for mutable references to the data section it generates)? |
15604 is indicative of a larger problem, but this works around the issue here.