Consensus on goto's into catch blocks

monarch_dodra monarchdodra at gmail.com
Thu Jun 13 11:38:34 PDT 2013


On Thursday, 13 June 2013 at 17:35:41 UTC, Iain Buclaw wrote:
> Can someone remind me again what was last agreed when I brought 
> this up?
>
> I seem to recall that this should be disallowed as is 
> practically always a bug, also, and it skips any initialisation 
> of the exception object. (See: http://dlang.org/statement.html 
> - "It is illegal for a GotoStatement to be used to skip 
> initializations.")
>
> Current failing test I want to have removed from the test suite.
>
> test/runnable/eh.d:
> void test8()
> {
>   int a;
>   goto L2;    // gdc Error: cannot goto into catch block
>
>   try {
>       a += 2;
>   }
>   catch (Exception e) {
>       a += 3;
> L2: ;
>       a += 100;
>   }
>   assert(a == 100);
> }
>
>
> Thanks
> Iain.

For what it's worth, this C++ program with GCC:

int main()
{
    goto L2;
    try {
        throw 5;
    }
    catch (int e) {
L2: ;
    }
}

produces:
main.cpp|| In function 'int main()':|
main.cpp|8|error: jump to label 'L2' [-fpermissive]|
main.cpp|3|error:   from here [-fpermissive]|
main.cpp|7|error:   crosses initialization of 'int e'|
main.cpp|8|error:   enters catch block|

I know you can add -permissive for GCC, or by default with VS*,
and crossed initliazers will be default constructed. That said, I
think it is mostly a workaround for legacy C, and *really* bad
practice to do it anyways.

So I think bottom line is: Invalid according to strict grammar,
and for a good reason.

*Amonst other things, like passing temporaries by ref. :puke: I
hate VS so much. PS: I'm responsible for cross compiling in my
company. The amount of shit VS allows is crazy scary.


More information about the Digitalmars-d mailing list