www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16362] New: `foreach (ref v; range)` with non-ref returning

https://issues.dlang.org/show_bug.cgi?id=16362

          Issue ID: 16362
           Summary: `foreach (ref v; range)` with non-ref returning
                    `.front()` missing dtors
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ketmar ketmar.no-ip.org

the following code fails last assert:

int allocCount;


struct Repeat(T) {
  alias Type = T;
  Type value;
   property empty () const { return false; }
  void popFront() {}
   property ref Type front() { return value; }
}

struct Take(Range) {
  alias Type = Range.Type;
  Range range;
  size_t limit;
   property empty () const { return !limit; }
  void popFront () { --limit; }
  /*ref*/ Type front () { return range.front; }
}

auto repeat(Type) (auto ref Type t) { return Repeat!Type(t); }

auto take(Range) (auto ref Range range, size_t limit) { return
Take!Range(range,limit); }

struct Test {
  this (int id_) { ++allocCount; }
  this (this) { ++allocCount; }
  ~this () { --allocCount; }
}

void testit () {
  int count;
  {
    auto arr = Test(1).repeat().take(2);
    foreach (ref v; arr) {
      ++count;
    }
  }
  if (count == 0) assert(0);
  assert(allocCount == 0);
}


void main () { testit(); }


if one will remove `ref` from loop, or uncomment `ref` in `.front()`, assertion
pass.

--
Aug 07 2016