Bug 4082 – nothrow main() can throw

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2010-04-12T04:26:00Z
Last change time
2012-10-22T02:23:34Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-04-12T04:26:37Z
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:; }
Comment #1 by bheads — 2010-06-15T14:14:19Z
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
Comment #2 by Marco.Leise — 2012-08-15T01:06:15Z
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.
Comment #3 by Marco.Leise — 2012-08-15T02:12:57Z
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: }
Comment #4 by Marco.Leise — 2012-08-15T03:05:26Z
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.
Comment #5 by github-bugzilla — 2012-10-22T00:46:53Z
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
Comment #6 by bearophile_hugs — 2012-10-22T02:23:34Z
Closed.