An Issue I Wish To Raise Awareness On

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 24 13:30:25 PDT 2017


On Monday, July 24, 2017 2:30:01 PM MDT Atila Neves via Digitalmars-d wrote:
> >> This is fine. What dmd does now is strip shared off of the
> >> `this` pointer, not the member variables. There's only a
> >> problem if the sharedness of the member variable(s) depends on
> >> sharedness of the enclosing object.
> >
> > What happens with something like
> >
> > struct S
> > {
> >
> >     Foo* _foo;
> >
> >     ~this() {...}
> >
> > }
> >
> > shared S s;
> >
> > Inside the destructor, is what _foo points to still treated as
> > shared: shared(Foo)*?
>
> No. This is what I meant by the sharedness depening on the
> enclosing object. However, there's a workaround:
>
> struct Foo { }
>
>
> struct S {
>
>      Foo* _foo;
>      bool _isShared;
>
>      this(this T, U)(U foo) if(is(T == shared) && is(U ==
> shared(Foo)*) || !is(T == shared) && is(U == Foo*)) {
>          static if(is(T == shared)) _isShared = true;
>          _foo = foo;
>      }
>
>      ~this() {
>          import std.stdio: writeln;
>          _isShared ? writeln("shared dtor") : writeln("non-shared
> dtor");
>      }
> }
>
> void main() {
>      auto f = Foo();
>      auto sf = shared Foo();
>      auto s = S(&f);
>      auto ss = shared S(&sf);
> }
>
>
> It's annoying to use that bool up memory-wise, but I assume it's
> not a big deal for most applications.
>
> In any case, that example wouldn't have worked anyway before my
> change to dmd - even creating the S struct would've been a
> compiler error.

The problem with this is that this means that shared is not being properly
enforced by the compiler. Your workaround is a way for the programmer to
figure out if the object is shared and do something differently based on
that, but for the compiler to do what it's supposed to be doing with shared
(e.g. prevent non-atomic operations), any indirections in the member
variables must continue to be typed as shared inside the destructor, and
that's clearly not happening right now, which is a serious problem IMHO. The
situation may be better thanks to your changes in that some stuff is now
possible that should be possible and was not before, but it's not completely
sound as far as the type system goes, and we really should be fixing it so
that shared is properly enforced rather than just blindly stripped off.

- Jonathan M Davis



More information about the Digitalmars-d mailing list