www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21669] New: closure over type with destructor allows

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

          Issue ID: 21669
           Summary: closure over type with destructor allows accessing
                    destroyed value if used after scope exits
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schveiguy gmail.com

An example:

import std.stdio;
struct S
{
   int i;
   ~this() { i = -1;}
}

void main()
{
   int delegate() dg;
   {
      S s;
      dg = () { return s.i; };
      writeln(dg()); // 0
   }
   writeln(dg()); // -1
}

Allowing access to a destroyed variable where a destructor has invalidated the
object should not be allowed. Prior to 2.053, this was an error. According to
Paul Backus, the responsible PR was this:
https://github.com/dlang/dmd/pull/5292

One who has created a delegate would probably expect that when calling the
delegate at a later time, the state of the object should be preserved, and not
destroyed. For some cases, it might be dangerous safety-wise to use an object
after destruction.

Fixing this issue probably requires a long deprecation period, as no doubt code
that depends on this (or works in spite of the dangers) exists today.

--
Feb 27