[Issue 16362] New: `foreach (ref v; range)` with non-ref returning `.front()` missing dtors
    via Digitalmars-d-bugs 
    digitalmars-d-bugs at puremagic.com
       
    Sun Aug  7 18:16:11 PDT 2016
    
    
  
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 at puremagic.com
          Reporter: ketmar at 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.
--
    
    
More information about the Digitalmars-d-bugs
mailing list