www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - strage heisenbug (has scoped destruction, cannot build closure)

reply ketmar <ketmar ketmar.no-ip.org> writes:
hi.

the following (manually "dustmited" ;-)) code gives the error from subj=20
on git HEAD:

=3D=3D=3D ztest.d =3D=3D=3D
  module ztest;

  auto streamAsRange(STP) (STP st) {
    static struct StreamRange(ST) {
    private:
      ST strm;
    public:
      void put (const(ubyte)[] data) { strm.rawWrite(data); }
    }
    return StreamRange!STP();
  }

  void zcompress(RO) (RO ro) {
    ubyte[1] obuf;

    void writeOBuf () {
      static if (is(typeof(() {
        ubyte[2] b;
        ro.put(b);
      }))) {
        ro.put(obuf);
      }
    }

    writeOBuf();
  }

  void test () {
    import std.stdio;
    auto fo =3D File();
    zcompress(streamAsRange(fo));
  }

  void main () {
  }
=3D=3D=3D=3D=3D=3D

the strange thing is that this is heisenbug. if i comment out `static if`=20
the module successfully compiles. i.e. changing `zcompress()` to this=20
allows me to compile the code:

=3D=3D=3D
  void zcompress(RO) (RO ro) {
    ubyte[1] obuf;

    void writeOBuf () {
      ro.put(obuf);
    }

    writeOBuf();
  }
=3D=3D=3D

so the offending line is `ro.put(b);` (comment it out, and everything is=20
working again).

am i doing something very wrong here, or this is really a compiler bug?=
Mar 02 2015
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
p.s. well, it's not a heisenbug, 'cause it reproducible. let's say that=20
it's a... very strange bug. ;-)=
Mar 02 2015
prev sibling next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
p.p.s. sure, i can see where i'm creating a closure. but i can't see how=20
that closure got to IR generator, as it is never executes, there is no=20
need to generate code for it, and it should be removed after checking=20
`static if` condition.=
Mar 02 2015
prev sibling parent "anonymous" <anonymous example.com> writes:
On Tuesday, 3 March 2015 at 07:26:13 UTC, ketmar wrote:
 hi.

 the following (manually "dustmited" ;-)) code gives the error 
 from subj
 on git HEAD:

 === ztest.d ===
   module ztest;

   auto streamAsRange(STP) (STP st) {
     static struct StreamRange(ST) {
     private:
       ST strm;
     public:
       void put (const(ubyte)[] data) { strm.rawWrite(data); }
     }
     return StreamRange!STP();
   }

   void zcompress(RO) (RO ro) {
     ubyte[1] obuf;

     void writeOBuf () {
       static if (is(typeof(() {
         ubyte[2] b;
         ro.put(b);
       }))) {
         ro.put(obuf);
       }
     }

     writeOBuf();
   }

   void test () {
     import std.stdio;
     auto fo = File();
     zcompress(streamAsRange(fo));
   }

   void main () {
   }
 ======
Reduced further: ---- module ztest; struct StreamRange { ~this() {} void put() {} } void zcompress(StreamRange ro) { void writeOBuf() { enum e = is(typeof({ro.put();})); ro.put(); } writeOBuf(); } void main() { zcompress(StreamRange()); } ---- Looks like a compiler bug to me.
Mar 03 2015