TL;DR: Issue 17658 [1] makes using shared very annoying/practically impossible.

Over the weekend, I attempted to fix Issue 15768 [2], which is an underlying problem in the std.stdio.File struct that stops it from closing properly when shared across threads. This is a big problem for users because stdin/out/err are all __gshared File, and are therefore unsafe. This makes for a really annoying situation where writeln("a") is @safe but stderr.writeln("a") isn't.

The obvious solution is to make stdin/out/err shared(File) and modify File to have shared overloads which either lock or use atomic ops safely. When I tried to write said functions, I ran into compilation issues that I couldn't diagnose until I ran across this thread by Atila [3]. The problem is, unlike a constructor, destructors and post-blits can't be overloaded with shared variants. Consider:

```
struct A
{
    this(string a) {}
    this(string a) shared {}

    ~this() {}
    ~this() shared {}

    this(this) {}
    this(this) shared {}
}

void main()
{
    shared f = A("");
}
```

Error: destructor f152.A.~this conflicts with destructor f152.A.~this at /d422/f152.d(6) Error: function f152.A.__postblit conflicts with function f152.A.__postblit at /d422/f152.d(9)

This is further compounded with this

```
struct A
{
        ~this() {}
}

void main()
{
        auto a = A();
        shared b = A();
}
```

Error: non-shared method f585.A.~this is not callable using a shared object

The only way to work around this is to create a new type that is defined as shared struct and copy over all of the code from the original type. This really hobbles shared in any real world context.

I ask that someone who knows the DMD code base could please take a look at this and see if this is something that can be fixed easily and without breakage.

[1] https://issues.dlang.org/show_bug.cgi?id=17658
[2] https://issues.dlang.org/show_bug.cgi?id=15768
[3] https://forum.dlang.org/post/[email protected]

Reply via email to