www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 2.058 broke my build. Is this a bug?

reply Caligo <iteronvexor gmail.com> writes:
----------------8<----------------8<----------------

import std.datetime : benchmark;
import std.stdio : writefln;

struct A(int r, int c){

 public:
  alias float[r * c] Data;

  Data _data;

  auto opBinary(A a){
    float t;
    foreach(i; 0..r*c)
      foreach(j; 0..r*c)
	t += this[i,j];
    return a;
  }
  pure float opIndex(size_t rr, size_t cc = 0) const{ return _data[cc
+ rr * c]; }

  pure ref float opIndex(size_t rr, size_t cc = 0){ return _data[cc + rr * c]; }
}

void bench(alias fun)(string msg, uint n = 1_000_000){

  auto b = benchmark!fun(n);
  writefln(" %s %s ms", msg, b[0].to!("msecs", int));
}

unittest{

  alias A!(3, 3) AA;
  AA a;

  bench!( {auto r = a * a;})("broken");
}

void main(){ }

---------------->8---------------->8----------------

Other parts of my code using bench() works fine, except in rare cases.
 So I'm guessing this is a bug?  I can't tell if it's in DMD or
std.datetime.  Can anyone help?

I will bug report myself tomorrow if it turns out to be a bug for sure.

DMD 2.058, 64-bit GNU/Linux
Feb 23 2012
next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 24.02.2012 10:28, Caligo wrote:
 ----------------8<----------------8<----------------

 import std.datetime : benchmark;
 import std.stdio : writefln;

 struct A(int r, int c){

   public:
    alias float[r * c] Data;

    Data _data;

    auto opBinary(A a){
      float t;
      foreach(i; 0..r*c)
        foreach(j; 0..r*c)
 	t += this[i,j];

I guess that should be foreach(i; 0..r) foreach(j; 0..c) t += this[i,j]; since you do row/col multiplication in opIndex?
      return a;
    }
    pure float opIndex(size_t rr, size_t cc = 0) const{ return _data[cc
 + rr * c]; }

    pure ref float opIndex(size_t rr, size_t cc = 0){ return _data[cc + rr *
c]; }
 }

 void bench(alias fun)(string msg, uint n = 1_000_000){

    auto b = benchmark!fun(n);
    writefln(" %s %s ms", msg, b[0].to!("msecs", int));
 }

 unittest{

    alias A!(3, 3) AA;
    AA a;

    bench!( {auto r = a * a;})("broken");
 }

 void main(){ }

 ---------------->8---------------->8----------------

 Other parts of my code using bench() works fine, except in rare cases.
   So I'm guessing this is a bug?  I can't tell if it's in DMD or
 std.datetime.  Can anyone help?

 I will bug report myself tomorrow if it turns out to be a bug for sure.

 DMD 2.058, 64-bit GNU/Linux

-- Dmitry Olshansky
Feb 24 2012
prev sibling next sibling parent Caligo <iteronvexor gmail.com> writes:
That was a typo, and it doesn't change anything.  Here is a shorter version:

----------------8<----------------8<----------------
import std.datetime;
import std.stdio;

struct A{

  auto fun(A a){ return 0; }
}

void bench(alias fun)(string msg, uint n = 1_000_000){

  auto b = benchmark!fun(n);
  writefln(" %s %s ms", msg, b[0].to!("msecs", int));
}

unittest{

  A a, b;

  void test1(){
    auto r = a.fun(b);
  }

  bench!( {auto r = a.fun(b);} )("Does Not work");
  bench!(test1)("Works");
}

void main(){ }
---------------->8---------------->8----------------



And here is the error:

/usr/include/d/dmd/phobos/std/datetime.d(30986): Error: safe function
'benchmark' cannot call system delegate '__lambda1'
t1.d(11): Error: template instance
t1.__unittest2.benchmark!(__lambda1) error instantiating
t1.d(23):        instantiated from here: bench!(delegate  system void()
{
int r = a.fun(b);
}
)
t1.d(23): Error: template instance t1.__unittest2.bench!(delegate  system void()
{
int r = a.fun(b);
}
) error instantiating
Feb 24 2012
prev sibling next sibling parent James Miller <james aatch.net> writes:
--f46d040891eb68b08804b9c0dc3f
Content-Type: text/plain; charset=UTF-8

On Feb 25, 2012 12:16 PM, "Caligo" <iteronvexor gmail.com> wrote:
 That was a typo, and it doesn't change anything.  Here is a shorter

 ----------------8<----------------8<----------------
 import std.datetime;
 import std.stdio;

 struct A{

  auto fun(A a){ return 0; }
 }

 void bench(alias fun)(string msg, uint n = 1_000_000){

  auto b = benchmark!fun(n);
  writefln(" %s %s ms", msg, b[0].to!("msecs", int));
 }

 unittest{

  A a, b;

  void test1(){
    auto r = a.fun(b);
  }

  bench!( {auto r = a.fun(b);} )("Does Not work");
  bench!(test1)("Works");
 }

 void main(){ }
 ---------------->8---------------->8----------------



 And here is the error:

 /usr/include/d/dmd/phobos/std/datetime.d(30986): Error: safe function
 'benchmark' cannot call system delegate '__lambda1'
 t1.d(11): Error: template instance
 t1.__unittest2.benchmark!(__lambda1) error instantiating
 t1.d(23):        instantiated from here: bench!(delegate  system void()
 {
 int r = a.fun(b);
 }
 )
 t1.d(23): Error: template instance t1.__unittest2.bench!(delegate  system

 {
 int r = a.fun(b);
 }
 ) error instantiating

Hmm it seems that the delegate is being implicitly marked as system, and im not sure why benchmark is safe. I'd say file a bug report. --f46d040891eb68b08804b9c0dc3f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <p>On Feb 25, 2012 12:16 PM, &quot;Caligo&quot; &lt;<a href=3D"mailto:itero= nvexor gmail.com">iteronvexor gmail.com</a>&gt; wrote:<br> &gt;<br> &gt; That was a typo, and it doesn&#39;t change anything. =C2=A0Here is a s= horter version:<br> &gt;<br> &gt; ----------------8&lt;----------------8&lt;----------------<br> &gt; import std.datetime;<br> &gt; import std.stdio;<br> &gt;<br> &gt; struct A{<br> &gt;<br> &gt; =C2=A0auto fun(A a){ return 0; }<br> &gt; }<br> &gt;<br> &gt; void bench(alias fun)(string msg, uint n =3D 1_000_000){<br> &gt;<br> &gt; =C2=A0auto b =3D benchmark!fun(n);<br> &gt; =C2=A0writefln(&quot; %s %s ms&quot;, msg, b[0].to!(&quot;msecs&quot;,= int));<br> &gt; }<br> &gt;<br> &gt; unittest{<br> &gt;<br> &gt; =C2=A0A a, b;<br> &gt;<br> &gt; =C2=A0void test1(){<br> &gt; =C2=A0 =C2=A0auto r =3D a.fun(b);<br> &gt; =C2=A0}<br> &gt;<br> &gt; =C2=A0bench!( {auto r =3D a.fun(b);} )(&quot;Does Not work&quot;);<br> &gt; =C2=A0bench!(test1)(&quot;Works&quot;);<br> &gt; }<br> &gt;<br> &gt; void main(){ }<br> &gt; ----------------&gt;8----------------&gt;8----------------<br> &gt;<br> &gt;<br> &gt;<br> &gt; And here is the error:<br> &gt;<br> &gt; /usr/include/d/dmd/phobos/std/datetime.d(30986): Error: safe function<= br> &gt; &#39;benchmark&#39; cannot call system delegate &#39;__lambda1&#39;<br=

&gt; t1.__unittest2.benchmark!(__lambda1) error instantiating<br> &gt; t1.d(23): =C2=A0 =C2=A0 =C2=A0 =C2=A0instantiated from here: bench!(de= legate system void()<br> &gt; {<br> &gt; int r =3D a.fun(b);<br> &gt; }<br> &gt; )<br> &gt; t1.d(23): Error: template instance t1.__unittest2.bench!(delegate sys= tem void()<br> &gt; {<br> &gt; int r =3D a.fun(b);<br> &gt; }<br> &gt; ) error instantiating</p> <p>Hmm it seems that the delegate is being implicitly marked as system, and= im not sure why benchmark is safe. I&#39;d say file a bug report.</p> --f46d040891eb68b08804b9c0dc3f--
Feb 24 2012
prev sibling next sibling parent Caligo <iteronvexor gmail.com> writes:
Is there another workaround than the one I've posted?

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

On Fri, Feb 24, 2012 at 8:44 PM, James Miller <james aatch.net> wrote:
 Hmm it seems that the delegate is being implicitly marked as system, and im
 not sure why benchmark is  safe. I'd say file a bug report.

Feb 24 2012
prev sibling parent Caligo <iteronvexor gmail.com> writes:
I found another workaround: mark the module as trusted.
Feb 24 2012