Bug 18474 – Postblit not working in shared structs

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-02-20T16:16:36Z
Last change time
2018-11-17T12:56:08Z
Assigned to
No Owner
Creator
Eduard Staniloiu

Comments

Comment #0 by edi33416 — 2018-02-20T16:16:36Z
The postblit for shared struct is not working as expected, as it is not infering the `shared` attribute. For the snippet bellow shared struct A { this(this); } struct B { A a; } void main() { shared B b1; auto b2 = b1; } the compiler gives the following error: Error: non-shared method test.B.__fieldPostblit is not callable using a shared object The current workaround for this is to define `A` s postblit as this(shared this);
Comment #1 by issues.dlang — 2018-02-22T06:09:05Z
I don't think that this is actually a bug. shared isn't inferred by anything let alone postlbit constructors, and fundamentally, the same code does not work as both thread-local and shared. And I don't know how safe copying is with shared in general. The copy needs to be thread-safe for it to work properly, but there are no mutexes involved when the struct is blitted. A mutex could then be used in the postblit constructor to protect it, but that gets a bit funny, because really, the it needs to be a mutex shared with the struct being copied in order to properly protect the shared reference types, and once the blitting has happened, you're mutating a copy. But fundamentally, I don't know that blitting the struct can even work properly with shared, since that needs to either be atomic or use a mutex, and AFAIK, it does neither. Really, I think that copying structs is one of those areas where shared has not been fleshed out as well as it needs to be.
Comment #2 by dfj1esp02 — 2018-02-22T09:42:54Z
Some shared objects can be copied just fine like mutex itself or FILE*, but they don't need much of postblit. And if shared postblit is unlikely to work, you might want to @disable this(shared this) shared;
Comment #3 by razvan.nitu1305 — 2018-03-28T15:01:02Z
PR : https://github.com/dlang/dmd/pull/8098 Also, if someone is interested, I documented the (bugish) behavior of the postblit: https://dlang.org/spec/struct.html#struct-postblit
Comment #4 by github-bugzilla — 2018-04-02T15:57:51Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/3a0f75d959bd8e875a7d7767caed8ae3fa94ee8e Fix Issue 18474 - Postblit not working in shared structs https://github.com/dlang/dmd/commit/e1aaf4ce8e9df63e383b60b2891051d633a63d76 Merge pull request #8098 from RazvanN7/Issue_18474 Fix Issue 18474 - Postblit not working in shared structs merged-on-behalf-of: Andrei Alexandrescu <[email protected]>
Comment #5 by github-bugzilla — 2018-11-17T12:56:08Z
Commits pushed to master at https://github.com/dlang/dlang.org https://github.com/dlang/dlang.org/commit/bd8bc44ca834bde8488b8c551b143bd5e25e55ba Spec change for Issue 18474 https://github.com/dlang/dlang.org/commit/866ad4992551dd9c38feb4cb58ffdd098bd2ff10 Merge pull request #2325 from RazvanN7/update_18474 Spec change for Issue 18474 merged-on-behalf-of: Nicholas Wilson <[email protected]>