[Issue 24772] New: Casting class references to void* should be @safe

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Sep 19 12:36:52 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=24772

          Issue ID: 24772
           Summary: Casting class references to void* should be @safe
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: safe
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: ogion.art at gmail.com

Any pointer implicitly converts to void*. This is considered safe. Class
references are essentially pointers, but converting them to void* requires
implicit cast. This makes this conversion illegal in safe code, even though it
doesn’t violate memory safety. 

class C {}
interface I {}

C c;
I i;

@safe void main()
{ 
    auto cp = cast(void*)c; // Error: cast from `app.C` to `void*` not allowed
in safe code
    auto ip = cast(void*)i; // Error: cast from `app.I` to `void*` not allowed
in safe code
}

--


More information about the Digitalmars-d-bugs mailing list