Thread overview
[Issue 6659] New: Destructor in range foreach called after initialization
Sep 13, 2011
dawg@dawgfoto.de
Mar 23, 2012
Kenji Hara
Mar 23, 2012
Kenji Hara
Mar 29, 2012
Walter Bright
September 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6659

           Summary: Destructor in range foreach called after
                    initialization
           Product: D
           Version: D2
          Platform: Other
        OS/Version: FreeBSD
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: dawg@dawgfoto.de


--- Comment #0 from dawg@dawgfoto.de 2011-09-13 00:11:52 PDT ---
void main()
{
    static struct Iter
    {
        ~this()
        {
            ++_dtor;
        }

        bool opCmp(ref const Iter rhs) { return _pos == rhs._pos; }
        void opUnary(string op:"++")() { ++_pos; }
        size_t _pos;

        static size_t _dtor;
    }

    foreach(ref iter; Iter(0) .. Iter(10))
    {
        assert(Iter._dtor == 0);
    }
    assert(Iter._dtor == 2);
}

---

The iteration is done using destructed instances.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6659


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
           Platform|Other                       |All
         OS/Version|FreeBSD                     |All
           Severity|normal                      |major


--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2012-03-23 05:52:32 PDT ---
A foreach range statement:

foreach (iter; lower .. upper) {}

is always translated to a for statement:

for (auto iter = lower, limit = upper; iter != upper; ++iter) {}

So, this is a lifetime issue declared in for statement Initialize part.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6659


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-03-23 06:04:47 PDT ---
https://github.com/D-Programming-Language/dmd/pull/826

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6659



--- Comment #3 from github-bugzilla@puremagic.com 2012-03-28 21:42:31 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/b5aa4bc4cdb52ed6996e0afab9368fa6d0ae5934 Merge pull request #826 from 9rnsr/fix6659

fix Issue 6659 - Destructor in range foreach called after initialization

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6659


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------