[Issue 10625] New: Compiler should warn or disallow using slice syntax in initialization

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 12 09:57:23 PDT 2013


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

           Summary: Compiler should warn or disallow using slice syntax in
                    initialization
           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: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-07-12 09:57:21 PDT ---
-----
void main()
{
    int[] source = [0, 1];

    int[] arr1 = new int[](2);
    arr1[] = source[];

    assert(arr1 !is source);  // ok

    int[] arr2 = source[];  // looks like a copy
    assert(arr2 !is arr2);  // assert fails: but in fact it's not!
}
-----

The 'int[] arr2 = source[];' syntax appears as though the source contents are
copied into arr2, but in fact this is the same code as 'int[] arr2 = source;'.

Since [] has a very special meaning, the above should either:

1) Become an actual deep copy, meaning arr2 would have to allocate space first
and then copy contents. This would be a breaking and negative change due to
performance implications.

2) Not compile. It looks like a deep copy but it isn't, and this can cause
issues down the road (for example using memcpy or even OpenGL functions can
create hard to track problems due to using the source and target arrays which
point to the same memory).

I'd vote heavily towards 2.

Of course one can always use 'int[] arr2 = source.dup'.

-- 
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