Simple example that compiles with dmd but runtime segfaults at the location of 'goto'. This is also uncompilable with gdc, and would cause an ICE if not for working around the problem as per https://github.com/D-Programming-Language/dmd/pull/2176.
void main()
{
int a;
goto L2; // BOOM!
try { }
catch (Exception e) {
L2: ;
a += 100;
}
assert(a == 100);
}
The most obvious wrong thing about code like this is that it skips over the initialisation of 'e', which is a direct violation of the spec for GotoStatements. But if the code is actively removed from the front-end, that makes checking this violation impossible in lower layers of the code generation routines. So we need to be able to achieve this in the front-end.
Comment #1 by ibuclaw — 2013-07-23T07:32:05Z
(In reply to comment #0)
>
> void main()
> {
> int a;
> goto L2; // BOOM!
>
> try { }
> catch (Exception e) {
> L2: ;
> a += 100;
> }
> assert(a == 100);
> }
>
For clarification, the front-end currently passes this to the back-end.
void main()
{
int a;
goto L2; // BOOM!
assert(a == 100);
}
Comment #2 by yebblies — 2013-11-20T08:54:27Z
This is just silly. Removing the try-catch if the try block is empty is plain wrong.
Comment #3 by blah38621 — 2013-11-20T09:06:43Z
C# require that goto's only be used to jump to labels in the same, or a parent, scope, and never to a child scope. Would that be a valid way to eliminate this problem?
Comment #4 by ibuclaw — 2013-11-20T09:16:20Z
(In reply to comment #2)
> This is just silly. Removing the try-catch if the try block is empty is plain
> wrong.
And it's not even an 'optimisation' :o)
Comment #5 by ibuclaw — 2013-11-20T09:23:00Z
(In reply to comment #3)
> C# require that goto's only be used to jump to labels in the same, or a parent,
> scope, and never to a child scope. Would that be a valid way to eliminate this
> problem?
GDC's way of eliminating the problem is to error when you try to goto into a try *or* catch block... but this is only detected in the glue layer. If the front-end omits the block then I am unable to properly analyse the codegen. :)
Hopefully, Daniel's patch to get for skipped initialisations will include this so I no longer require this glue-layer code checking.