www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19602] New: Under some circumstances: throwing Error

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

          Issue ID: 19602
           Summary: Under some circumstances: throwing Error subclasses
                    unwinds without invoking dtors
           Product: D
           Version: D2
          Hardware: All
               URL: http://dlang.org/
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: eyal weka.io

Reproduction:

import std.stdio;
import std.traits;

struct S {
    this(int) { writeln("this(int)"); }
    ~this() { writeln("~this()"); }
}

void dtorsOk() {
    with(S(1)) { throw new Error("Err"); } // dtor call generated here
}

// inferred as "nothrow"
auto sneakyThrow() { throw new Error("Err"); }

void failsToDtor() {
    with(S(1)) sneakyThrow(); // dtor call skipped here, incorrectly
}

// And this is the reason:
static assert(hasFunctionAttributes!(sneakyThrow, "nothrow"));

unittest {
    writeln("dtorsOk:");
    try dtorsOk();
    catch(Error) {}

    writeln("failsToDtor:");
    try failsToDtor();
    catch(Error) {}
}

Prints:

dtorsOk:
this(int)
~this()
failsToDtor:
this(int)

--
Jan 21 2019