www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What's going on here?

reply Manu <turkeyman gmail.com> writes:
So I'm interacting with C (although it works the same in D), I call a
function that returns a pointer, and gives the size through an out arg:
  ubyte* test(size_t* ptr)
  {
*ptr = 100;
return cast(ubyte*)1234;
  }


And 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 do this, it works:
  size_t size;
  ubyte* pt = test(&size);
  ubyte[] t = pt[0..size];

Why should I need that extra line?
Jan 09 2014
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/9/14 9:08 PM, Manu wrote:
 So I'm interacting with C (although it works the same in D), I call a
 function that returns a pointer, and gives the size through an out arg:
    ubyte* test(size_t* ptr)
    {
 *ptr = 100;
 return cast(ubyte*)1234;
    }


 And 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 do this, it works:
    size_t size;
    ubyte* pt = test(&size);
    ubyte[] t = pt[0..size];

 Why should I need that extra line?

It's a bug in the compiler. Evaluation should proceed as if it were strictly left to right. So test(&size) must be called before size is loaded to construct the slice. Please report. Andrei
Jan 09 2014
next sibling parent Manu <turkeyman gmail.com> writes:
Reported.


On 10 January 2014 15:34, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org
 wrote:

 On 1/9/14 9:08 PM, Manu wrote:

 So I'm interacting with C (although it works the same in D), I call a
 function that returns a pointer, and gives the size through an out arg:
    ubyte* test(size_t* ptr)
    {
 *ptr = 100;
 return cast(ubyte*)1234;
    }


 And 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 do this, it works:
    size_t size;
    ubyte* pt = test(&size);
    ubyte[] t = pt[0..size];

 Why should I need that extra line?

It's a bug in the compiler. Evaluation should proceed as if it were strictly left to right. So test(&size) must be called before size is loaded to construct the slice. Please report. Andrei

Jan 09 2014
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 10 January 2014 at 05:34:27 UTC, Andrei Alexandrescu 
wrote:
 It's a bug in the compiler. Evaluation should proceed as if it 
 were strictly left to right. So test(&size) must be called 
 before size is loaded to construct the slice. Please report.

 Andrei

I remember this conversation popping up repeatedly. *Did* we ever make the choice to enforce this? I mean, is this part of the spec now? When did it happen? I seem to remember Walter was always against it.
Jan 09 2014
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, January 10, 2014 07:24:43 monarch_dodra wrote:
 On Friday, 10 January 2014 at 05:34:27 UTC, Andrei Alexandrescu
 
 wrote:
 It's a bug in the compiler. Evaluation should proceed as if it
 were strictly left to right. So test(&size) must be called
 before size is loaded to construct the slice. Please report.
 
 Andrei

I remember this conversation popping up repeatedly. *Did* we ever make the choice to enforce this? I mean, is this part of the spec now? When did it happen? I seem to remember Walter was always against it.

Walter has stated that he wanted to, but AFAIK, it never actually became official. But knowing how things tend to go around here, even if Walter, Andrei, and all of the main devs thought that it was official, it still probably wouldn't be in the spec. - Jonathan M Davis
Jan 11 2014
parent "Jakob Ovrum" <jakobovrum gmail.com> writes:
On Saturday, 11 January 2014 at 11:03:22 UTC, Jonathan M Davis 
wrote:
 Walter has stated that he wanted to, but AFAIK, it never 
 actually became
 official. But knowing how things tend to go around here, even 
 if Walter,
 Andrei, and all of the main devs thought that it was official, 
 it still
 probably wouldn't be in the spec.

 - Jonathan M Davis

It is in the spec[1]. [1] http://dlang.org/expression.html
Jan 11 2014