www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Semantics of casting **to** shared?

reply dsimcha <dsimcha yahoo.com> writes:
I noticed that the following code compiles even in SafeD.  Is this a bug?  If
not, what are the semantics of casting unshared to shared?  TDPL doesn't seem
to mention this.  Isn't it dangerous to have shared and unshared references to
the same memory?

 safe:
class Foo {
    uint num;
}

void main() {
    auto foo = new Foo;
    auto bar = cast(shared) foo;
}
Sep 04 2010
next sibling parent reply Jason House <jason.james.house gmail.com> writes:
safeD doesn't mean your code is safe... only "memory safe". Similarly, "shared"
sort of partitions data and there is no provably safe way to create or use it.
I have no idea if your example is truly legit safeD or not...

dsimcha Wrote:

 I noticed that the following code compiles even in SafeD.  Is this a bug?  If
 not, what are the semantics of casting unshared to shared?  TDPL doesn't seem
 to mention this.  Isn't it dangerous to have shared and unshared references to
 the same memory?
 
  safe:
 class Foo {
     uint num;
 }
 
 void main() {
     auto foo = new Foo;
     auto bar = cast(shared) foo;
 }
Sep 04 2010
parent dsimcha <dsimcha yahoo.com> writes:
== Quote from Jason House (jason.james.house gmail.com)'s article
 safeD doesn't mean your code is safe... only "memory safe". Similarly, "shared"
sort of partitions data and there is no provably safe way to create or use it. I have no idea if your example is truly legit safeD or not... 1. Using __gshared and casting **away** shared are illegal in SafeD. 2. Low level data races can cause memory corruption if pointers get overwritten with invalid data.
Sep 04 2010
prev sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Sat, 04 Sep 2010 22:05:05 +0400, dsimcha <dsimcha yahoo.com> wrote:

 I noticed that the following code compiles even in SafeD.  Is this a  
 bug?  If
 not, what are the semantics of casting unshared to shared?  TDPL doesn't  
 seem
 to mention this.  Isn't it dangerous to have shared and unshared  
 references to
 the same memory?

  safe:
 class Foo {
     uint num;
 }

 void main() {
     auto foo = new Foo;
     auto bar = cast(shared) foo;
 }
I loved the recent clone() idea. I think it might be better to disable casting to (and from) shared (and immutable) and make a (deep?) copy of an object instead. This will make sure that the isn't any dangling reference left. I strongly believe dangling references could become a major source of bugs which might be hard to find and fix.
Sep 04 2010
parent dsimcha <dsimcha yahoo.com> writes:
== Quote from Denis Koroskin (2korden gmail.com)'s article
 I loved the recent clone() idea. I think it might be better to disable
 casting to (and from) shared (and immutable) and make a (deep?) copy of an
 object instead. This will make sure that the isn't any dangling reference
 left. I strongly believe dangling references could become a major source
 of bugs which might be hard to find and fix.
Holy %)(*$%, casting **to** immutable works in SafeD, too? Of course casting from/to shared/immutable should be allowed in non- safe code, but I don't think either should be allowed in safe code. The only way to get aliasing between mutable and immutable data in safe code should be by using broken trusted code. Otherwise the safe-ty of SafeD is really questionable. Also, as far as cloning, The fact that it's really a special case of serialization was brought up by a few people, so I'm taking a wait-and-see attitude and seeing what people who wrote serialization libraries come up w/ for cloning, rather than working on cloning now.
Sep 04 2010