Thread overview
[Issue 11888] New: Incorrect behaviour taking slice from return value
Jan 10, 2014
Manu
Jan 10, 2014
Manu
Jan 10, 2014
Manu
January 10, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11888

           Summary: Incorrect behaviour taking slice from return value
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: turkeyman@gmail.com


--- Comment #0 from Manu <turkeyman@gmail.com> 2014-01-09 22:46:54 PST ---
So given a C-style function like this, that returns a pointer and length via
pointer argument:
   ubyte* test(size_t* len)
   {
     *len = 100;
     return cast(ubyte*)1234;
   }

Call it, but immediately use the size argument to slice a range:
   size_t size;
   ubyte[] t = test(&size)[0..size];

t is null.

If I break it into separate statements, it works:
   size_t size;
   ubyte* pt = test(&size);
   ubyte[] t = pt[0..size];

t.ptr = 1234, t.length = 100;

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 10, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11888


Manu <turkeyman@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical


--- Comment #1 from Manu <turkeyman@gmail.com> 2014-01-09 22:52:40 PST ---
I think invalid codegen deserves a higher priority...

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 10, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11888


monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra@gmail.com


--- Comment #2 from monarchdodra@gmail.com 2014-01-09 23:12:49 PST ---
(In reply to comment #0)
> So given a C-style function like this, that returns a pointer and length via
> pointer argument:
>    ubyte* test(size_t* len)
>    {
>      *len = 100;
>      return cast(ubyte*)1234;
>    }
> 
> Call it, but immediately use the size argument to slice a range:
>    size_t size;
>    ubyte[] t = test(&size)[0..size];
> 
> t is null.
> 
> If I break it into separate statements, it works:
>    size_t size;
>    ubyte* pt = test(&size);
>    ubyte[] t = pt[0..size];
> 
> t.ptr = 1234, t.length = 100;

Isn't this an issue mutating and using a parameter in a same "function"?

I don't know how the compiler rewrites slicing a pointer, but if you interpret it as a "3-argument function, you get":

slice(ptr, low, high);
eg:
slice(test(&size), 0, size);

Here, "size" is read as parameter 3, but also mutated as parameter 1.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 10, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11888



--- Comment #3 from monarchdodra@gmail.com 2014-01-09 23:15:27 PST ---
(In reply to comment #2)
> Isn't this an issue mutating and using a parameter in a same "function"?

Ah. I just stumbled on the thread. I see Andrei commented that: "Evaluation should proceed as if it were strictly left to right."

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 10, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=11888



--- Comment #4 from Manu <turkeyman@gmail.com> 2014-01-10 04:40:32 PST ---
(In reply to comment #2)
> (In reply to comment #0)
> > So given a C-style function like this, that returns a pointer and length via
> > pointer argument:
> >    ubyte* test(size_t* len)
> >    {
> >      *len = 100;
> >      return cast(ubyte*)1234;
> >    }
> > 
> > Call it, but immediately use the size argument to slice a range:
> >    size_t size;
> >    ubyte[] t = test(&size)[0..size];
> > 
> > t is null.
> > 
> > If I break it into separate statements, it works:
> >    size_t size;
> >    ubyte* pt = test(&size);
> >    ubyte[] t = pt[0..size];
> > 
> > t.ptr = 1234, t.length = 100;
> 
> Isn't this an issue mutating and using a parameter in a same "function"?
> 
> I don't know how the compiler rewrites slicing a pointer, but if you interpret it as a "3-argument function, you get":
> 
> slice(ptr, low, high);
> eg:
> slice(test(&size), 0, size);
> 
> Here, "size" is read as parameter 3, but also mutated as parameter 1.

If it's not to be fixed, it must at least be an error. This sort of undefined and unexpected behaviour isn't really acceptable.

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