www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19512] New: Exception during scope(exit) of an exception


          Issue ID: 19512
           Summary: Exception during scope(exit) of an exception yields
                    undefined behavior
           Product: D
           Version: D2
          Hardware: All
               URL: http://dlang.org/
                OS: All
            Status: NEW
          Severity: major
          Priority: P3
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: eyal weka.io

An exception during scope(exit) handler of an already-propagating exception
does not get handled correctly. Under some circumstances, it triggers missing
destructor bugs too.

Example program:

// use puts, not writeln, because puts is nothrow. any code that may throw
prevents some of the bug from reproducing
import core.stdc.stdio: puts;

struct C {
    this(int x) {}
    ~this() { puts("C.~this()"); }

struct D {
    auto c() { return C(1); }

D d;

void foo() {
    with(d.c) {
        puts("This code is reached");
        // The exception here seems to long-jump back to the original exception
        try { throw new Exception("Exc"); }
        catch(Exception e) { puts("This handler is never reached"); }
        // Iff ONLY nothrow things are done below here, then d.c's dtor does
not run at all!

        puts("This code is NOT reached, and C's destructor never runs!");

void main() {
    scope(exit) { foo(); }
    throw new Throwable("Bomb");

Dec 24 2018