www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16678] New: [REG] Fix for issue 16193 creates major breakage

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

          Issue ID: 16678
           Summary: [REG] Fix for issue 16193 creates major breakage
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: public dicebot.lv

Original issue: https://issues.dlang.org/show_bug.cgi?id=16193

Change in
https://github.com/dlang/dmd/commit/f7819c898a05d28297024b7355d4ace94f1e4465
(later adjusted in
https://github.com/dlang/dmd/commit/f55b4d970b9463fda459fedf13bbcd58369d9ee9)
fixed that issue by conservatively allocating a closure unless `opApply` is
scope.

However, this breaks existing code if closure is attempted on struct with
destructor:

$ cat sample.d
unittest
{
    static struct External
    {
        ~this () { }

        void foo () { }
    }

    static struct Iterable
    {
        int opApply ( int delegate ( int ) dg )
        {
            return dg(42);
        }
    }

    static void foo (External s1, Iterable s2)
    {
        foreach (x; s2)
        {
            s1.foo();
        }
    }

    foo (External.init, Iterable.init);
}

$ dmd-dev -transition=safe -unittest -main -run sample.d 
sample.d(20): To enforce  safe compiler allocates a closure unless the
opApply() uses 'scope'
sample.d(18): Error: variable sample.__unittestL1_1.foo.s1 has scoped
destruction, cannot build closure

--
Nov 10 2016