Thread overview
[Issue 4082] New: nothrow main() can throw
Jun 15, 2010
Byron Heads
Aug 15, 2012
Marco Leise
Aug 15, 2012
Marco Leise
Aug 15, 2012
Marco Leise
April 12, 2010
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


--- Comment #0 from bearophile_hugs@eml.cc 2010-04-12 04:26:37 PDT ---
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: -------
June 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4082


Byron Heads <bheads@emich.edu> changed:

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


--- Comment #1 from Byron Heads <bheads@emich.edu> 2010-06-15 14:14:19 PDT ---
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: -------
August 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4082



--- Comment #2 from Marco Leise <Marco.Leise@gmx.de> 2012-08-15 01:06:15 PDT ---
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: -------
August 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4082



--- Comment #3 from Marco Leise <Marco.Leise@gmx.de> 2012-08-15 02:12:57 PDT ---
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: -------
August 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4082



--- Comment #4 from Marco Leise <Marco.Leise@gmx.de> 2012-08-15 03:05:26 PDT ---
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: -------
October 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4082



--- Comment #5 from github-bugzilla@puremagic.com 2012-10-22 00:46:53 PDT ---
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: -------
October 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4082


bearophile_hugs@eml.cc changed:

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


--- Comment #6 from bearophile_hugs@eml.cc 2012-10-22 02:23:34 PDT ---
Closed.

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