www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What's going on here?

reply Manu <turkeyman gmail.com> writes:
--089e0141ab82ff9ff004ef96b5d5
Content-Type: text/plain; charset=UTF-8

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?

--089e0141ab82ff9ff004ef96b5d5
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div>So I&#39;m interacting with C (although it works=
 the same in D), I call a function that returns a pointer, and gives the si=
ze through an out arg:</div><div>=C2=A0 ubyte* test(size_t* ptr)</div><div>=
=C2=A0 {</div>
<div><span class=3D"" style=3D"white-space:pre">    </span>*ptr =3D 100;</d=
iv><div><span class=3D"" style=3D"white-space:pre">    </span>return cast(u=
byte*)1234;</div><div>=C2=A0 }</div></div><div><br></div><div><br></div><di=
v>And call it, but immediately use the size argument to slice a range:</div=

[0..size];</div><div><br></div><div>t is null.</div><div><br></div><div>If = I do this, it works:</div><div><div>=C2=A0 size_t size;<br></div><div>=C2= =A0 ubyte* pt =3D test(&amp;size);</div> <div>=C2=A0 ubyte[] t =3D pt[0..size];</div></div><div><br></div><div>Why s= hould I need that extra line?</div></div> --089e0141ab82ff9ff004ef96b5d5--
Jan 09 2014
next sibling parent 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
prev sibling next sibling parent Manu <turkeyman gmail.com> writes:
--047d7bd767202e4c3404ef9818e1
Content-Type: text/plain; charset=UTF-8

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

--047d7bd767202e4c3404ef9818e1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Reported.</div><div class=3D"gmail_extra"><br><br><div cla= ss=3D"gmail_quote">On 10 January 2014 15:34, Andrei Alexandrescu <span dir= =3D"ltr">&lt;<a href=3D"mailto:SeeWebsiteForEmail erdani.org" target=3D"_bl= ank">SeeWebsiteForEmail erdani.org</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div class=3D"HOEnZb"><div class=3D"h5">On 1= /9/14 9:08 PM, Manu wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> So I&#39;m interacting with C (although it works the same in D), I call a<b= r> function that returns a pointer, and gives the size through an out arg:<br> =C2=A0 =C2=A0ubyte* test(size_t* ptr)<br> =C2=A0 =C2=A0{<br> *ptr =3D 100;<br> return cast(ubyte*)1234;<br> =C2=A0 =C2=A0}<br> <br> <br> And call it, but immediately use the size argument to slice a range:<br> =C2=A0 =C2=A0size_t size;<br> =C2=A0 =C2=A0ubyte[] t =3D test(&amp;size)[0..size];<br> <br> t is null.<br> <br> If I do this, it works:<br> =C2=A0 =C2=A0size_t size;<br> =C2=A0 =C2=A0ubyte* pt =3D test(&amp;size);<br> =C2=A0 =C2=A0ubyte[] t =3D pt[0..size];<br> <br> Why should I need that extra line?<br> </blockquote> <br></div></div> It&#39;s a bug in the compiler. Evaluation should proceed as if it were str= ictly left to right. So test(&amp;size) must be called before size is loade= d to construct the slice. Please report.<span class=3D"HOEnZb"><font color= =3D"#888888"><br> <br> Andrei<br> <br> </font></span></blockquote></div><br></div> --047d7bd767202e4c3404ef9818e1--
Jan 09 2014
prev sibling next sibling parent "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
prev sibling next sibling parent 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
prev sibling 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