www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4082] New: nothrow main() can throw

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4082

           Summary: nothrow main() can throw
           Product: D
           Version: future
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This program compiles with dmd 2.043, and the main() throws, even if it's a
nothrow function:


struct Foo {
    ~this() { throw new Exception(""); }
}
nothrow void main() {
    Foo f;
    goto NEXT;
    NEXT:;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 12 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4082


Byron Heads <bheads emich.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bheads emich.edu



Same for dmd 2.047

It seems to be a problem with the goto.

The compile errors on this code:

struct foo {
        ~this() { throw new Exception( "BAD!" ); }
}

void bar() {}

nothrow void main()
{
        foo f;
        bar();
        goto STOP;
        STOP:;
}


Error: function D main 'main' is nothrow yet may throw

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 15 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4082




Since it all applies to FuncDeclaration::semantic3(...), I'll add this case:

  void b(Test t) nothrow {
  }

  struct Test {
    ~this() {
      throw new Exception("i am throwing");
    }
  }

The compiler checks for thrown exceptions or - more generally - the block exit
state in two cases:

1)
https://github.com/D-Programming-Language/dmd/blob/869d6dbffc4ab85576a39f19085ab7270ae2191e/src/func.c#L1301

2)
https://github.com/D-Programming-Language/dmd/blob/869d6dbffc4ab85576a39f19085ab7270ae2191e/src/func.c#L1577

(2) handles the case of appending destructor calls for structs passed by value
to a function. Functions without return/throw/etc. just get the destructors
appended, while all others get them wrapped in a try-finally clause to ensure
they are called. Since destructors may throw and the function isn't checked
again after their addition, nothrow doesn't necessarily hold here. That's the
case in the above example.

(1) is the normal check if a function is nothrow, but throws. blockExit()
determines the way any code block exits. This may be through 'return', 'throw',
'goto', halt (assert(0)?), 'break', 'continue' or the execution may
unconditionally succeed or 'fall through'. That's my understanding anyways.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 15 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4082




It also happens when I add a switch statement and the goto is a 'goto case
...'. It seems the BEgoto flag has a viral effect. I'll try to fix it.

    int x;
    switch(x)
    {
        case 1:
            break;
        case 0:
            goto case 1; // disables nothrow check
        default:
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 15 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4082




Ok I think I have fixed it. The original case was due to try-finally-statements
not checking their finally section for thrown exceptions. And the goto made it
so that the destructor call got wrapped in a try-finally where it was hidden
from the compilers eyes.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 15 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4082




Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/748f3509827d6e0e1c1efabddc23de24aa3873e9
fix issue 4082 - Finally-block hides thrown exceptions from nothrow attribute

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 22 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4082


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



Closed.

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