It is illegal for a goto to enter a try or finally block in D. For consistency it should also be illegal to enter a catch block via goto also.
GDC has been emitting an error since 2010, but DMD will happily continue to accept such code.
----
Previous ML discussions:
http://forum.dlang.org/post/[email protected]http://forum.dlang.org/post/[email protected]
----
Previous bug report which the fix was to disallow goto into TryStatement's.
https://issues.dlang.org/show_bug.cgi?id=4655
This error is not documented as far as I can see (it should be!)
----
Previous bug report which introduced an error for goto's into catch blocks only if an initialisation was skipped.
https://issues.dlang.org/show_bug.cgi?id=602
----
What the specification has to say about 'finally' blocks:
"A FinallyStatement may not exit with a goto, break, continue, or return; nor may it be entered with a goto."
-- http://dlang.org/statement.html#TryStatement
Reasoning for this (Quote from Walter in first ML link above):
"On a related note, doing a goto into a finally block is pretty problematic to support because it's actually a mini-function."
In a way, GDC is exactly the same as DMD here, but also with try and catch blocks. The difference being that it's not a function, it is treated as an isolated area/block that is inaccessible from the outside.
----
Comment #1 by razvan.nitu1305 — 2023-02-13T14:42:18Z
This code:
void test8()
{
int a;
goto L2; // Error: `goto` skips declaration of variable `test.test8.e` at test.d(9)
try {
a += 2;
}
catch (Exception e) {
a += 3;
L2: ;
a += 100;
}
assert(a == 100);
}
fails to compiler, so I guess we can consider this fixed.
Comment #2 by ibuclaw — 2023-02-14T00:03:20Z
Nope still a bug in DMD.
```
void test8()
{
int a;
goto L2; // BOOM! Jump into a forbidden EH region
try {
a += 2;
}
catch (Exception) {
a += 3;
L2: ;
a += 100;
}
assert(a == 100);
}
```
Comment #3 by robert.schadek — 2024-12-13T18:42:25Z