[Issue 6869] New: Disallow array to pointer cast

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Oct 31 12:23:24 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6869

           Summary: Disallow array to pointer cast
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-10-31 12:22:10 PDT ---
This is a spinoff of bug 3990

In DMD 2.056 this code compiles:

void main() {
   int[] a1 = [5, 4, 3];
   int* p1 = cast(int*)a1; // no compile error here
}


Similar code using user-created struct doesn't compile:


struct Foo {
    int* p;
    size_t n;
}
void main() {
    Foo f;
    auto x = cast(int*)f; // compile error here
}


I don't see the need to accept this cast, because allowing the array to pointer
cast means introducing/leaving an useless special case, and there in practice
this special case is not useful because arrays have the ptr property:


struct Foo {
    int* p;
    size_t n;
}
void main() {
    Foo f;
    auto x = f.ptr; // OK
}


So I think cast(int*)a1 should be forbidden.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list